Chapter – 7 : Functions in Python
Summary (A Quick Recap)
• In programming, functions are used to achieve modularity and reusability.
• Function can be defined as a named group of instructions that are executed when the function is invoked or called by its name. Programmers
can write their own functions known as user defined functions.
• The Python interpreter has a number of functions built into it. These are the functions that are frequently used in a Python program. Such functions are known as built-in functions.
• An argument is a value passed to the function during function call which is received in a parameter defined in function header.
• Python allows assigning a default value to the parameter.
• A function returns value(s) to the calling function using return statement.
• Multiple values in Python are returned through a Tuple.
• Flow of execution can be defined as the order in which the statements in a program are executed.
• The part of the program where a variable is accessible is defined as the scope of the variable.
• A variable that is defined outside any particular function or block is known as a global variable. It can be accessed anywhere in the program.
• A variable that is defined inside any function or block is known as a local variable. It can be accessed only in the function or block where it is defined. It exists only till the function executes or remains active.
• The Python standard library is an extensive collection of functions and modules that help the programmer in the faster development of programs.
• A module is a Python file that contains definitions of multiple functions.
• A module can be imported in a program using import statement.
• Irrespective of the number of times a module is imported, it is loaded only once.
• To import specific functions in a program from a module, from statement can be used.
NCERT Book Exercise Solution – Ch 7 : Functions
1. Observe the following programs carefully, and identify the error:
a) Identify the error
def create (text, freq): for i in range (1, freq): print text create(5) #function call
Answer: – Error 1 : print text :- in this statement () is missing. Correct statement is print(text)
Error 2 : create(5) in this statement missing of one argument.
Correct statement is
b) Identify the error:
from math import sqrt,ceil def calc(): print cos(0) calc() #function call
Answer: – Missing of parentheses () in print statement.
Invalid call of cos() function. Function cos() not imported.
c) Identify the error
mynum = 9 def add9(): mynum = mynum + 9 print mynum add9() #function call
Answer: – Missing of parentheses () in print statement.
Correct Statement is
d) Identify the error
def findValue( vall = 1.1, val2, val3): final = (val2 + val3)/ vall print(final) findvalue() #function call
Answer: – In function header def findValue(), non-default argument can not be allowed after the default argument.
e) Identify the error
def greet(): return("Good morning") greet() = message #function call
Answer: – Function call statement is written wrong.
It should be
message = greet()
2. How is math.ceil(89.7) different from math.floor(89.7)?
math.floor(x) : Return the floor of x, the largest integer less than or equal to x.
3. Out of random() and randint(), which function should we use to generate random numbers between 1 and 5. Justify.
Answer: – randint() function of random module returns the number between start to end.
4. How is built-in function pow() function different from function math.pow() ? Explain with an example.
Answer: – pow() is an built-in function while math.pow() function defined under the math module. Both function is used to calculate x**y i.e. x raised to power y.
When x and y are integer and given as argument to pow() and math.pow() :
pow( x, y) function returns the value as integer type, while math.pow(x, y) function returns float type value.
5. Using an example show how a function in Python can return multiple values.
Answer: – A function in python can return multiple values in the form of tuple.
def calcsquare(x, y, z): t = x**2, y**2, z**2 return t sq = calcsquare(3,4,5) print(sq) a, b, c = calcsquare(3,4,5) print(a, b, c)
6. Differentiate between following with the help of an example:
a) Argument and Parameter
Answer: – Argument : Variable / Value / Expression which is written inside the parenthesis at the time of function call to passed to the function is called Argument.
Parameter: Variable written inside the parenthesis in the function header is called parameter.
An argument is a value passed to the function during the function call which is received in corresponding parameter defined in function header.
#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
b) Global and Local variable.
Answer: – A variable that has global scope is known as a global variable and a variable that has a local scope is known as a local variable.
Global Variable : In Python, a variable that is defined outside any function or any block is known as a global variable. It can be accessed in any functions defined onwards. Any change made to the global variable will impact all the functions in the program where that variable can be accessed.
Local Variable : A variable that is defined inside any function or a block is known as a local variable. It can be accessed only in the function or a block where it is defined. It exists only till the function executes.
Accessing num = 5
num reassigned = 10
Accessing num outside myfunc1 10
7. Does a function always return a value? Explain with an example.
Answer: – Generally a function return a value with the help of return statement. But in python, a function can return None value, if return statement is not used. Let’s understand with the help of example:
def fun1(): print("hello") def fun2(): return 20 #After calling fun1() print("After calling fun1()") print(fun1()) #After calling fun2() print("After calling fun2()") print(fun2())
After calling fun1()
After calling fun2()