So far, we have known how to use basic control structures to perform sequential, looping and selecting tasks. In this tutorial, to take a further step, I would show you the way to help you to manage your program easier when the number of lines in your code become larger and to make the program more concise.
The first thing I wanna show you is functions. It can be seen as blocks of code written to execute a specific task. A function can return whether values (like numbers, strings, lists, etc.) by using the command return or nothing (for example, functions that are used just to display information).
How to create a function
To create a function, we use the structure below:
def name_of_the_function(parameters): (1) statements (2) (return value) (3)
Remember, just like when using for-looping structures or if-structures, we need an indentation to make the Python interpreter understand which parts of code belong to the function.
Elements of a function
OK, let’s take a look at the structure of the function above.
In the line (1), the command def help the interpreter know that you are defining a function, followed by the name of the function. In addition, a function could have parameters or not. In the next section, I will show you more detail about how to pass arguments into a function.
In the line (2), we put statements here to perform desired tasks.
The line (3) shows that a function could return a value or not, depending your purpose when writing it.
Together, let’s make a ‘Hello World’ program, but now, we will write a function to do it instead of only using the command print.
The say_hello above is a function that doesn’t return any values, its purpose is just to print the line “Hello World” on the screen.
Here is another example, in which, a function return a value of an addition.
Or you can even return a list.
All the codes above can be downloaded from my github: https://github.com/tenga061/basic_python_tutorials/blob/master/pytut7_functions/lab1_elements_of_a_function.py.
Parameters of a function
Now, it’s time to take a further detail in parameters in function and how to pass arguments into it.
Now, let’s tweak the ‘Hello world’ program above by adding a parameter ‘name’ to it. Our program now won’t say hello world when we call it but say hello to the name that we have entered, instead.
By that way, we added a parameter and passed argument to our function.
The code can be downloaded from my github: https://github.com/tenga061/basic_python_tutorials/blob/master/pytut7_functions/lab2_say_hello.py.
Passing a fixed number of arguments
Let’s take a look at another example below. The code show a function using Euclid algorithm to find the greatest common divisor of two nonnegative m and n.
Functions that we made above is kind of functions with fixed number of parameters. Because of this, be careful when calling a function with parameters, the number of arguments you pass need to be equal to the number of parameters, otherwise, errors will occur.
You can download the code from my github: https://github.com/tenga061/basic_python_tutorials/blob/master/pytut7_functions/lab3_euclid_gcd.py.
Passing an arbitrary number of arguments
So, to have a function that permit you to pass the arbitrary number of arguments, there is two ways.
The first one is to put all values into a list and pass the list to the function.
The second is to put the symbol ‘*’ before the parameters of the function.
The codes can be downloaded from the link: .
Using default values
Sometimes, you want to set the default value for the parameters.
For example, back to the ‘Hello World’ example above. Now, we set the default value of parameter ‘name’ of the function to ‘World’, so, if we don’t pass any value into the function when calling it, it will print ‘Hello World!’ again, otherwise, it will say hello to the name we have passed.
The code can be downloaded here: https://github.com/tenga061/basic_python_tutorials/blob/master/pytut7_functions/lab5_say_hello_2.py.
The scope of variables
The last thing in this post I wanna tell you is that how vital to know the scope of variables.
Let’s continue to learn from codes together.
You can download the code here: https://github.com/tenga061/basic_python_tutorials/blob/master/pytut7_functions/lab6_scope_of_variable_1.py.
So, you can see the results clearly. The variables a and b which are initialized in the function check_print just exist while the function was running.
Now, if you wanna use the variables a and b as global variables, how could you do that? What if we don’t declare variable names in the function like the code below?
It’s OK, but don’t do that frequently, it sometimes brings to you serious mistakes in your code. To use global variables, I prefer to put ‘global’ before the name of variable in the function.
The code can be downloaded here: https://github.com/tenga061/basic_python_tutorials/blob/master/pytut7_functions/lab7_scope_of_variable_2.py.
OK, that’s all I wanna talk about in this post, in the next tutorial, I will introduce basic of classes in Python.
Hope you enjoy it,