12 Q and A Function
12 Q and A Function
Ans.:
A function is a set of instructions or subprograms that are used to fulfil the user’s need.
A function is used to do a specific task and divide the large program into smaller blocks.
A function is a small unit of a program that processes the data and often returns a value.
Ans.:
Easy to update
Start the code with def followed by function name and supply input through the parameters.
1. Function header: It starts with def followed by the function name and required parameters.
Parameters are the input variables written into brackets. These parameters are supplied in the
function calling statement.
2. Function Body: This is the main part of a program. It contains the main block of the program. The
set of instructions such as calculations, logical comparisons etc is written here in this part. It ends
with a return statement. Indentation is very important for the function body.
3. Function calling statement: This is the final part of a function. It invokes the function and returns
the output as instructed into the function body.
Part Description
Function Always starts with the “def” keyword followed by the function name and its
Part Description
Indentation White space at the beginning of every statement with the same block
Function
writing function name including parameter values
Calling
6. What are the comments? What are the role comments in the program? How to write single-line
comments and multi-line comments?
Ans.: Comments are the additional explanatory text written in the function to provide some description or
explanation about the statement or block of code.
Comments play an important role in understanding a program. It provides a descriptive explanation of the
written statements. It makes the intention very clear for what the statement is used or what process is
going to be done.
The single-line comment is written by # followed by the text. The multiline comments start with ”’ and are
followed by the text then ends with ”’.
Ans.: The physical line structure of a program contains the lines of codes. Physical lines are the lines which
you can see on the screen in a python program.
In other words, a physical line contains a set of characters that ends with an EOL character. The EOL
character is newline generally ‘\n’ in python.
Example:
>>> print(name)
Ans.:
To call a function, write the function name followed by parameter values in brackets
When a function is called, an execution frame is created and controls the transfer
Within the execution frame, the statements written in the function body are executed and return a
value or execute the last statement
If python notices function definition with a def statement it just executes the function header line
and skips all statements in the function body these statements execute when a function will be
called
Ans.:
Built-in Functions: Pre-defined functions of python such as len(), type(), input() etc.
Functions defined in modules: Functions defined in particular modules, can be used when the
module is imported. A module is a container of functions, variables, constants, and classes in a
separate file which can be reused.
Ans.:
o import statement: Used to import the entire module. EX. import math
o from statement : import all functions or the selected one. EX. from random import randint
Ans.:
Parameters Arguments
These are specified during the function definition. Values passed during the function call.
These variables help in the complete execution of the These variables are passed to the
function. function for execution.
The values contained by these parameters can only The arguments are accessible throughout
be accessed from function return statements or if the the program depending upon the scope
scope of these parameters is made global. of the variable assigned.
12. What are the arguments supported by python? Explain each of them with a suitable example.
3. Key Word Arguments: Keyword arguments are the named arguments with assigned values being
passed in the function call statement, the user can combine any type of argument.
4. Variable Length Arguments: It allows the user to pass as many arguments as required in the
program. Variable-length arguments are defined with the * symbol.
13. What is the local variable and global variable? Explain with an example.
Ans.:
Global Variable: A variable that is declared in top-level statements is called a global variable. To access the
value of a global variable user need to write a global keyword in front of the variable in a function.
Local Variable: A name declared in a specific function body is called a local variable.
In the next section of Working with functions Class 12 questions, I will cover some output and error-based
questions. Here we go!
14. What are the rules for combining three types of arguments in a Python function?
Ans.: The following rules need to be followed for combining three types of arguments in a python
function.
1. An argument list must contain positional arguments followed by any keyword argument.
Ex.:
cal_discount(amt=500,rate=10) or cal_discount(rate=10,amt=500)
In the next section of Working with Functions Class 12 we are going through the output questions.
Output Questions Working with functions Class 12
1. def Fun1():
Fun1()
Ans.:
2.def add(i):
if(i*3%2==0):
i*=i
else:
i*=4
return i
a=add(10)
print(a)
b=add(5)
print(b)
Ans.:
100
20
Explanation: In the first function call statement 10 is passed as a value, so i*3%2=10*3%2=30%2=0, hence
prints square of 10 i.e. 100. In the second function call statement 5 is passed as a value, so
i*3%2=5*3%2=15%2=1, hence else block will be executed, and its i=i*4 t*4=20.
3. import math
def area(r):
return math.pi*r*r
a=int(area(10))
print(a)
Ans.:314
Explanation: In function, call value is passed 10. Hence the calculation will be 3.14*100=314.
x=x+y
y=x–y
x=x–y
print(‘a =’,x)
print(‘b =’,y)
a=5
b=3
fun1(a,b)
Ans.:
a=3
b=5
Explanation:
a=5
b=3
fun1(5,3)
x=5+3=8
y=8-3=5
x=8-5=3
a=3
b=5
4. def div5(n):
if n%5==0:
return n*5
else:
return n+5
def output(m=5):
for i in range(0,m):
print(div5(i),’@’,end=” “)
print(”)
output(7)
output()
output(3)
Ans.:
0 @ 6 @ 7 @ 8 @ 9 @ 25 @ 11 @
0@6@7@8@9@
0@6@7@
Explanation: In first calling for output(7) loop executes 7 times. and returns 5 as 25 and the rest will be
incremented by 5 hence the first line is generated.
In the second calling function no value is passed so it takes the default argument and the loop terminates
when the of i is 5.
In the third calling, the function value is passed as 3 so there is no value which a multiple of 5.
5.def sum(*n):
total=0
for i in n:
total+=i
print(‘Sum=’, total)
sum()
sum(5)
sum(10,20,30)
6.def func(b):
global x
print(‘Global x=’, x)
y=x+b
x=7
z=x–b
print(‘Local x = ‘,x)
print(‘y = ‘,y)
print(‘z = ‘,z)
x=5
func(10)
7. def func(x,y=100):
temp = x + y
x += temp
if(y!=200):
print(temp,x,x)
a=20
b=10
func(b)
print(a,b)
func(a,b)
print(a,b)
8. def get(x,y,z):
x+=y
y-=1
z*=(x-y)
print(x,’#’,y,’#’,z)
def put(z,y,x):
x*=y
y+=1
z*=(x+y)
print(x,’$’,y,’$’,z)
a=10
b=20
c=5
put(a,c,b)
get(b,c,a)
put(a,b,c)
get(a,c,b)