A function is a group of statements within a program that exists for the purpose of performing a specific task.
A
program that has been written with each task in its own function is called a modularized
program.
1. Benefits
of Modularizing a Program with Functions
Simpler
Code: A program’s code tends to
be simpler and easier to understand when it is broken down into functions.
Several small functions are much easier to read than one long sequence of statements.
Code
Reuse: Functions also reduce the duplication of code within
a program. If a specific operation is performed in several places in a program,
a function can be written once to perform that operation and then be executed
any time it is needed.
Better
Testing: When each task within a program is contained in its
own function, testing and debugging becomes simpler. Programmers can test each
function in a program individually, to determine whether it correctly performs
its operation. This makes it easier to isolate and fix errors.
Easier
Facilitation of Teamwork: Functions also make it easier for
programmers to work in teams. When a program is developed as a set of functions
that each performs an individual task, then different programmers can be assigned
the job of writing different functions. It leads to faster development.
2. Void
Functions and Value-Returning Functions
There are two types of functions: void functions and
value returning functions.
When a void function is called, it simply executes the statements it contains and
then terminates. When a value-returning function is called, it executes the statements that it contains, and then
it returns a value back to the statement that called it.
3. Defining and Calling a Function
To create a function you write its definition.
Here is the general format of a function definition in Python:
def function_name():
statement
statement
etc.
The first line is known as the function header.
It marks the beginning of the function definition.
The function header begins with the key word def,
followed by the name of the function, followed by a set of parentheses,
followed by a colon.
The next line is a set of statements known as a
block. A block is simply a set of statements that belong together as a
group.
A function definition specifies what a function
does, but it does not cause the function to execute. To execute a function, you
must call it.
When a function is called, the interpreter jumps to
that function and executes the statements in its block. Then, when the end of
the block is reached, the interpreter jumps back to the part of the program
that called the function, and the program resumes execution at that point.
Example:
# This program demonstrates a function.
# First, we define a function named message.
def message():
print('I am
Ashraf,')
print('computer
science faculty.')
# Call the
message function.
message()
In fact, it is common for a program to have a main
function that is called when the program starts. The main function then calls
other functions in the program as they are needed. It is often said that the
main function contains a program’s mainline logic, which is the overall logic
of the program.
Example:
# This program has two functions.
# define the main function.
def main():
print('I have
a message for you.')
message()
print('Goodbye!')
# Next we define the message function.
def message():
print('I am
Ashraf,')
print('computer
science faculty.')
# Call the main function.
main()
Designing
a Program to Use Functions
Programmers commonly use a technique known as
top-down design to break down an algorithm into functions.
This process is called top-down design because the programmer
begins by looking at the topmost level of tasks that must be performed and then
breaks down those tasks into lower levels of subtasks.
The
process of top-down design is performed in the following manner:
·
The overall task that the program is to
perform is broken down into a series of subtasks.
·
Each of the subtasks is examined to
determine whether it can be further broken down into more subtasks. This step
is repeated until no more subtasks can be identified.
·
Once all of the subtasks have been identified,
they are written in code.