0% found this document useful (0 votes)
7 views2 pages

Section5 1-2+notes+

The document explains the concept of functions in programming, emphasizing their role in modularizing code for better organization and reusability. It outlines the characteristics of void and value-returning functions, as well as the importance of defining functions before use, including naming conventions and syntax. Additionally, it provides an example of a function definition and its invocation in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Section5 1-2+notes+

The document explains the concept of functions in programming, emphasizing their role in modularizing code for better organization and reusability. It outlines the characteristics of void and value-returning functions, as well as the importance of defining functions before use, including naming conventions and syntax. Additionally, it provides an example of a function definition and its invocation in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Section 5.

1 Notes

A program is broken into manageable pieces know as functions.

A function is a group of statements within a program that accomplishes a specific task.

The process of splitting a large task into manageable small tasks is called divide-and-conquer. This
process is what facilitates program modularization.

A modularized program is one that is written as a collection of functions. The concept of program
modularization is important in programming for the following reasons.

1. The program is simpler and it is easier to understand.


2. A function can be reused many times within the program without having to rewrite it.
3. It is easier to test functions individually.
4. In program design and development, a programmer can manage only the assigned functions
within a program for faster code development and better teamwork.

There are two types of functions: void and value-returning functions. A void function executes
statements in the function and terminates, whereas, a value-returning function executes statements in
the function and returns value to the caller (another function or part of the program that calls it).

The print function is an example of a void function. Although, it displays a string to the screen, it does
not return value to the caller.

The float, input and int functions are examples of a value-returning function.

Section 5.2 Notes

A function must be defined before it is used in a program.

The code for a function is called function definition. Like a variable, a function must have a name that is
used to reference it in the computer memory. The function name follows the same naming convention
used in a variable, namely,

1. must begin with a letter or an underscore (_).


2. must not include special characters (e.g., space, %, #, $, -,+, *) except underscore (_).
3. It is case sensitive, meaning that calculate_rate is not the same as Calculate_rate.

As a good programming style, it is advisable to use verbs as function names to describe the actions of
the function.

The general format (syntax) for a function definition (one of many) is

def function_name(): #this is the standard function header.


statement1
statement2
….
statementN
The first line is called the function header and it has the reserved word def, followed by function name
(with at least a space separator), open and close parentheses and a colon (:). The function block is a
group of consistently indented statements; it is also called the function body. The function block is
executed when a function is called.

A block is set of consistently indented statements that form a group.

A function definition causes the function to be loaded to the memory, it does not cause it to execute. A
function MUST be called in a program to execute it.

For a void function a call will look like:

function_name()

For example, define a function that will prompt the user to enter an integer, the function will display
whether the integer entered is divisible by 5.

1. #function definition
2. def is_divisible_by_5(): #function header
3. n = int(input('Enter an integer')) #prompt user to enter an integer
4. if n % 5 ==0: #check if divisible by
5. print(n, 'is divisble by 5')
6. else:
7. print(n, 'is not divisible by 5')

8. #function call
9. is_divisible_by_5()

Here, the function name is is_divisible_by_5 (see line 2, the function header). The function call (also
known as function invocation) is in line 9. Lines 2-7 form the function definition and at this point of the
program, the function is loaded to the memory. When line 9 is executed, the program flow goes to the
function loaded in the memory and execute it by executing all statements in the function.

The statements in the function are consistently indented; this is important because, it tells the Python
interpreter where the block start and end.

You might also like