As your code grows larger and more complex, it becomes important to modularise your code by breaking it up into functions. This abstraction of functionality is often desired for repetitive tasks and easy maintenance.
In programming, a function is a sequence of expressions that performs one or multiple related tasks, packed in a single callable unit.
Functions are defined using the def keyword. We have already seen the syntax for defining functions on multiple occasions, but it was always provided. We have also seen multiple examples of function usage, with built-in functions like len() and range().
Here name is the designated name for the function, it's the reference that will be used in succeeding code.
A function can have a variable amount of parameters, ranging from zero to a lot. Similar to loops, these parameters are to be used in the subsequent code block contained by the function.
The main routine of the function is defined in the indented statement, the statement can be any valid program and can thus contain conditional constructs, loops, or even other function definitions.
Optionally, a function can return a number of values. If the function contains a statement, it results in the value(s) when evaluated. If not, the function returns instead.
For example, here we see the function definition of .
The function has as name.
The function has two parameters, and .
The function includes one statement, the variable assignment for .
The function returns the value of , which is equal to .
|
|
def summation(x, y): val = x + y return val
print(summation(1, 2))
|
3
|
|
When talking about functions there is a difference between defining a function and calling a function. When we define a function using the keyword, we are simply specifying the function's behaviour. When a function is defined it is not executed, its instructions are saved for future reference. Only when a function is called, it is actually executed.
As mentioned, there is a difference between functions that return values, and functions that do not. It is vital to realize that, while results in output, it is not the same as , which conversely doesn't output anything directly.
In the first block of code we define the functions and , one prints and one returns . We observe that when we call both functions only is visible in the output, highlighting the difference between and .
In contrast, when we assign the functions to variables, we observe that while is printed again, because we call the function while assigning it to a variable, it is not actually stored in the variable , while is stored in the variable .
|
|
def hello(): print("Hello, world!")
def hi(): return "Hi, world!"
hello() hi()
|
Hello, world!
|
a = hello() b = hi()
print(a) print(b)
|
Hello, world! None Hi, world!
|
|
The terms parameter and argument are used quite often in the context of functions, There is a subtle difference between the two; a function has parameters and takes arguments. In other words, the variables specified in the function definition are referred to as parameters, while a function that is called with values for those parameters has arguments.
The function has two parameters and . The same function is called in line 4 with the two arguments and .
|
|
def combine(a, b): return a + " and " + b
var = combine("me", "you") print(var)
|
me and you
|
|