Hello Technotizers, this article will give you detailed information on Python Functions. Function is a block of code that is designed to perform a specific task. It runs only when it is called in the code. A function can be given some data known as parameters which is used in the function. It can also return some result.
·
Creating a Function:
In
Python, a function is created or defined using the def keyword.
def my_func():
print("Hello, I'm a function!")
my_func is the name of the function.
·
Calling a Function:
To call a function, the name
of the function is written followed by parenthesis.
def my_func():
print("Hello, I'm a function!")
my_func()
The output is obtained as:
Hello, I'm a
function!
The task written in the function of printing
a line is done when the function is called in the code. The function can be
called multiple times and it will give the same output multiple times.
·
Arguments:
Information
passed inside a function are called arguments. There can be multiple number of
arguments inside a function, separated by a comma. Arguments are written inside
parenthesis after the function name.
def my_name(name):
print("My name is",name)
my_name("Harsh")
my_name("Simran")
The output is obtained as:
My name is
Harsh
My name is
Simran
In the above example, the function is created
using one argument ‘name’. When the function is called the name is passed in
parenthesis with the function name. This name is then used in the function to
print the entire sentence.
Arguments or Parameters?
These both are used for the same thing,
information passed in a function. But there is a major difference from the
function’s perspective. A parameter is the
variable name inside the parentheses in the function definition. An argument is
the value that is sent to the function when it is called in place of the
parameter.
Number of Arguments
The number of arguments passed while defining
the function and the number of arguments passed while calling the function
should be the same. Example: If a function is defined with 2 arguments
then it should be called with 2 arguments only. Using 1 or 3 arguments will
generate an error.
def my_name(fname,lname):
print("My name is",fname,lname)
my_name("Harsh")
The output is obtained as:
TypeError: my_name() missing 1 required
positional argument: 'lname'.
The error shows that the function is missing
one argument.
Arbitary Arguments
When the number of arguments to be passed is
not known, we need to add a * before the parameter name in the function
definition. With this the function will receive a tuple of arguments and access
the items accordingly.
def my_name(*names):
print("My name is",names[2])
my_name("Simran","Tanuja","Harsh")
The output is obtained as:
My name is
Harsh
Keyword Arguments
We can also pass the arguments in key = value pairs. In this way, the order of
arguments does not matter.
def my_name(name2,name3,name1):
print("My name is",name3)
my_name(name1 = "Simran",name2 = "Tanuja",name3 = "Harsh")
The output is obtained as:
My name is
Harsh
Keyword Arguments are often shortened to kwargs in Python documentations.
Arbitary Keyword Arguments
When the number of keyword arguments to be
passed is not known, we need to add ** before the parameter name in the
function definition. With this the function will receive a dictionary of
arguments and access the items accordingly with the help of keys.
def my_name(**name):
print("My name is",name2)
my_name(name1 = "Simran",name2 = "Harsh")
The output is obtained as:
My name is
Harsh
Arbitrary Keyword Arguments are often shortened to **kwargs in Python documentations.
·
Default Parameters:
Default
parameter is used when no argument value is passed to the function. Default
parameter is assigned while defining the function, by writing the default
parameter value in front of the parameter name.
def my_name(name = "Harsh"):
print("My name is",name)
my_name("Simran")
my_name()
my_name("Tanuja")
The output is obtained as:
My name is
Simran
My name is
Harsh
My name is
Tanuja
In the above example, the default parameter value is “Harsh”, so when
the function was called without an argument, it used the default parameter
value to print the statement.
·
Passing any data type as
Argument:
Python
allows any data type to be passed as an argument. The data type doesn’t lose
its identity while being used as an argument.
Example:
Passing list as an argument.
def my_name(names):
for i in names:
print("My name is",i)
names = ["Simran","Harsh","Tanuja"]
my_name(names)
The output is obtained as:
My name is
Simran
My name is Harsh
My name is
Tanuja
·
Return Values:
To
return some information as result, use the return keyword.
def my_func(number):
return 6*number
print("2*6 is:",my_func(2))
print("3*6 is:",my_func(3))
print("5*6 is:",my_func(5))
The output is obtained as:
2*6 is: 12
3*6 is: 18
5*6 is: 30
·
Pass Statement:
function
definitions cannot be empty, but if you for
some reason have a function
definition with no content, put in
the pass
statement to avoid getting an error.
def my_func():
pass
This code will show no output as pass keyword
is used in the function.
·
Recursion:
Recursion
is a method where a function calls its self. In lay man’s language, recursion
can be considered as nested functions. Recursion is a
very common mathematical and
programming concept. It’s benefit is that we can loop through data to reach a result. The programmer should be very
careful with recursion as it can be quite common to fall into writing a function which
never terminates, or one that uses excess amounts of memory or processor power.
However, when written aptly recursion can be a very
efficient and mathematically-elegant approach to programming.
def recursion(number):
if number>0:
result = number + recursion(number-1)
print(result)
else:
result = 0
return result
print("Recursion results:")
recursion(6)
The output is obtained as:
Recursion
results:
1
3
6
10
15
21
In this recursion, the argument value is
added with the next recursion result which continues in loop until the number
becomes 0.
With this we come to an end of this article. Hope it was helpful. Do provide your feedback and ideas through comments, it would be highly appreciated. See you soon!
Keep coding and exploring new techs!!
0 comments:
Post a Comment