We have already seen and used some Functions such as
range() which are included within Python. But what are Functions really?
As in math, a Function is a basic structure that can accept inputs, perform some processing on those inputs, and give back a result. Let’s create a basic Function that will add two to a given number and give us back the result:
def add_function(input_number): result = input_number + 2 return result
A function’s definition begins with the keyword
def. After this is the function's name, which follows the same naming conventions as Variables. The Function's name is always followed immediately by a set of parenthesis. Inside the parenthesis you can place any number of input variables separated by commas (
,), which will be passed to the function when it is called, and are available within the body of the function. If a function does not expect any inputs you still include the parenthesis, however in this case there won't be anything inside of them, for example:
On its own, this code will only define what the Function does, but will not actually run any code. To execute the code inside the Function you have to call it somewhere within the script and pass it the proper inputs:
Here we call the Function by writing its name and passing in '2' as the input. The result of the Function (the number '4') will then be passed into the
print() Function which will print '4' to the console.
When you call a Function, you can either directly pass values or pass Variables that have values stored inside of them. For example, this code will call the Function in the same way:
var = 2 print(add_function(var))
Here the value of the
var variable, which in this case is 2, is being passed to the
add_function Function, and is then available within that Function through the
input_number Variable. Notice that the names of the two Variables
input_number don’t have to match. When a value gets passed to a Function it is reassigned to the Variable name declared in the Function definition, and the value is then available through that name.
In this case we refer to
var as a global variable since it stores the value '2' in the main script, while
input_number is a local variable since it stores that value only within the body of that Function. In this way, Functions 'wrap up' specific tasks and all the data that is necessary to execute that task to limit the number of global variables necessary in the main script.
The first line declaring the Function and its inputs ends with a colon (
:), which should be familiar by now, with the rest of the Function body inset from the first line. Optionally, if you want to return a value from the function back to the main script, you can end the function with the keyword
return, followed by the value or variable you want to return. Once the function hits on a
return statement, it will skip over the rest of the body and return the associated value. This can be used to create more complex behavior within the function:
def add_function(input_number): if input_number < 0: return 'Number must be positive!' result = input_number + 2 return result print(add_function(-2)) print(add_function(2))
You can see that in this case, if the input is less than zero the conditional will be met, which causes the first
return statement to run, skipping the rest of the code in the Function. However, if the number is equal or greater than zero, the conditional will be skipped causing the rest of the Function to run, ending with the second
You can pass any number of inputs into a Function, but the number of inputs must always match between what is defined in the Function, and what is passed into it when the Function is called. For example, we can expand our simple addition Function to accept two numbers to be added:
def add_two_numbers(input_number_1, input_number_2): result = input_number_1 + input_number_2 return result print(add_two_numbers(2, 3))
You can also return multiple values by separateing them with a comma (
,) after the return statement. In this case, you also need to provide the same number of variables to which to assign the results of executing the Function. Let’s expand our Function to return both the addition and multiplication of two numbers:
def two_numbers(input_number_1, inputinput_number_2Number2): addition = input_number_1 + input_number_2 multiplication = input_number_1 * input_number_2 return addition, multiplication val_1, val_2 = twoNumbers(2, 3) print('addition: ' + str(val_1)) print('multiplication: ' + str(val_2))
Functions are extremely useful for creating efficient and readable code. Wrapping up sections of code into custom Functions allow you (and possibly others) to reuse code in a very efficient way, and also forces you to be explicit about the set of operations involved in accomplishing a certain task in your code, as well as the data needed execute it.
You can see that the basic definition of Functions is quite simple, however you can quickly start to define more advanced logics, where Functions call each other and pass around inputs and returns in highly complex ways. You can even pass a Function as an input into another function and create Functions which call themselves. These are called Recursive Functions which we will look at in a later module.