89DEVs

How to call a function in Python?

How to call a function in Python? Functions are a main component of every major programming language, because they make your code reusable. A function has to be declared once, but can be used as often as necessary. This makes your code modular and also easier to maintain. For example if a change needs to be done, it has to be done only in one place not in different places across your code. For large projects, this is a huge timesaver and also makes collaboration with oder developers much easier.

How to define a function?

A function is defined by using the def keyword, followed by the name of the function. The name is then followed by round brackets () and a colon. The actual content of the function follows on the next line, important to remember is the indentation of three spaces used in python. If this indentation is not used, an error will be raised and the script won't run. For this example, let's define a function called test that just prints the word test to the screen. # how to define a function def test(): print('test') If we run this code, nothing happens because the function is only defined but not yet called. To actually receive some kind of result, we need to call the function.

How to call a function?

Now let's finally call the function test, which we defined in the last section. To do so, we write the function name followed by brackets (). Important to remember is, that a function has to be defined before it is called. The only exception are built-in functions, they are already defined by python and can be called instantly. And after importing a module, new functions or more precise so called methods are available. # how to call a function test() The function is called and the code inside the function is executed, which prints the word test to the screen. test

How to return values from a function?

A function doesn't have to print something to the screen, it can also be used to return values. This is useful if we want to use the values in the following part of our script. In this example a value is returned from the function and then printed to the screen outside of the function. # how to return values from a function def test(): myvalue = 1+1 return myvalue result = test() print(result) The function calculates the result of 1+1 and saves it to the variable myvalue. This variable is only available within the function, so we have to use the return keyword to make it available outside of the function. Outside of our function the return value is assigned to the variable test by using the following syntax: variable = function(). Finally, the variable result is printed and the result of our operation within the function is printed to the screen. 2

How to pass values to a function?

Our functions can also accept values which we can pass to the function using so called arguments. This is done by defining the accepted arguments first and then passing the values when the function is called. # how to pass values to a function def add(number1, number2): return number1 + number2 result = add(5, 10) print(result) In this example, the function add is defined and it is also defined that it can except 2 number as arguments. Then the function add() is called and the numbers 5 and 10 are passed to the function. Within the function both numbers are added up and then returned. The result of the operation is assigned to the variable result and then finally printed to the screen. 15

                
        

Summary


Click to jump to section