Function 1.1-1
Function 1.1-1
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)
a=5
b=10
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
def show(a,b):
print("a=",a)