Keywords: function, def, input parameters, top-down programming
A function is defined (created) easily in Python by specifying which name the function should have and if the function should have some inparameters. A function is always terminated with the return command (return a result), or if all operations in the function have been performed.
Creating a function in Python is very simple
When creating a function, you need to specify the input parameters, and whether the function will return any values. We can design our function in several ways:
def function_name(input parameter): “Function Documentation” what the function should perform return value
Let’s take a closer look at the code above.
def function_name (input parameters):
Then, on line 2, we added a Function Documentation where we can explain what the function actually does/purpose
“Function Documentation”
Furthermore, the third line of code in our syntax above is where we write what the function should do, in other words, the operations it should perform.
what the funct should perform
Finally, we end the function by using the reserved word return and the value that the function should return.
return value
Note: all the code that is “inside” the function is written indented.
An essential principle in all programming is encapsulation. This means that each part of the program should “take care of its own”. Instead of writing large functions that handle different things, you encapsulate every small part of the program.
Imagine a function as a piece of the puzzle, where each piece of the puzzle makes the “smallest possible” calculation. Through this important principle, you can combine the logic from several different functions that together build large programs. Programming is thus like a jigsaw puzzle of functions combined to create large and intelligent programs.
Let’s take a simple example just to illustrate how we can use a function.
def my_function(x): "multiplies input parameter with 3" return 3 * x
So the only thing the function, that we named my_function, is to take the input parameter and multiply that value by 3. Additionally, the function should after that then return the value
If we try our function by sending the value 5 as input parameter
result = my_function(5) print(result)
We will get the result
15
If you are not sure what the function actually does, you can call the function description with help () that returns the function documentation:
help(my_function)
Will print the result
Help on function my_function in module __main__: my_function(x) Multiplies the input parameter by 3
Functions in Python is a powerful tool for reusing and structuring our code. We can use functions to simplify and break down our programs into smaller parts, and then solve the smaller pieces that together build the complete solution. Moreover, using a function is denominated as calling it (as we will look closer at in the coming article), and the result of that call is named what the function returns. When you declare a function, you need to specify the input parameters the function should receive and use and what the function should return.
def function_name(input parameter): “Function Documentation” what the function should perform return value