0% found this document useful (0 votes)
7 views8 pages

Function 1.1-1

Uploaded by

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

Function 1.1-1

Uploaded by

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

2023-2024 Session Class 12 Computer Science

Function in Python
Worksheet No. 1
S P SHARMA SIR Lecturer Computer Science

1. What is function?
 A function is a set of related statements that performs a specific task. Functions improves a
program's clarity and readability and makes programming more efficient by reducing code
duplication and breaking down complex task into more manageable small parts.
 A function is a block of code that performs a specific task.
 A function is a block of code which only runs when it is called.
 A function is a block of organized, reusable code that is used to perform a single, related action.
 A function is simply a “part” of code that you can use over and over again, rather than writing it
out multiple times.
 Functions enable programmers to break down or decompose a problem into smaller parts, each
of which performs a particular task.
 Functions also known as routines, sub-routines, methods, procedures and sub-programs.
2. Explain different types of functions.
Types of Function:
1. Built - In or Library Functions
2. Functions in Module
3. User - Defined Functions
3. Write the name of five built – in functions.
Built -in functions are the predefined functions that are already available in Python.
input(), print(), int(), float(), eval(), max() and min(), abs(), type(), len(), round(), range()
4. What is Module?
 When a program become more lengthy and complex, there arises a need for the tasks to be spilt
into smaller segments called modules.
 When we break a program into modules, each module should contain functions that perform
related tasks.
 A module in Python is a file that contains a collection of related functions.
5. Write the name of five functions of random modules.
randrange(), randint(), random(), seed(), choice(), shuffle(), uniform()
6. Write the name of five functions of math modules.
ceil(), floor(), pow(), sqrt(), fabs(), cos(), sin(), tan()
7. Write the name of three functions of statistics modules.
mean(), mode(), median(), stdev(), variance()
8. What is user defined function? Explain with example.
A user defined function is created or defined by the def keyword, followed by the function name,
parentheses () and colon (:)
Syntax of Python Function
def function_name (optional parameters ):
Code of function
function_name(optional parameters values)

1 S P SHARMA SIR CS Support Material


2023-2024 Session Class 12 Computer Science
 In Python every function defined by def keyword.
 Function must be called/invoked to execute it code.
 A function must be defined before the function calling.
 Every function returns a value using the return statement as the last statement of the function
code.
 If a function does not have any return statement then it will return None
 The first line of function definition i.e. def function_name (optional parameters): is called function
header.
 function_name (optional parameters values) statement is called function calling statement
For Example:
def show():
print("Hello")
show()
Here def show(); is called function header, print("hello") is code of the function and show() is
function calling statement.

9. Function name must be followed by ------------------- (parenthesis () / def)


10. ------------ Keyword is used to define a function (def/define/create)
11. Function will perform its action only when it is ------------- (call/jump)
12. A function which does not has return statement always return --------- (None/none/0)
13. ------- statement is the last statement of the function (calling/return/print).
14. A function must be defined ----- the function calling (before/after).
15. Function return a value using the ----- statement (jump/return).
16. What will be the output of the following code?
def show():
print("Hello")
show()
Output:
Hello
17. What will be the output of the following code?
def show():
print("Hello")
print(show())

2 S P SHARMA SIR CS Support Material


2023-2024 Session Class 12 Computer Science
Output:
Hello
None
18. What will be the output of the following code?
def show():
print("Hello")
return 5
print(show())
Output:
Hello
5
19. What will be the output of the following code?
def show():
print("Hello")
print("India")
return 5
print(show())
Output:
Hello
India
5
20. What will be the output of the following code?
def show():
print("Hello")
return 5
print("India")
print(show())
Output:
Hello
5
21. What will be the output of the following code?
def show():
print("Hello")
return 5
S=show()
print(S)
Output:
Hello
5
22. Write statement to call the function.
def Sum():

3 S P SHARMA SIR CS Support Material


2023-2024 Session Class 12 Computer Science
S = 10 + 20
print(S)
--------------- # Statement to call the above function
Answer:
Sum()
23. Write statement to call the function.
def Sum(A,B):
C=A+B
print(C)
--------------- # Statement to call the above function
Answer:
Sum(10,20)
24. Write statement to call the function.
def Sum(A,B):
C=A+B
return C
--------------- # Statement to call the above function
print(“Sum = “, S)
Answer:
Sum(10,20)
25. What will be the output of the following code?
def Cube(n):
print(n*n*n)
Cube(10)
Output:
1000
26. What will be the output of the following code?
def Cube(n):
print(n*n*n)
print(Cube(10))
Output:
1000
None
27. What will be the output of the following code?
def Cube(n):
print(n*n*n)
Cube(10)
print(Cube(10))
Output:
1000
1000

