Python Basics — Functions (cont.)
In the previous lesson, you’ve learned what functions are and how to implement them. In this lesson, we will learn more about Python's functions. This lesson covers optional parameters, named parameters, the return statement, and variable scopes. At the beginning of the lesson, let’s see the difference between parameters and arguments in Python.
What is the difference between parameters and arguments?
Parameters are variables in a function definition. Arguments are the actual values of these variables that you pass into the function’s parameters while calling the function.
Optional Parameters
In Python, a function can be defined with optional parameters. Optional parameters are parameters with default values. We can set a default value for a parameter using the assignment operator. In this case, when you call the function, there is no need to specify a value for an optional parameter. This is because the default value will be used if one is not specified. Let’s see how we can define optional parameters and use them.
Go ahead and run the code. The HelloNTimes
function takes two arguments, someone’s name, name
, and the number of times that it prints “Hello someone”, n
. As can be seen, n
is an optional parameter, and its default value is 3
. Now let’s call the function twice, one with the default argument and then without it.
When we provide a specific value for the optional argument, the function takes it and runs its body based on the provided value. However, if we don’t provide any value for the optional argument, the function uses the default value and prints out “Hello Mehdi” three times.
In Python, optional parameters MUST always go after non-optional parameters.
Named Parameters
If we forget the order of values that we need to pass in a function and run our code. Python interpreter issues an error message.
Try to run the following statement:
HelloNTimes(4, "Mehdi")
An error message is shown. The reason for issuing the error message is the function expects to take a string for the first argument and an integer for the second one, but we passed the arguments in the reverse order. Now the question is how we can solve this issue. In Python, you can use the named parameter assignment technique. In this technique, we explicitly assign values to parameter names during the function call. Let’s try it:
HelloNTimes(n = 4, name = "Mehdi")
Go ahead and run the cell. We explicitly assign the values to the arguments by referencing their names. So it does not issue any error message and prints out "Hello Mehdi"
four times.
Return Statement
Sometimes, we don’t want to print the data directly in a function; instead, we want to return a specific value and assign it to a variable. In this case, the function can return the value by adding the return
keyword, followed by the value to return. Now we can assign the result of the function call to a variable, as you see here.
The above code shows how the parameter is passed into the function that uses it to calculate the cube of the number and then returns the result to be stored into a variable. This variable can be used later with that value. Let’s implement the code and see its output.
Go ahead and run the cell. When we call cube
, the Python interpreter runs the function with 4
and returns its cube. It then stores that returned value within the result
variable, then the print
statement prints its value out.
You can also simply print the returned value in place.
print(cube(2))
We can only return a single variable with any data type in Python. When you want to return more than one piece of data, you can return a data collection (e.g., a list). Let’s look at the following piece of code:
Go ahead and run the cell. As you can see, the function returns a list, and then we can provide a proper output based on the data stored in the list.
Global vs. Local Scope
The variable scope is one of the fundamental concepts in programming, which deals with the accessibility of variables declared within a program. Here, we want to learn about global and local scopes in Python.
When we declare a variable to be accessible to an entire program, its scope is global. On the other hand, the local scope is about variables being declared and accessible only within functions, which cannot be accessed outside of the function. Once a function terminates, all variables declared inside it will be deleted. Let’s look at the difference between global and local scopes in the following code snippets.
num = 10def func_1(): num = 100 print("num inside func =",num)func_1()print("num outside func =",num)
Go ahead and run the cell. The function returns 100. If we then try to print the num
variable after function execution, it returns 10. Any time we use a variable in the function's local scope, it will look first in the local scope. That is why calling func_1()
returns 100 and not 10. If Python cannot find the variable in the local scope, it will only look in the global scope.
Now, let’s look at the following code snippet:
num = 10def func_2(): global num print("The num inside func =",num)num = 6func_2()print("num outside func =",num)
Go ahead and run the cell. Within the function definition, we use the keyword global
followed by the name of the global variable that we wish to access and alter. As can be seen on the output, calling func_2
returns 10
, because the function uses the global variable instead of the local one. However, the last print statement returns 6
, because we assign a new value to num
-as a global variable- inside the function, and this value is accessible outside the function.
Alright, I hope you enjoyed this lesson. In the next lesson, we will discuss Python methods.