Revision Notes -11 Functions
Revision Notes -11 Functions
Science
GCSE Python
Functions
teachcomputerscience.co
m
1
.
Revision notes
teachcomputerscience.co
m
Introduction
In the first topic of this course, “Introduction to Python”, we
learnt about simple procedures. Procedures allow us to group a
block of code under a name. This block of code can be called
anywhere in the program to execute the instructions in it. In
Python, the def keyword denotes the start of a procedure or
function definition. Defining a simple procedure named Hello,
#procedure Hello
def Hello():
print("Hello!, Welcome to Trinket")
When called, this procedure displays the text stated.
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 a function reduces
the time taken and improves the readability of the program.
The 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 is:
i. The main program executes until it reaches a function call
statement.
ii. The function call statement transfers the control to the
function. The inputs are also passed using this statement.
teachcomputerscience.co
m
i. The function code is executed. In other words, the input
passed to the function is processed.
ii. The control is transferred back to the main program once
the execution of the function is complete or a statement
passes the control out of the function.
iii. 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.
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
teachcomputerscience.co
m
Functions: Example
program
Let us understand more about functions using an example. Let
us use a function to calculate the area of a circle.
#function definition
def areaCircle(radius):
area = 3.142*radius*radius
return area
Calling this function,
#main program to call function areaCircle
r=int(input(“Enter radius of circle:”)
A=areaCircle(r)
print(“The area of circle is: %.2f” %A)
Executing the above program, the result obtained is given.
teachcomputerscience.co
m
teachcomputerscience.co
m
Returning several values
from a function
A function may return several values. In such cases, the
different values are separated by a comma. A function that
takes in two input values, performs the four basic arithmetic
operations and returns the results is given.
#function definition
def basicArithmetic(a,b):
sum1=a+b
diff=a-b
product=a*b
quotient=a/b
return sum1, diff, product, quotient
#main program
num1=int(input('Enter number1: ‘))
num2=int(input('Enter number2: ‘))
p, q, r, s=basicArithmetic(num1,num2)
print("Sum =", p)
print("Difference =",q)
print("Product =",r)
print("Quotient =", s)
teachcomputerscience.co
m
Procedures
Similar to functions, procedures are subprograms defined
outside the main program. The difference between procedures
and functions is procedures do not return a value, but functions
return a value.
An example of a Python program that uses a procedure to print
text entered by the user several times is given below.
#Procedure definition
def printProcedure(text, num):
for i in range(0,num):
print(text)
teachcomputerscience.co
m
Global and local variables
Variables and constants declared inside a function or procedure
are local to that subroutine. Consider the example program
given below that shows how a variable of the same name in a
function and main program behaves.
#procedure definition
def procA():
myString=“procedure A"
print("Executing statements in", myString)
def procB():
myString="procedure B"
print("Executing statements in", myString)
#main program
myString="main program“
print("Executing statements in", myString)
procA()
print("Executing statements in", myString)
procB()
print("Executing statements in", myString)
Executing this program, the output is,
teachcomputerscience.co
m