regularpython@gmail.com
Functions
Functions are mainly used to do a specific task and which only runs when we called. To create a function in python we use def keyword. There are four types of functions in every programming language. If you know these four types, you can write or learn any programming language easily.
How to create a function in python?
We can create functions by using a def keyword.
Syntax:
def function_name():
#your statements
pass
Types of functions:
Function with no parameters and no return value
Function with parameter and no return value
Function with no parameters and with a return value
Function with parameters and values
Function with no parameters and no return value:
These types of functions are used to run other functions. In this function, we don’t need to pass any parameters and we don’t get any return values. See the below example to get clarity.
Example:
def addition(): print('This is an additional function') def subtration(): print('This is a subtraction function') def multiplication(): print('This is a multiplication function') def runall_functions(): addition() subtration() multiplication() # Function with no parameters and no return value runall_functions()
Output:
This is an additional function
This is a subtraction function
This is a multiplication function
Function with parameter and no return value:
This type of function is used to send parameters to a function and no need to get return values.
Example:
def addition(a, b): print('{} + {} = {}'.format(a,b, a+b)) def subtration(a, b): print('{} - {} = {}'.format(a,b, a-b)) def multiplication(a, b): print('{} * {} = {}'.format(a,b, a*b)) # Function with parameters and no return value addition(5, 5) # Function with parameters and no return value subtration(5, 5) # Function with parameters and no return value multiplication(5, 5)
Output:
5 + 5 = 10
5 - 5 = 0
5 * 5 = 25
Function with no parameters and with return value:
This type of functions are used to store all constant values in a function and get whenever we want it.
Example:
def constant_values(): values = { "pi" : 3.1415, "one_minute" : 60, #seconds "name" : "RegularPython" } return values cv = constant_values() print("Pi value = {}".format(cv['pi'])) print("One Minute = {} Seconds".format(cv['one_minute'])) print("Website name is {}".format(cv['name']))
Output:
Pi value = 3.1415
One Minute = 60 Seconds
Website name is RegularPython
Function with parameters and values:
This is the most important function in all programming languages. This function takes parameters and send the expected values. If you are using any predefined functions or modules, means you’re always using this function defiantly. Don’t neglect to understand this function. If you understand this function you can do or learn any programming languages, not only python.
Example:
def addition(a, b): return a+b def subtration(a, b): return a-b def multiplication(a, b): return a*b # Function with parameters and return value a = addition(5, 5) print("A value = {}".format(a)) # Function with parameters and no return value b = subtration(a, 5) print("b value = {}".format(b)) # Function with parameters and no return value c = multiplication(b, 5) print("c value = {}".format(c))
Output:
A value = 10
b value = 5
c value = 25
Default Parameter Value:
If we want to send a default value to a function, see the below example how to do it.
Example:
def addition(a, b = 5):
return a+b
def subtration(a, b = 6):
return a-b
def multiplication(a, b = 8):
return a*b
# Default value of b is 5
a = addition(5)
print("A value = {}".format(a))
# Default value of b is 6
b = subtration(10)
print("b value = {}".format(b))
# Default value of b is 8
c = multiplication(2)
print("c value = {}".format(c))
Output:
A value = 10
b value = 4
c value = 16
What is the default return value in python functions?
If we don’t send any return value than python sends a default value(None).
Example:
def default_value():
a = 100
b = default_value()
print("Default Value = {}".format(b))
Output:
Default Value = None