0% found this document useful (0 votes)
5 views

Revision Notes - 38 Functions and Procedures (1)

The document discusses the importance of functions and procedures in programming, highlighting their advantages such as reduced code size, improved readability, and easier debugging. It explains the definition and execution process of functions and procedures, providing examples in Python for calculating the volume of a cylinder and printing text multiple times. Additionally, it covers the concepts of global and local variables, along with activities and questions for further understanding.

Uploaded by

mrdaxton
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Revision Notes - 38 Functions and Procedures (1)

The document discusses the importance of functions and procedures in programming, highlighting their advantages such as reduced code size, improved readability, and easier debugging. It explains the definition and execution process of functions and procedures, providing examples in Python for calculating the volume of a cylinder and printing text multiple times. Additionally, it covers the concepts of global and local variables, along with activities and questions for further understanding.

Uploaded by

mrdaxton
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Teach Computer

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.

The definition of a function requires:


• Name of function
• Inputs to be passed to the function
• Function code
• Outputs that are produced by the function

teachcomputerscience.co
m
Execution of Function
main program definition

Execution of
Function call
function code

Continued Calculated
execution of results by
main program function

Figure 1: Function call

Let us understand more about functions using an example.


Python program to calculate volume of cylinder is given below.
Volume of cylinder=(area of base)× height
The area of base is circle in cylinder. Let us use a function to
calculate the area of circle in our program.
#Function definition
def area(radius):
area = 3.142*radius*radius
return area

#main program to find volume of cylinder and cone


radius_cyl=int(input('Enter the radius of cylinder: '))
height_cyl=int(input('Enter the height of cylinder: '))
volume_cylinder=area(radius_cyl)*height_cyl
print("Volume of cylinder is: %.2f" %volume_cylinder)

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)

Combining volume of cylinder and volume of cone in a same


program and executing it results in the following output:

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)

#main program to print text


string=str(input('Enter the text: '))
times=int(input('Enter the number of times to print text: '))
print_procedure(string,times)
print('Execution of procedure is complete')
The procedure ‘print_procedure’ is defined and is called from
the main program. It can be noted that the procedure does not
return any value. Here it prints the text multiple times. Once,
the execution of the procedure is complete, the control is
passed to the main program. The output of the above program
is:

teachcomputerscience.co
m
Sub-routines in flowchart
The flowchart symbol for representing a sub-routine is:

Sub-routine

Consider the procedure program explained in the previous


section to print text entered by user several times. It can be
represented in a flowchart as:

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

1. You have created a Python program to find out whether a


number is prime or not in topic “Basic programming
constructs”. Now, implement the same program using
function or procedure.

2. Which of the two: functions or procedures have you


implemented? Why?

teachcomputerscience.co
m
Activity-2
Duration: 15 minutes

1. Create a python program to find the smallest of two


numbers. Implement functions in your program.

2. Extend the program written above to find the smallest of


three numbers.

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

You might also like