Revision Notes - 38 Functions and Procedures (1)
Revision Notes - 38 Functions and Procedures (1)
Science
Functions and
procedures
teachcomputerscience.co
m
1
.
Revision notes
teachcomputerscience.co
m
Introduction
Programs implement several tasks. Many times similar tasks
are repeated. Typing the same code multiple times is time-
consuming. Such tasks are written as modules such as
functions, procedures or subprograms are written. These can
be called multiple times in the main program reducing the time
taken.
Advantages of using functions and procedures are:
• The overall size of the code is reduced as the codes are not
repeated several times.
• Use of functions and procedures improves the readability of
the program as only the name of these modules are used in
the main program.
• Reduces the time taken to write and test the program.
Repeated codes can be called from the main program and
its enough if they are tested once.
• Reduces the time taken to debug the program. The code of
functions and procedures are checked only once.
• Programming languages also provide in-built, pre-written
and thoroughly checked functions that can be used by
programmers. This saves time and reduces the possibility of
errors. For example: In VB.net, function InStr(‘Apple’,’ple’)
returns the position of sub-string ‘ple’ in string ‘Apple’. In
this case, the InStr function returns the value 3.
teachcomputerscience.co
m
Function
Functions are subprograms that can be called whenever
required from the main program. The inputs for the function are
passed from the main program. Once, the function code is
executed, the results are returned to the function call
statement of the main program. The use of function reduces
the time taken and improves the readability of the program.
User need not know the function code. It’s enough if the user
knows how to use the function, that is, what inputs are given,
what outputs are produced. This is an application of
abstraction.
The step-by-step process of a function call are:
i. The main program executes until it reaches a function call
statement.
ii. The function call statement transfers the control to
function. The inputs are also passed using this statement.
iii. The function code is executed. In other words, the input
passed to function is processed.
iv. The control is transferred back to the main program once
the execution of function is complete or a statement
passes control out of the function.
v. The results calculated by the function are returned to the
main program. This happens at the function call
statement. Thereafter, the main program continues the
execution of the program.
teachcomputerscience.co
m
Execution of Function
main program definition
Execution of
Function call
function code
Continued Calculated
execution of results by
main program function
teachcomputerscience.co
m
In the above function:
• Name of function: area
• Input passed to function: radius
• Function code: area=3.142 × radius × radius
• Output from the function: area
This area is calculated to find the volume of cylinder. The
function code can be reused to find the volume of cone too.
Volume of cone=(area of base)× height
radius_cone=int(input('Enter the radius of cone: '))
height_cone=int(input('Enter the height of cone: '))
volume_cone=1/3*area(radius_cone)*height_cone
print("Volume of cone is: %.2f" %volume_cone)
teachcomputerscience.co
m
Procedures
Similar to functions, procedures are sub-programs defined
outside the main program. The difference between procedures
and functions is procedures need not return a value but
functions return a value.
An example of Python program that uses a procedure to print
text entered by user several times is given below.
#Procedure definition
def print_procedure(text, num):
for i in range(0,num):
print(text)
teachcomputerscience.co
m
Sub-routines in flowchart
The flowchart symbol for representing a sub-routine is:
Sub-routine
START
INPUT
string
#Procedure definition
INPUT
times def print_procedure(text,
num):
print_procedure
(string, times)
for i in range(0,num):
print(text)
Print
‘Completed’
END
teachcomputerscience.co
m
Global and local variables
Variables and constants declared inside a function or procedure
is local to that sub-routine. To enable the usage of a variable
throughout the program, it is made global using the keyword
global.
An example showing the use of local and global variables in
Python is shown below.
x=9
def example():
global x
y=5
x=x*2
print(x)
print(y)
example()
In this program, x is a global variable and y is a local variable
to function example. The value of x can be accessed in main
program also but the value of y is undefined outside the
function example. Executing the program to print the values of
x and y in the main function,
x=9
def example():
global x
y=5
x=x*2
print(x)
print(y)
example()
print(x)
print(y)
teachcomputerscience.co
m
2
.
Activity
teachcomputerscience.co
m
Activity-1
Duration: 10 minutes
teachcomputerscience.co
m
Activity-2
Duration: 15 minutes
teachcomputerscience.co
m
3
.
End of topic
questions
teachcomputerscience.co
m
End of topic questions
1. What are the advantages of using functions and procedures
in programs?
2. What are functions?
3. Explain the function-call process in detail.
4. What parameters are included in function definition?
5. How are procedures different from functions?
6. Using functions or procedures, write a Python program to
check whether the given string is a palindrome or not.
teachcomputerscience.co
m