Functions and methods in Python

Functions and methods in Python

Source code better organize functions and methods in Python


Author / editor: Thomas Joos / Stephan Augsten

Subs of a program can be summarized in Python with functions.This serves to better structure the code and save the developer redundant work.

Companies on the subject

If certain functionalities are regularly required in different points in a program, it makes sense to summarize them in a function.Whenever the functionality is needed, it is sufficient to call up the respective function in the program.

At the same time, the program code becomes much clearer, since the cohes belonging together are summarized in the program.With this increasing overview, the sources of error also decrease, since an error -free function at all points in the code is flawless and is called up at any time.

Instead, developers implement the tasks at each code area without function, the number of possible sources of error increases, at the same time the overview drops.Functions are simply expressed under programs of a program.In addition to creating your own functions, functions are also integrated into Python that help with tasks.The integrated functions are also referred to as "Built-in Functions".

Call up functions

The Built-In-Functions and the self-created functions are called up by calling in the code of the function and then in brackets the associated parameters.This is a function call.An example is the use of the print function.

With print ("hello") "hello" is also output in the console.In this example, a help can be worked on with Help (print).The Help command also works with other functions, such as help (max).The Max function shows the highest value from a list of values:

Number = max ([1.4,10]) print (number)

The list using the example of the upper example with Max is a parameter of the function.If a function results in a result, as the "10" for the variable "number", the value can be used as an instance in the program

Create your own functions with "DEF"

If your own functions are created, these must meet the requirements that also apply to Built-in-Fucions.A function must be given a name that can be used to start with a function call from the code.Then it must be determined how data can be transferred from the program code to the function.

Finally, it is important to define the value the function returns.If there is no value, such as the highest value in a list via Max, then the function as a result "None" returns.

Own functions can be created with the keyword DEF.As a result, a simple function can be defined, for example, which provides the text "This is a text from a function":

DEF MEIN FULLATION (): Print ("This is a text from a function")

Print ("end of program")

Funktionen und Methoden in Python

In order to test the procedure, it is determined in parallel that the text is issued "program end".If the program is started, in this example only the string "end of program" appears as a result.This is because the function was only defined and the program continues with the actual code.

Conversely, this means that the function is only executed and the text shows when a functional call is explicitly:

DEF MEIN FULLATION (): Print ("This is a text from a function")meinefunktion()Print ("end of program")

In diesem Beispiel vor Print ("end of program") rufen wir über „meinefunktion()“ die vorher erstellte Funktion auf.Since the function does nothing other than the text "This is a text from a function", in this case the two lines of text appear:

This is a text from a functional program end

Call your own functions in the code

The advantage of functions is the possibility of being able to call them up in the code at any point and use your program code without having to type in everything.In this example, the function is called several times in different places and always issues your defined result:

DEF MEIN FULLATION (): Print ("This is a text from a function")meinefunktion()meinefunktion()meinefunktion()Print ("end of program")meinefunktion()

An important meaning of functions is to obtain values from the program code, to process it and then to pass it on to the program again.In the following example, the value "10" is handed over to the "Halification" function.The function halves the transferred value and outputs it:

def halbierung(wert1): print("Halbierung") print(wert1/2)halbierung(10)Print ("end of program")

By calling up the "Halification" function and the handover of the value "10", the function processes the transferred value as "value", carries out the calculation and issues the result.Then the program continues and reports the "end of the program".Functions can also be nested.It is possible to call up one or more functions in functions.

Use methods

Methods are functions that can be used depending on certain data types and instances.With the "List" data type, for example, the "Sort" method can be used with which a list can be sorted.A method refers to an instance.

For example, if a list (or.a one -dimensional array) of numbers as variable is saved with numbers = [1.5,3,2,10,7,6] and this list of numbers.Sort () sorted, the instance of this action is a sorted order in the form [1,2,3,6,5,10].The instance can be used in the code.Which methods are available depends on the data type used.

Methods generally function like functions and can treat variables as an object.In the following example, a text of a variable is stored (text) A new variable (text new) is then defined and using the Lower method, the variable "Text new" is converted as small letters and then output with the print function:

Text = "This is an example" Text new = text.Lower () print (text new)

The output would be accordingly:

This is an example

Methods therefore offer an object -oriented view of instances, variables and other areas in programming.The good thing about such a procedure is that the original value of a variable is not changed.Both the manipulated and the original value can be output via the print function:

Text = "This is an example" print (text.Lower ()) Print (text)

(ID: 47135449)