Q1. What is function? Why we need functions, Explain its types.
Answer-
Function Definition-
A function is a block of organized and reusable program code that performs single, specific, and well defined task.
It is piece of code that perform a well defined task.
Need of Function-
1. Dividing the program into separate well defined functions facilitates each function to be written and tested separately.
2. User-defined functions are reusable code blocks; they only need to be written once, then they can be used multiple times.
3. Functions are very useful, writing common utilities to specific business logic.
4. These functions can also be modified as per requirement.
5. The code is usually well organized, easy to maintain, and developer-friendly. Which means it can support the modular
design approach.
6. As user-defined functions can be written independently, the tasks of a project can be distributed for rapid application
development.
7. A well-defined and thoughtfully written user-defined function can ease the application development process.
Types of Functions-
1. Built-in functions- These are the predefined function in python.
e.g. min(), max(), print(), input()
2. User-Defined Functions (UDFs)- These are user or programmer created functions.
3. Anonymous functions/lambda function- The function which is created without def keyword. These function does not
have any name , mostly have single line code
--------------------------------------------------------------------------------------------------
Q2. What is user defined function(UDF)? How we define and call user define function. Give suitable example.?
Answer-
User Defined Functions-
1. Besides using built-in functions, users can also write their own functions. Such functions are called user defined functions.
2. User-defined functions are reusable code blocks; they only need to be written once, then they can be used multiple times.
Defining a Function-
Defining a function means specifying its name, parameters that are expected and set of instructions.
There are four steps to defining a function in Python-
Step 1: Declare the function with the keyword def followed by the function name.
Step 2: Write the parameters inside the opening and closing parentheses of the function, and end the declaration with a colon.
Step 3: Add the program statements to be executed.
Step 4: End the function with/without return statement.
Syntax to create user defined function
def functionname (para1, para2, para3 ...): # function header
program statement1
program statement3 #function body
program statement3
....
return expression
Function Call-
1. Function is called using function name and specifying number of argument in parentheses
2. When function is called, the interpreter checks that the correct number and type of arguments are used in the function call.
3. List of variables used in function call is known as the actual parameter list.
4. Actual parameters may be variables, expressions or constants.
Syntax to Call/Execute user defined function-
functionname(arg1,arg2...)
Example of User defined function-
PPS UNIT 3 Functions and Modules By- Er. P. N. Shingote
, Example of User defined function-
#Defining function
def my_function( ):
print("Hello from a function")
#calling function
my_function()
Output- Hello from a function
------------------------------------------------------------------------------------------------
Q3. What is use of return statement in function? give syntax and example.
Answer-
return statement-
1. The return statement exit a function.
2. return statement passing back an expression to the calling function
3. The return statement with no argument is same as return None.
4. If function don’t have return statement, then by default it returns None.
5. Function can return multiple values using return statement.
6. Multiple values from function retrun as tuple, so these multiple values are assigned to multiple variables based on
sequence.
Syntax for return statement-
return [expression]
Example of return statement-returning single value
#Program to add two numbers
def add(a,b): #function definition
c=a+b
return c #return value of c
sum=add(10,20) #function call
print(sum)
In above example, def is keyword, a and b are the parameters , c=a+b is the statement/expression executed by function.
return statement return value of c to the caller. We can call above function as - sum=add(10,20)
OUTPUT-
30
Example of retrun statement return multiple values
#Defining function
def sumprod(a,b):
c=a+b
d=a*b
return c,d #return statement, returns multiple values c and d
# function call
s,p=sumprod(10,20) #returned values c and d are stored in variable c and d respectively
print(“s= “,s) #30
print(“p= “,p #200
Q4. Explain function parameters and arguments.what are types of arguments? Give suitable examples.
Answer-
Function parameters and arguments-
1. Parameter is a variable in a function definition.
2. Argument is the actual value of the variable that gets passed to function.
3. When a function is called, the arguments are the data you pass into the function’s parameters.
PPS UNIT 3 Functions and Modules By- Er. P. N. Shingote