Worksheet 3
Worksheet 3
Topic: Functions
1. Using an example show how a function in Python can return multiple values.
2. Differentiate between following with the help of an example:
a) Argument and Parameter b) Global and Local variable
3. Does a function always return a value? Explain with an example.
4. Write a program consisting of four functions (fruitful) with names and order of appearance as
Add , Sub ,Prod and Div.
As the name suggests, each function calculates the sum, difference , product and quotient of two
numbers. Call the functions in reverse order to display the results.
5 . Write the output of the given program.
def add(a,b):
print("The sum is ",a+b)
def sub(a,b):
print("The diff is ",a-b)
def prod(a,b):
print("The product is ",a*b)
def div(a,b):
if b == 0:
print ("Division by Zero Error")
else:
print("The quotient is ",a/b)
a=div(12,2)
print(a)
prod(11,3)
sub(34,78)
div(20,0)
ANSWERS
1. floor(): The function 'floor(x)' in Python returns the largest integer not greater than x. i.e. the
integer part from the variable.
ceil(): The function 'ceil(x)' in Python returns the smallest integer not less than x i.e., the next integer
on the RHS of the number line.
Hence, 'math.ceil(89.7)' will return 90 whereas 'math.floor(89.7)' will return 89.
2. 'randint(a,b)' function will return a random integer within the given range as parameters.
'random()' function generates a random floating-point value in the range (0,1).
Therefore, to generate a random number between 1 and 5 we have to use the randint(1,5) function.
3. The built-in pow(x,y [,z]) function has an optional third parameter as modulo to compute x raised
to the power of y and optionally do a modulo (% z) on the result. It is same as the mathematical
operation: (x ^ y) % z. Whereas, math. pow() function does not have modulo functionality.