B Sc Computer Science – VI Semester
Elective Papers - CS6PET01: Python and LateX
Module III – Functions
Basic in-build functions, User defined functions, Function Calls, Parameterized
function calls, Function returns, Recursive functions, Scope concepts - local, global
Functions
The process of dividing a computer program into separate independent blocks of code or separate
sub-problems with different names and specific functionalities is known as modular
programming.
In programming, the use of function is one of the means to achieve modularity and reusability.
Function can be defined as a named group of instructions that accomplish a specific task when it
is invoked. Once defined, a function can be called repeatedly from different places of the
program without writing all the codes of that function every time, or it can be called from inside
another function, by simply writing the name of the function and passing the required
parameters, if any. The programmer can define as many functions as desired while writing the
code.
Advantages of Function
The following are the advantages of using functions in a program:
• Increases readability, particularly for longer code as by using functions, the program is
better organised and easy to understand.
• Reduces code length as same code is not required to be written at multiple places in a
program. This also makes debugging easier.
• Increases reusability, as function can be called from another function or another program.
Thus, we can reuse or build upon already defined functions and avoid repetitions of
writing the same piece of code.
• Work can be easily divided among team members and completed in parallel.
USER DEFINED FUNCTIONS
Taking advantage of reusability feature of functions, there is a large number of functions already
available in Python under standard library. We can directly call these functions in our program
without defining them. However, in addition to the standard library functions, we can define our
own functions while writing the program. Such functions are called user
VIJAYA GOPINATH M
,B Sc CS CS6PET01: Python and LateX Page |2
defined functions. Thus, a function defined to achieve some task as per the programmer's
requirement is called a user defined function.
Creating User Defined Function
A function definition begins with def (short for define).
The syntax for creating a user defined function is
as follows:
• The items enclosed in "[ ]" are called parameters and they are optional. Hence, a function
may or may not have parameters. Also, a function may or may not return a value.
• Function header always ends with a colon (:).
• Function name should be unique. Rules for naming identifiers also applies for function
naming.
• The statements outside the function indentation are not considered as part of the function.
Write a user defined function to add 2 numbers and display their sum.
#Function to add two numbers
#The requirements are listed below:
#1. We need to accept 2 numbers from the user.
#2. Calculate their sum
#3. Display the sum.
#function definition
def addnum():
fnum = int(input("Enter first number: "))
snum = int(input("Enter second number: "))
sum = fnum + snum
print("The sum of ",fnum,"and ",snum,"is ",sum)
#function call
addnum()
Output:
Enter first number: 5
Enter second number: 6
The sum of 5 and 6 is 11
In order to execute the function addnum(), we need to call it. The function can be called in the
program by writing function name followed by () as shown in the last line of program.
VIJAYA GOPINATH M
, B Sc CS CS6PET01: Python and LateX Page |3
Arguments and Parameters
In the above example, the numbers were accepted from the user within the function itself, but it
is also possible for a user defined function to receive values at the time of being called. An
argument is a value passed to the function during the function call which is received in
corresponding parameter defined in function header.
Write a program using a user defined function that displays sum of first n natural numbers,
where n is passed as an argument.
#Program to find the sum of first n natural numbers
#The requirements are:
#1. n be passed as an argument
#2. Calculate sum of first n natural numbers
#3. Display the sum
#function header
def sumSquares(n): #n is the parameter
sum = 0
for i in range(1,n+1):
sum = sum + i
print("The sum of first",n,"natural numbers is: ",sum)
num = int(input("Enter the value for n: "))
#num is an argument referring to the value input by the user
sumSquares(num) #function call
Note : Since both num and n are referring to the same value, they are bound to have the same
identity. We can use the id() function to find the identity of the object that the argument and
parameter are referring to. Let us understand this with the help of the following example.
Write a program using user defined function that accepts an integer and increments the value by
5. Also display the id of argument (before function call), id of parameter before increment
and after increment.
#Function to add 5 to a user input number
#The requirements are listed below:
#1. Display the id()of argument before function call.
#2. The function should have one parameter to accept the argument
#3. Display the value and id() of the parameter.
#4. Add 5 to the parameter
#5. Display the new value and id()of the parameter to check
#whether the parameter is assigned a new memory location or
#not.
def incrValue(num):
#id of Num before increment
print("Parameter num has value:",num,"\nid =",id(num))
num = num + 5
#id of Num after increment
VIJAYA GOPINATH M
Elective Papers - CS6PET01: Python and LateX
Module III – Functions
Basic in-build functions, User defined functions, Function Calls, Parameterized
function calls, Function returns, Recursive functions, Scope concepts - local, global
Functions
The process of dividing a computer program into separate independent blocks of code or separate
sub-problems with different names and specific functionalities is known as modular
programming.
In programming, the use of function is one of the means to achieve modularity and reusability.
Function can be defined as a named group of instructions that accomplish a specific task when it
is invoked. Once defined, a function can be called repeatedly from different places of the
program without writing all the codes of that function every time, or it can be called from inside
another function, by simply writing the name of the function and passing the required
parameters, if any. The programmer can define as many functions as desired while writing the
code.
Advantages of Function
The following are the advantages of using functions in a program:
• Increases readability, particularly for longer code as by using functions, the program is
better organised and easy to understand.
• Reduces code length as same code is not required to be written at multiple places in a
program. This also makes debugging easier.
• Increases reusability, as function can be called from another function or another program.
Thus, we can reuse or build upon already defined functions and avoid repetitions of
writing the same piece of code.
• Work can be easily divided among team members and completed in parallel.
USER DEFINED FUNCTIONS
Taking advantage of reusability feature of functions, there is a large number of functions already
available in Python under standard library. We can directly call these functions in our program
without defining them. However, in addition to the standard library functions, we can define our
own functions while writing the program. Such functions are called user
VIJAYA GOPINATH M
,B Sc CS CS6PET01: Python and LateX Page |2
defined functions. Thus, a function defined to achieve some task as per the programmer's
requirement is called a user defined function.
Creating User Defined Function
A function definition begins with def (short for define).
The syntax for creating a user defined function is
as follows:
• The items enclosed in "[ ]" are called parameters and they are optional. Hence, a function
may or may not have parameters. Also, a function may or may not return a value.
• Function header always ends with a colon (:).
• Function name should be unique. Rules for naming identifiers also applies for function
naming.
• The statements outside the function indentation are not considered as part of the function.
Write a user defined function to add 2 numbers and display their sum.
#Function to add two numbers
#The requirements are listed below:
#1. We need to accept 2 numbers from the user.
#2. Calculate their sum
#3. Display the sum.
#function definition
def addnum():
fnum = int(input("Enter first number: "))
snum = int(input("Enter second number: "))
sum = fnum + snum
print("The sum of ",fnum,"and ",snum,"is ",sum)
#function call
addnum()
Output:
Enter first number: 5
Enter second number: 6
The sum of 5 and 6 is 11
In order to execute the function addnum(), we need to call it. The function can be called in the
program by writing function name followed by () as shown in the last line of program.
VIJAYA GOPINATH M
, B Sc CS CS6PET01: Python and LateX Page |3
Arguments and Parameters
In the above example, the numbers were accepted from the user within the function itself, but it
is also possible for a user defined function to receive values at the time of being called. An
argument is a value passed to the function during the function call which is received in
corresponding parameter defined in function header.
Write a program using a user defined function that displays sum of first n natural numbers,
where n is passed as an argument.
#Program to find the sum of first n natural numbers
#The requirements are:
#1. n be passed as an argument
#2. Calculate sum of first n natural numbers
#3. Display the sum
#function header
def sumSquares(n): #n is the parameter
sum = 0
for i in range(1,n+1):
sum = sum + i
print("The sum of first",n,"natural numbers is: ",sum)
num = int(input("Enter the value for n: "))
#num is an argument referring to the value input by the user
sumSquares(num) #function call
Note : Since both num and n are referring to the same value, they are bound to have the same
identity. We can use the id() function to find the identity of the object that the argument and
parameter are referring to. Let us understand this with the help of the following example.
Write a program using user defined function that accepts an integer and increments the value by
5. Also display the id of argument (before function call), id of parameter before increment
and after increment.
#Function to add 5 to a user input number
#The requirements are listed below:
#1. Display the id()of argument before function call.
#2. The function should have one parameter to accept the argument
#3. Display the value and id() of the parameter.
#4. Add 5 to the parameter
#5. Display the new value and id()of the parameter to check
#whether the parameter is assigned a new memory location or
#not.
def incrValue(num):
#id of Num before increment
print("Parameter num has value:",num,"\nid =",id(num))
num = num + 5
#id of Num after increment
VIJAYA GOPINATH M