Functions in Python
Last updated Aug 10, 2021 A function is known as the block of code which only gets executed and run when it is called. Basically a function is a block of organized, reusable code which is generally used to perform a single or a given number of actions. In Python there are a number of in-built functions like - Print() and in python you can even create your own functions. A function is used to return data in the form of a result.
Creating and Calling a Function
Function in Python Programming Language is defined through def keyword. Let's understand the steps about how we can create a function:-
def func1():
print("Hello Programmer")
To call a function in Python, use the function name which you had used followed by parenthesis:
def func1():
print("Hello Programmer")
func1()
|
So we have covered the basic concepts of Creating and Calling a function in Python Programming Language.
Python function Examples
Now, we will understand the use of function and how it works with the help of example:-
def func1(name):
""" function is created """
print("Hello, " + name + ". How are you?")
func1('rohit')
|
OUTPUT
Hello, rohit. How are you?
In the above code we had used func1 which is out function and we had assigned the value of rohit to func1 and we get the desired output at the time of calling the function. We had understood the basic example of function.
Lambda Functions Python Anonymous Functions
In Python Lambda functions are also known by the name of Anonymous function. Generally Lambda functions are single line functions which are declared without any name and these functions can have any number of arguments but they must contain only one expressions. These are also known as One-Liner functions. These functions are similar to regular functions and these lambda functions are also written with the help of def keyword.
Syntax of Lambda Functions / Anonymous Functions
Let's look at the syntax of Lambda functions:-
lambda arguments : expression
|
s you can see that the syntax of Lambda function is written in a single line, this is the reason why these are known as one-liner functions. These functions can take a number of arguments.
Now we will take few examples which will explain us about the use of Lambda Functions / Anonymous Functions
When we have one argument
Here we will take the case when we have only single argument i.e. a
ans = lambda a : a + 5
print(ans(2))
|
OUTPUT
7
When we have two arguments
Now we will take two arguments which are a and b and will use lambda functions with the help of these two arguments
ans = lambda a, b : a * b
print(ans(2, 3))
|
OUTPUT
6
3. When we have three arguments
In this case we will take three arguments a, b and c and will use all three arguments in lambda functions
ans = lambda a, b, c : a + b + c
print(ans(2, 3, 4))
|
OUTPUT
9
So we had understood the use of Lambda functions by taking all the basic examples of lambda functions.
Why use Lambda Functions instead of regular Functions?
As lambda functions are one-liner functions so they are faster than regular def functions. Also lambda functions provide higher efficiency than def functions. And we use these lambda functions when we require nameless functions for a short span of time. These Lambda Functions are used along with some built-in functions like they are used with map(), filter() and reduce() functions. Lambda Functions does not include any return statement but regular functions include return statements. Generally lambda functions contain expressions which are returned by nature.
Both Functions provide good framework but Lambda functions provide a better platform than regular def functions.
Python Function Arguments
Arguments in python are specified after the function name, inside the parenthesis. You can add a number of arguments you wish to but you need to separate them with a comma. Here the information is passed into functions in the form of arguments. Arguments are also known by the name of pass by reference.
Let's take an example of Arguments:-
def func1(fname): print(fname + " sharma")
func1("sumit")
func1("abhinav")
|
OUTPUT
sumit sharma
abhinav sharma
So as you can see we have assigned a value (sharma) and it will be passed to all the arguments which come under fname. So we can get as many output as many arguments are passed in the function.
Types of Arguments
There are 4 types of Arguments in Python Programming Language:-
-
Required arguments
-
Keyword arguments
-
Default arguments
-
Variable-length arguments
Required Argument
The arguments which are passed to a function in a correct positional order is known as the required argument. But in this case we get a syntax error like this as shown in the example given below:-
def func2(str):
print(str)
return;
func2()
|
OUTPUT
func2()
TypeError: func2() missing 1 required positional argument: 'str'
So you can see we got an error in the above code.
Keyword Argument
These functions are related to the function calls. Here, the function caller identifies the given argument by its parameter name. In this type of argument we get the value we stored in the argument and is used as a keyword. Let's take an example of Keyword Argument:-
def func2(str):
print(str)
return;
func2(str="Hello coder")
|
OUTPUT
Hello coder
As you can clearly see that we got our output which we passed in the argument.
Default Argument
The argument which assumes a default value if a value is not given or provided int the function call for that given argument then is known as Default Argument. Let's understand an example of Default Argument:-
def func3(name, age=20):
print("Name: ", name)
print("Age ", age)
return
func3(age=50, name="sonu")
func3(name="sonu")
|
OUTPUT
Name: sonu
Age 50
Name: sonu
Age 20
Here in the above code we got the default age when the function is not passed.
Variable Length Argument
These arguments are not named in the function definition, unlike required and default arguments. These variables acts as args and are denoted by asterisk(*) and it is placed before the variable name that holds the values of all non keyword variable arguments. Let's take an example of args or Variable length argument:-
def func4(arg1, *value):
print("Output is: ")
print(arg1)
for var in value:
print(var)
return;
func4(10)
func4(20, 30)
|
OUTPUT
Output is:10
Output is:2030
So we got the output even after calling the args value.
Thanks for Reading. Hope you got a clear and concise explanation about Python Functions
Article Contributed By :
|
|
|
|
77 Views |