CBSE CS

cbse cs logo

Home

Questions and Anwers on Functions in Python

1. What is a function in python?
Ans. A function is a block of organised, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing.
Python gives many built-in functions like print(), etc. but you can also create your own functions. These functions are called user-defined functions.
You can pass data, known as parameters, into a function. A function can return data as a result.

2. What is the role of indentation in python function.
Ans. Indentation (Space) in Python Functions
Python functions don’t have any explicit begin or end like curly braces to indicate the start and stop for the
function, they have to rely on this indentation. It is also necessary that while declaring indentation, you have to
maintain the same indent for the rest of your code.

3. How to define user define function?
Ans. Defining a Function
You can define functions to provide the required functionality. Here are simple rules to define a function in Python.
• Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).
• Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.
• The first statement of a function can be an optional statement – the documentation string of the function or docstring.
• The code block within every function starts with a colon (:) and is indented.
• The statement return [expression] exits a function, optionally passing back an expression to the caller.
A return statement with no arguments is the same as return None.
Syntax
def functionname( parameters ):           “function_docstring”
       function_suite
        return [expression]

4. How to call a function in python ? Explain with an example.
Ans. Calling a Function: To call a function, use the function name followed by parenthesis.
Example:
def fun():
     print(“Hello”)
fun() # function calling

output
Hello

 

5. Give the output

def prn():
       print(“****”)
       print(“hello”)
prn()
print(“hi”)
prn()
print(“bye”)
prn()

Ans.

hello
****
hi
****
bye
****

6. What is Parameter in python function?
Ans. Information can be passed to functions as parameter. Parameters are specified after the function name, inside the
parentheses. You can add as many parameters as you want, just separate them with a comma.

 

7. What is the difference between parameter and argument?
Ans. Parameters are temporary variable names within functions.
The argument can be thought of as the value that is assigned to that temporary variable.
For instance, lets consider the following simple function to calculate sum of two numbers.
def sum(a,b):
            return a+b
sum(10,20)
a,b here are the parameter for the function ‘sum’.
Arguments are used in procedure calls, i.e. the values passed to the function at run-time.10,20 are the arguments
for the function sum

 

8. Differentiate between actual parameter(s) and a formal parameter(s) with a suitable example for each. 
Ans. The list of identifiers used in a function call is called actual parameter(s) whereas the list of parameters used in
the function definition is called formal parameter(s).
Actual parameter may be value/variable or expression. Formal parameter is an identifier.
Example:
def area(side): # line 1
      return side*side;
print(area(5)) # line 2
In line 1, side is the formal parameter and in line 2, while invoking area() function, the value 5 is the actual parameter.
A formal parameter, i.e. a parameter, is in the function definition. An actual parameter, i.e. an argument, is in a function call.


9. What are the different types of Functions in python?
Ans. There are two basic types of functions: built-in functions and user defined functions.
(i) The built-in functions are part of the Python language; for instance dir() , len() , or abs() .
(ii) The user defined functions are functions created with the def keyword.

 

10. Explain the types of function arguments in python
Ans. Function Arguments: You can call a function by using the following types of formal arguments:
• Required arguments • Keyword arguments
• Default arguments • Variable number of arguments
• Arguments are passed as a dictionary.
(i) Required arguments: Required arguments are the arguments passed to a function in correct positional
order. Here, the number of arguments in the function call should match exactly with the function definition.
def show(x,y):
     print(“Name is “,x)
     print(“Age is “,y)
show(“Raj”,18)
Output:
Name is Raj
Age is 18

 

(ii) Keyword arguments: Keyword arguments are related to the function calls. When you use keyword
arguments in a function call, the caller identifies the arguments by the parameter name. This allows you to
place argument out of order because the Python interpreter is able to use the keywords provided to match
the values with parameters.
def show(x,y):
print(“Name is “,x)
print(“Age is “,y)
show(y=18,x=”Raj”)
OUTPUT
Name is Raj
Age is 18

(iii) Default arguments: A default argument is an argument that assumes a default value if a value is not
provided in the function call for that argument. The following example gives an idea on default arguments.
def sum(a,b=10):
print(‘sum is’,a+b)
sum(15,25)
sum(15)
OUTPUT
sum is 40
sum is 25
The rule for placing the default arguments in the argument list : Once default value is used for an
argument in function definition, all subsequent arguments to it must have default value. It can also be stated
as default arguments are assigned from right to left.
(iv) Variable Number of Arguments: In cases where you don’t know the exact number of arguments that you
want to pass to a function, you can use the following syntax with *args:
The variable stored in *args are represented in the form of tuple.
def change(*a):
for i in a:
       print(i)

print(a)

 

change(10,5)
change(15,20,25)
OUTPUT
10
5
(10, 5)
15
20
25
(15, 20, 25)

 

11. How does a function return Value?
Ans. If you want to continue to work with the result of your function and try out some operations on it, you will need to use the return statement to actually return a value.
Example:
def square(x):
     return x * x
a=5
print(square(3))
print(square(a))
OUTPUT
9
25

 

12. Can a function return multiple values? Explain with an example.
Ans. Yes, the return values should be a comma-separated list of values and Python then constructs a tuple and returns this to the caller,
Example:
def change(a,b,c,d):
         m=max(a,b,c,d)
         n=min(a,b,c,d)
         s=a+b+c+d
         return n,m,s
g,h,i=change(2,5,7,20)
print (g,h,i)
OUTPUT
2 20 34

14. Give the output
(i)
print(“hi”)
def abc():
       print(“hello”)
print(“bye”)
abc()
(ii)
def abc():
        print(“hello”)
print(“hi”)
print(“bye”)
abc()

(iii)
print(“hi”)
print(“bye”)
def abc():
       print(“hello”)
abc()
Ans. OUTPUT
(i), (ii), (iii)
hi
bye
hello

15. Give the output 
def cal():
       i = 9
       while i> 1:
       if i%2==0:
             x = i%2
             i  = i-1
       else:
              i = i-2
              x = i
        print (x**2)
cal()
Ans. 49
25
9
1

16. Give the output
def sum(a,b):
           print(“sum is “,a+b)
x=5
y=10
sum(x,y)
sum(20,40)

Ans. Output
sum is 15
sum is 60

 

17. Give the output of the following program
def check(a):
         for i in range(len(a)):
                a[i]=a[i]+5
                return a
b=[1,2,3,4]
c=check(b)
print(c)
Ans. OUTPUT
[6, 7, 8, 9]

18. Give the output
def sum(*a):
       s=0
       for i in a:
             s=s+i
             print(s)
sum(2,3)
sum(2,6,3)
sum(1,2,3,4)
Ans. OUTPUT
5
11
10

Click the button to see more MCQ

error: Content is protected !!