4 S P SHARMA SIR CS Support Material


2023-2024 Session Class 12 Computer Science
None
28. What will be the output of the following code?
def show(a):
print(a)
b=5
show(b)
Output:
5
29. What will be the output of the following code?
def show(a,b):
print("a=",a)
print("b=",b)
show(5,10)
Output:
a=5
b=10
30. What will be the output of the following code?
def show(a,b):
print("a=",a)
print("b=",b)
show(10,5)
Output:
a=10
b=5
31. What will be the output of the following code?
def show(a,b):
print("a=",a)
print("b=",b)
show(a=5,b=10)
show(b=10,a=5)
show(5,b=10)
show(5,a=10)
show(a=5,10)
Output:
a=5
b=10

a=5
b=10
a=5
b=10

5 S P SHARMA SIR CS Support Material


2023-2024 Session Class 12 Computer Science
Error: Error in function calling show(a=5,10) keyword argument must be written after the
positional arguments.
32. What will be the output of the following code?
def show(a=5,b=10):
print("a=",a)
print("b=",b)
show()
show(15)
show(20,30)
show(20,30,50)
Output:
a= 5
b= 10

a= 15
b= 10

a= 20
b= 30

Error: Error in Function calling show(20,30,50) Need two arguments given three
33. What will be the output of the following code?
def show(a=5,b):
print("a=",a)
print("b=",b)
show(10)
Output:
Error –Error in function header default argument written after positional argument
34. What will be the output of the following code?
def show(a,b=10):
print("a=",a)
print("b=",b)
show(10)
show(20,20)
show()
Output:
a= 10
b= 10
a= 20
b= 20

Error: Error in function calling show() missing one positional argument.

6 S P SHARMA SIR CS Support Material


2023-2024 Session Class 12 Computer Science
35. What will be the output of the following code?
def show(*n):
print(n)
show()
show(3)
show(3,9)
show(9,7,5,8)
show(31,"Shiva Parashar", 28, 500.25)
Output:
()
(3,)
(3, 9)
(9, 7, 5, 8)
(31, 'Shiva Parashar', 28, 500.25)
36. What is the difference between argument and parameter?
Parameters are the variable listed inside the parentheses in the function definition. Arguments are
the values that are sent to the function when it is called.
37. What is the difference between actual and formal parameter?
Formal parameters are the variables listed inside the parentheses in the function definition. Actual
parameters are the values that are sent to the function when it is called.
38. What is the signature of a function?
Combination of the function name and its parameter list is called the signature of the function.
39. How many types of function arguments?
Four types of arguments use in Python –
1. Positional Arguments
2. Keyword / Name Arguments
3. Default Arguments
4. Variable Length Arguments
40. Explain positional arguments with example.
Positional arguments are arguments that need to be included in the proper position or order. The
first positional argument always needs to be listed first when the function is called. The second
positional argument needs to be listed second and the third positional argument listed third, etc.
def show(a,b):
print("a=",a)
print("b=",b)
show(5,10)
Output will be:
a=5
b=10

def show(a,b):
print("a=",a)

7 S P SHARMA SIR CS Support Material


2023-2024 Session Class 12 Computer Science
print("b=",b)
show(10,5)
Output will be:
a=10
b=5

41. Explain keyword/name arguments with example.


A keyword argument is an argument passed to a function or method which is preceded by a
keyword and an equals sign. The syntax is:
function_name(keyword=value)
Where function_name is the name of the function, keyword is the keyword argument and value is
the value or object passed as that keyword.
42. Explain default arguments with example.
43. Explain variable length arguments with example.
44. Positional arguments always written --------- the keyword arguments (before/after).
45. Default arguments values are ------------------ to pass in the function calling (necessary/optional).
46. Default arguments always written -------------- the positional arguments (before/after).
47. Positional arguments values are ------------------ to pass in the function calling (necessary/optional).
48. Keyword arguments always written --------- the Positional arguments (before/after).
49. Functional name and parameter list is called function ---------- (signature/definition)
50. We can pass zero or more arguments in ------------ arguments (variable length/positional)
51. Default arguments define in function ------------- (header/calling).

8 S P SHARMA SIR CS Support Material

You might also like