SYNTAX AND STRUCTURE OF User Defined Functions
(Own Functions)
Python allows us to define our own functions.
These own functions are called as user defined functions.
The creation of user defined functions has two parts:
1. Function Definition
2. Function Use (Function Call or Function Invocation)
Function Definition
In Python, before using the functions in the program, the function must be defined.
Syntax:
def name of function (list of formal parameters):
body of function
def is a reserved word that tells Python that a function is about to be defined.
name of function is the name which is used to refer to the function.
List of formal parameters of the function is the sequence of names within the parentheses
following the function name.
body of function refers to any piece of Python code. A special statement return can be used
only within the body of a function.
Example:
The function definition for finding maximum of two numbers is
def max(x, y):
if x > y:
return x
else:
return y
name of function - max
list of formal parameters – x,y
Function Use (Function Call Or Function Invocation)
A function call(function invocation) is an expression that has a value which is the value
returned by the invoked function.
When the function is used, the formal parameters are bound (as in an assignment statement)
to the actual parameters (arguments) of the function invocation (function call).
Syntax:
functionname (actualarguments)
Example:
max(3, 4)
- binds x to 3 and y to 4.
The execution of a return statement terminates the invocation of the function.
Example:
The value of the expression max(3,4) * max(3,2) is 12, because the first invocation of max
returns the int 4 and the second returns the int 3.
Program:
# Python Program to add 2 numbers using function
def add(x, y): # function definition
return x+y
a=int(input("Enter a:"))
b=int(input("Enter b:"))
c=add(a,b) # function call
print("Addition:",c)
PARAMETER PASSING IN USER DEFINED FUNCTIONS:
• The parameters present in function definition statement is called as Formal Parameters or
Parameters.
• The arguments present in function call statement is called as Actual arguments or
Arguments.
• Inside the function, the arguments are assigned to variables called parameters.
def print_twice(result):
print(result)
print(result)
• This function assigns the argument to a parameter named result.
• The value can also be used as argument. When the function is called, it prints
the value of the parameter twice.
Example:
>>> print_twice('Hello')
Hello
Hello
>>> print_twice(42)
42
42
• The expression can also be used as arguments. The argument is evaluated before the
function is called.
Example:
>>> print_twice('Hello'*4)
Hello Hello Hello Hello
Hello Hello Hello Hello
• Variable can also be used as an argument.
Example:
>>> m = 'Hello Python'
>>> print_twice(m)
Hello Python
Hello Python
#FUNCTION DEFINTION
def swap(x,y):
x, y = y, x
#DRIVER CODE
x = int(input(“Enter x value:”))
y = int(input(“Enter y value:”))
print(“Before Swapping”)
print(“x=”,x,”y=”,y)
swap(x,y)
print(“After Swapping”)
print(“x=”,x,”y=”,y)
OUTPUT:
Swap two variables using function in Python
Swap of two variables using function in Python can be done by passing our variables to
the user defined function as arguments and returning or printing the swapped data.
Regarding arguments and returning values , functions are 4 types
•With arguments and return value
•With arguments and without return value
•Without arguments and with return value
•Without arguments and without return value
Python code: Swap two variables using function in Python
With arguments and return value: