All Function Methods:
Example: 1
Python Function with No argument and No Return value Example
def Adding():
a = 20
b = 30
Sum = a + b
print("After Calling :", Sum)
Adding()
O/P: After Calling : 50
Example: 2
Function with No arguments and with a Return value Example
def Multiplication():
a = 10
b = 25
Multi = a * b
return Multi
print("After Calling the Multiplication : ", Multiplication())
O/P: After Calling the Multiplication : 250
Example: 3
Python Function with argument and No Return value
def Multiplications(a, b):
Multi = a * b
print("After Calling the Function:", Multi)
Multiplications(10, 20)
O/P: After Calling the Function: 200
,Multiplications(15, 18)
O/P: After Calling the Function: 270
Example: 4
Python Function with arguments and Return value Example
def Addition(a, b):
Sum = a + b
return Sum
# We are calling the Function Outside the Definition
print("After Calling the Function:", Addition(25, 45))
O/P: After Calling the Function: 70
Syntax:
Syntax of Function
def function_name(parameters):
"""docstring"""
statement(s)
Example: 5
def greet(name):
"""
This function greets to
the person passed in as
a parameter
"""
print("Hello, " + name + ". Good morning!")
greet('saiii')
O/P: Hello, saiii. Good morning!
greet('Love')
,O/P: Hello, Love. Good morning!
Example: 6
In python, the function definition should always be present before the function call. Otherwise,
we will get an error. For example?
# function call
greet('Paul') #Fisrt hey function ni call chesthey error vasthadi. first function rayali next function
call cheyali.
# function definition
def greet(name):
"""
This function greets to
the person passed in as
a parameter
"""
print("Hello, " + name + ". Good morning!")
#first function cheysinaduku error vachindi.
O/P: NameError: name 'greet' is not defined
Example: 7
Docstrings:
def greet(name):
"""
This function greets to
the person passed in as
a parameter
"""
print("Hello, " + name + ". Good morning!")
print(greet.__doc__)
O/P:
, This function greets to
the person passed in as
a parameter
Example: 8
The return statement
The return statement is used to exit a function and go back to the place from where it was called.
Syntax of return
return [expression_list]
This statement can contain an expression that gets evaluated and the value is returned. If there is
no expression in the statement or the return statement itself is not present inside a function, then
the function will return the None object.
def greet(name):
"""
This function greets to
the person passed in as
a parameter
"""
print("Hello, " + name + ". Good morning!")
print(greet("May"))
O/P:
Hello, May. Good morning!
None
Example: 9
Example of return
Example: 1
Python Function with No argument and No Return value Example
def Adding():
a = 20
b = 30
Sum = a + b
print("After Calling :", Sum)
Adding()
O/P: After Calling : 50
Example: 2
Function with No arguments and with a Return value Example
def Multiplication():
a = 10
b = 25
Multi = a * b
return Multi
print("After Calling the Multiplication : ", Multiplication())
O/P: After Calling the Multiplication : 250
Example: 3
Python Function with argument and No Return value
def Multiplications(a, b):
Multi = a * b
print("After Calling the Function:", Multi)
Multiplications(10, 20)
O/P: After Calling the Function: 200
,Multiplications(15, 18)
O/P: After Calling the Function: 270
Example: 4
Python Function with arguments and Return value Example
def Addition(a, b):
Sum = a + b
return Sum
# We are calling the Function Outside the Definition
print("After Calling the Function:", Addition(25, 45))
O/P: After Calling the Function: 70
Syntax:
Syntax of Function
def function_name(parameters):
"""docstring"""
statement(s)
Example: 5
def greet(name):
"""
This function greets to
the person passed in as
a parameter
"""
print("Hello, " + name + ". Good morning!")
greet('saiii')
O/P: Hello, saiii. Good morning!
greet('Love')
,O/P: Hello, Love. Good morning!
Example: 6
In python, the function definition should always be present before the function call. Otherwise,
we will get an error. For example?
# function call
greet('Paul') #Fisrt hey function ni call chesthey error vasthadi. first function rayali next function
call cheyali.
# function definition
def greet(name):
"""
This function greets to
the person passed in as
a parameter
"""
print("Hello, " + name + ". Good morning!")
#first function cheysinaduku error vachindi.
O/P: NameError: name 'greet' is not defined
Example: 7
Docstrings:
def greet(name):
"""
This function greets to
the person passed in as
a parameter
"""
print("Hello, " + name + ". Good morning!")
print(greet.__doc__)
O/P:
, This function greets to
the person passed in as
a parameter
Example: 8
The return statement
The return statement is used to exit a function and go back to the place from where it was called.
Syntax of return
return [expression_list]
This statement can contain an expression that gets evaluated and the value is returned. If there is
no expression in the statement or the return statement itself is not present inside a function, then
the function will return the None object.
def greet(name):
"""
This function greets to
the person passed in as
a parameter
"""
print("Hello, " + name + ". Good morning!")
print(greet("May"))
O/P:
Hello, May. Good morning!
None
Example: 9
Example of return