MORE ABOUT FUNCTION
1. What is the difference between Actual argument and Format argument? Explain with example
Ans : Argument: - The values being passed through a function call statement to function definition are called argument
(or actual parameters or actual argument).
Parameters: - The values received in the function definition header are called parameter (or formal parameters or
formal arguments).
For example:-
def calcSum ( x , y ):
s=x+y
return s
a ,b = 5 , 6
print (calcSum ( a , b ))
Here a , b are “Actual arguments” X and y are formal argument
2. What is the use of global keyword used in a function with the help of suitable example
Ans : in python Global keyword allows the programmer to declare global variable inside function or in the local
context. A variable declared inside a function is by default local and a variable declared outside function is by default
global. The keyword global is written before variable inside function to use its global value. Now the value of global
variable can be changed inside function for example :
a=30 // GLOBAL VARABLE
def call (x):
global a // HERE WITH GLOBAL KEUWORD A IS REFERS A GLOBAL VARAIBLE
a=a+2 //value of global varable changed now
print(“inside function ”,a)
print(“before calling function ”,a)
add()
print(“outside function after calling function”,a)
Ans : before calling function 30
inside function 32
outside function after calling function 32
3. Is return statement is compulsory in python function? How many values a return statement can
return at a time ? If return statement is not used inside a function, what will be the return value of the
function?
Ans : return statement is optional only when the function is void or it doesn’t return any values , otherwise if function
that return a value must have at least one return statement . Return statement can return any number of values at a
time. if no return statement is there in function then function will return NONE
4. Compare and comment on the following statement?
return
return val
Ans : first return statement does not return any value rather t return the control back to calling statement with
empty value that is None While second statement returning the value back to the calling function
5. Ram , python programmer ,is working on a project which require him to define a function interest.
He define it as def interest (Principal ,Rate= 0.15,time) :
But this code is not working, help him to identify the error in the above function and also correct it
Ans : according to rule when we pass default values to argument in function definition then it should be
from right to left, so correct way is
def interest (Principal ,Rate= 0.15,time =0) :
6. Trace the flow of execution of the following program
1 def increment (x)
2. x=x+1
3. x=3
4. print (x)
5. increment (x)
6 print(x)
Ans : 1-3-4-5-1-2-6
b)
The sequence of execution is:
1 -> 4 -> 9 -> 10 -> 11 -> 1 -> 2 -> 12 -> 13 -> 14 -> 4 -> 5 -> 6 -> 14 -> 15
7. What is the difference between local and global variable explain with example
LOCAL VARIABLE GLOBAL VARIABLE
It is a variable which is declared with in a function or It is a variable which is declared outside all function or a
within a block block
It is accessible only within the function of block in which It is accessible throughout the whole program
it is declared
def cube(n):
c=n*n*n // n and c is local variable
return c
x=10 // x is global variable
print(cube(x)
8. What is positional argument? Explain with example
Ans : When the functions call statement must match the number and Order of arguments as define in the functions
definition this is called the position argument matching. They are also called required or mandatory argument as no
value can be skipped from the function call statement or you cannot change the order of arguments
def calcSum ( x , y ):
s=x+y
return s
a=5
b=6
print (calcSum ( a , b ))
like here calcsum() required two argument so function calling statement must have two argument. Now x will get
value of a and y will get b.
9. What is default argument? Explain with example its benefit and rule of passing default argument?
Ans : A parameter having defined value in the function header is known as a default parameter.
For example:- def interest( principal , time , rate = 10 ) :
Calling function : interest (1000,4,5)
interest(1000,4)
That means the default values (values assigned in function header) are considered only if no value is provided for that
parameter in the function call statement.
According to rule- when we pass default values to argument in function definition then it should be from
right to left Default arguments are useful in situations where some parameters always have same value.
You can understand more by seeing below examples:-
def interest ( prin , time , rate = 0.10) # legal
def interest ( prin , time = 2 , rate) # illegal ( default parameter before required parameter )
def interest ( prin = 2000 ,time = 2 ,rate) # illegal# (same reason as above)
def interest ( prin , time = 2 , rate = 0.10 ) # legal
def interest ( prin = 2000 , time = 2 , rate = 0.10) # legal
10. What is keyword or named arguments? explain
Ans - Keyword arguments are the named arguments with assigned values being passed in the function call statement.
Keyword argument allows us to write any argument in any order with the help of an argument name.
Advantages:
No need to remember the order of the arguments.
No need to give value for all, specify only those which you want (if default value is mentioned for all).
Keyword arguments are the named arguments with assigned values being passed in the function call statement.
For example:-
def interest ( prin , time , rate ) :
return prin * time * rate
print (interest ( prin = 2000 , time = 2 , rate 0.10 ))
print (interest ( time = 4 , prin = 2600 , rate = 0.09 ))
print (interest ( time = 2 , rate = 0.12 , prin = 2000 ))
RULES FOR PASSING DEFAULT ARGUMENT
An argument list must first contain positional argument followed by any keyword argument
Keyword argument must be taken from psitonal argument
OUTPUT QUESTION BASED ON FUNCTIONS
1. Find the output of the following
def Diff(N1,N2):
ifN1>N2:
return N1-N2
else:
returnN2-N1
NUM=[10,23,14,54,32]
for CNT in range (4,0,-1):
A=NUM[CNT]
B=NUM[CNT-1]
print(Diff(A,B),'#',end='')
Ans : 22 # 40 # 9 # 13 #
2. Find the output of the following
v=25
def fun(ch):
v=50
print(v,end=ch)
v*=2
print(v, end=ch)
print(v, end="*")
fun("!")
print(v)
Ans: 25*50!100!25
3. Find the output of the following
defAlter(A=12,B=10):
A=A*B
B=A/B
print A,"@",B
return B
X=200
Y=300
X=Alter(X,Y)
print (X,"$",Y)
Y=Alter(Y)
print (X,"$",Y)
4. Find the output of the following
a=30
def call (x):
global a
if x%2==0:
x=x+a
else:
x=x-a
return(x)
print(call(67), end="#")
print(call(40),end="#")
Ans :37#70#
5. Find the output of the following
val = 100
def display(N):
global val
val = 50
if N%14==0:
val = val + N
else:
val = val - N
print(val, end="@")
display(40)
print(val)
Ans : 100@10
6. Find the output of the following
R=0
def change(A,B):
global R
A+=B
R+=3
print(A,B,sep =”#”, end="")
change(10,2)
print(R,end='%')
change(B=3,A=2)
Ans: 12# 2
3%5# 3
7. Find the output of the following
def div(L,n):
for i in range(0,n):
if L[i]%5==0:
L[i]+=5
else:
L[i]=L[i]//2
t=[45,20,23,54,5]
div(t,len(t))
for i in t:
print(i,end="#")
Ans : 50#25#11#27#10#
8. Find the output of the following
def change(m,n=10):
global x
x+=m
n+=x
m=n+x
print(m,n,x)
x=20
change(10)
change(20)
Ans : 70 40 30
110 60 50
9. Find the output of the following
def calc(u):
if u%2==0:
return u+10
else:
return u+2
def pattern (M,B=2):
for CNT in range(0,B):
print(calc(CNT),M,end="")
print()
pattern("*")
pattern("#",4)
pattern("@",3)
Ans : 10 *3 *
10 #3 #12 #5 #
10 @3 @12 @
10. Find the output of the following
L1 =[]
def display(N):
for K in N:
if K % 2 ==0:
L1.append(K//2)
else:
L1.append(K*2)
L = [11,22,33,45,55,66]
print(L)
display(L)
print(L1)
Ans : [11, 22, 33, 45, 55, 66]
[22, 11, 66, 90, 110, 33]
MCQ
11. What is the default return value for the function which does not return any value
a) int b) None c) double d) null
Ans: b) None
12. Pick one of the following statement s to correctly complete the function body in the given code \
def f(num) :
# missing function body
print(f(s))
a) return ‘num’ b) print(num) c) print(‘num) d) return num
Ans : d) return num
13. Which of the following statements is not true for parameter passing to function?
a. You can pass positional argument in any order
b. You can pass keyword argument in any order
c. You can call a function with positional and keywords argument
d. Positional argument must be before keyword arguments in a functional call statements
Ans : a) You can pass positional argument in any order
14. Which of the following is not correct in context of positional and default argument in python functionsl
a. Default argument must occur to the right of the positional parameters
b. positional parameters must occur to the right of the Default argument
c. positional parameters must occur to the left of the Default argument
d. all parameters to the right of default parameters must also have default values
Ans : b) positional parameters must occur to the right of the Default argument
15. which of the following is not correct in context of scope of variables
a. global keyword is used to change value of a global variable in local scope
b. local keyword is used to change value of a local variable in global scope
c. global variables can be accessed without using the global keyword in a local scope
d. local variables cannot be used outside its scope.
Ans : b) local keyword is used to change value of a local variable in global scope
16. Which of the following function call can be used to invoke a function geiven below
def test (a,b,c,d):
a) test(1,2,3,4) b) test(a=1,2,3,4) c) test(a=1,b=2,c=3,4) d) test(a=1,b=2,c=3,d=4)
Ans : a) and d)
17. what is the data type of variable a
def test (a,b,c,d):
return a,b,c,d
x,y,z,w = 1,2,3,4
res = test(x,y,z,w)
print(res)
a) int b) list c) tuple d) Error
Ans : c) tuple
18. for a function header as follows : def calc(x,y=20):
which of the following function calls will give an error
a) calc(15,25) b) calc(x=15,y=25) c) calc(y=25) d) calc(x=25)
Ans : c) calc(y=25)
19. what is the order of resolving scope of a name in a python program
(L=local namespace, E: enclosing namespace, B: built-in-namespace, G : Global namespace
a) B G E L b) L E G B c) G E B L d) L B E G
Ans : b) L E G B