Introduction
We have frequently used
functions so far, and they are essentially
subroutines, or
subprograms, that help us process information.
For example, the
len function takes an object as a parameter and returns its length, whether it's a string, list, tuple, dictionary, or set:
Definition 1. A
subprogram is understood to be
a set consisting of declarations and instructions written for a specific processing task,
a set implemented separately and identified by a name.
We can enumerate some of the advantages of using subprograms:
•
code reuse - once written, a subprogram can be used by multiple programs;
•
algorithm development by decomposing the problem into simpler ones - this way, we solve the problem much more easily;
•
reducing the number of errors that can occur when writing programs;
•
easily detecting errors - we first check the subprograms, then how we assembled them (called them within the program);
• creating
easy-to-follow (readable) programs.
Functions can be grouped into
modules, which are separate files with the
*.py extension, that we can include in a program using
the
import directive (you will see how later).
Creating a Function
To begin, we will create a function within a program. Let's start with a simple case.
Example. Read
n, a natural number. Write a program that prints the calculated value of the expression:
Analyze the following program, which uses a function named
subp (short for subprogram), created by us:
DETAILS
The function header is "
def subp(x):", and its body is the indented compound statement (set of indented instructions):
The function (subprogram) is called "
subp".
The function has a parameter named
x. Its role is important because it specifies the value for which the expression needs to be calculated.
As we will see, it is possible to have multiple parameters.
The function has its own variables - that is, variables defined/declared inside it. In the example,
s and
i.
These variables are called
local variables.
We saw that the function returns a certain result! Notice the mechanism by which we achieved this.
We calculate the expression as usual. The result is stored in the local variable
s. Through the instruction "
return s",
the function returns the value of the variable
s.
Formal Parameters vs. Actual Parameters
In the terminology used in subprogram theory - in particular, in the case of functions - the terms
formal parameters and
actual parameters are used.
Definition 2. Parameters found in the function header are called
formal parameters.
When writing a function, we do not know the actual values of the parameters.
The function must return the correct result, regardless of their values. From this point of view, they are called
formal.
Definition 3. Parameters used in the call are called
actual parameters (
arguments).
In the call, things are different: their values are known. Therefore, they are called
actual parameters.
For example, in the call "
result = subp(n)", the actual parameter is
n.
Proceed to the next page.