0% found this document useful (0 votes)
9 views10 pages

12 Q and A Function

The document provides a comprehensive overview of functions in Python, explaining their definition, creation, parts, and types. It covers the importance of functions for code organization and efficiency, as well as how to call them and the role of comments in programming. Additionally, it discusses arguments and variables, including local and global variables, and illustrates various examples and output scenarios related to function usage.

Uploaded by

mukul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

12 Q and A Function

The document provides a comprehensive overview of functions in Python, explaining their definition, creation, parts, and types. It covers the importance of functions for code organization and efficiency, as well as how to call them and the role of comments in programming. Additionally, it discusses arguments and variables, including local and global variables, and illustrates various examples and output scenarios related to function usage.

Uploaded by

mukul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1. What is a function?

Ans.:

 A function is a set of instructions or subprograms that are used to fulfil the user’s need.

 In other words, a function is a bunch of code which performs a specific task.

 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.

2. Why do programmers need functions in python programming?

Ans.:

 To make the program easy

 Divide the large program into a small block of codes

 Reduce the lines of code

 Easy to update

3. How to create a function in python? Explain in detail.

Ans.: To create a function in python follow the given steps:

 Start the code with def followed by function name and supply input through the parameters.

 Write the set of statements to be executed in the program.

 Call the function to invoke or execute the statements.

4. What are the parts of functions? Explain with a suitable example.

Ans.: The arts of functions are as follows:

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

Header parameters, and ends with a colon (:)

Parameters Variablessupplied in brackets of the function header

Block of statements/instructions that define the action performed by the function,


Function Body
indentation must be followed

Indentation White space at the beginning of every statement with the same block

Function
writing function name including parameter values
Calling

5. How to call a function? Write steps.

Ans: Calling a function is an easy task. Here are the steps:

 Write the function name.

 Supply the values of the parameters, if any.

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 ”’.

7. Explain the physical line structure of a program. Illustrate with an example.

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:

>>> name=’Chapter 3 Working with functions’

>>> print(name)

Chapter 3 Working with functions

The above statement is same as:

>>> name=’Chapter 3 Working with functions’; print(name)

8. Illustrate the flow of execution in the function call statement.

Ans.:

 A function in the python program is called by a function call statement

 To call a function, write the function name followed by parameter values in brackets

 A block of statements executed in the execution frame

 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

 Python follows top to bottom approach for executing program

 Comments are ignored in execution

 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

9. Write and explain the types of functions supported by python.

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.

 User-Defined Functions: Function created by the programmer


In the next section of Working with Functions Class 12 questions and answers, we are going to discuss
some questions based on topics arguments, and parameters for Working with Functions Class 12.

10. Write the ways of import module in the python program.

Ans.:

 The module can be imported in two ways

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

11. Differentiate between parameters and arguments.

Ans.:

Parameters Arguments

These are specified during the function definition. Values passed during the function call.

They are also known as actual


They are also known as formal parameters.
parameters.

The values passed as parameters are local variables


Every argument is assigned to a
and are assigned values of the arguments during the
parameter when the function is defined.
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.

Ans.:Python supports four argument types:

1. Positional Arguments: Arguments passed to a function in correct positional order, no. of


arguments must match with no. of parameters required.
2. Default Arguments: Assign a default value to a certain parameter, it is used when the user knows
the value of the parameter, default values are specified in the function header. It is optional in the
function call statement. If not provided in the function call statement then the default value is
considered. Default arguments must be provided from right to left.

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.

2. Keyword arguments should be taken from the required arguments preferably.

3. Value of the argument can’t be specified more than once.

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():

print(‘Python, let\’s fun with functions’)

Fun1()

Ans.:

Python, let’s fun with functions

Explanation: Fun1 is a user-defined function having 1 statement with \ to print ‘ in output.

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.

4.def fun1(x, y):

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)

You might also like