Page 1 of 1
Unit 1 - Functions
fotocopiabile per uso didattico Petrini - Informatica a colori © D Scuola SpA 1
Summary
In computer science, a function is a sequence of instructions, suitably structured, which
allows carrying out a specific action by acting on input data and returning output data.
Normally, each function can have a variable number of inputs but always a maximum of one
output.
The use of functions has numerous advantages, some of which are:
• It is easier to correct and maintain the code.
• The code is compact, more readable and reusable.
• It is easier to identify and solve problems.
In C, when introducing new functions into the program, we follow these three steps:
1. Function declaration
2. Function definition
3. Function invocation
In Python, when introducing new functions into the program, we follow these two steps:
1. Function definition
2. Function invocation
The input variables of a function are called formal parameters.
Variables passed to a function from the main program during function invocation are called
actual parameters.
In Python, functions do not necessarily need direct correspondence between actual
parameters and formal parameters as the latter can take on default values, if set
appropriately.
A variable defined outside of any function or block of code is called a global variable and is
visible from anywhere within the program.
A variable declared inside a function or a single block of code is called a local variable and is
visible only inside the function or block of code in which it was declared.
Local variables facilitate writing of programs as they refer to limited portions of code.
Furthermore, their permanence in memory is limited only to the execution of the function
in which they appear. The careful use of local variables facilitates code maintenance and
reuse.
In Python, the keyword global is used when we need to make changes to global variables
in local contexts (such as functions).
A particular case of function invocation is recursive function, which occurs when the
function invokes itself.
✔
✔
✔
✔
✔
✔
✔
✔
✔
✔
✔
✔