Python Function

Software developer often needs to use some part of the code frequently. Instead of writing the same code again and again, it is better to write once.

A function is a reusable code which only executes when it is called. In Python, the “def” keyword is used to define the function. You can pass the information to function is called the parameters. A function can return the data as a result.

Syntax:

def function_name(arg1,arg2,....,argn):
     ''' Reuseable code'''

Examples –

def sum_fn(a,b):
     return a+b              # arithmetic addition of two varaible

print(sum_fn(2,3))
print(sum_fn(5,-2))
#Output
5               # output of sum_fn(2,3)
3               # output of sum_fn(5,-2)

You can also pass the value to parameter by using an assignment operator.

def multiply(a,b,c):
    print(f'a: {a}  b: {b}  c: {c}')
    return a*b*c

print("The Multiplication: ",multiply(2,4,3))    
print("The Multiplication: ",multiply(b=2,c=4,a=3))               
# Output
a: 2  b: 4  c: 3               # Output of multiply(2,4,3)       
The Multiplication: 24

a: 3  b: 2  c: 4               # Output of multiply(b=2,c=4,a=3)
The Multiplication: 24

Default Parameter Value

You can also specify the default value to the parameter. If you call a function without parameter, the default parameter value is considered. Let’s see the below example of how to use default parameter in a function.

In the example, the function has specified the default value of parameter ‘c’ to 10.

def multiply(a,b,c=10):
    return a*b*c

print("Example 1:",multiply(2,4,3))    # c=3 is considered

# c=10 is considered, as function doesn't pass the parameter c
print("Example 2:",multiply(2,4))      
# Output
Example 1: 24
Example 2: 80

Arbitrary Arguments

If you don’t know how many parameters that will be passed into your function, add a * before the parameter name in the function definition.

This way the function will receive a tuple of parameters and can access the items accordingly.

def printinfo( x, *y ):
    print ("First parameter x is: ",x)
    for e in y:
        print ("Arbitrary Arguments y :",e)

printinfo( 10 )
printinfo( 10, 600, 520 )
# Output:
First parameter x is: 10        # output of printinfo(10)

First parameter x is:  10       # output of printinfo(10,600,520)
Arbitrary Arguments y : 600
Arbitrary Arguments y : 520

Lambda:

A lambda is a small asynchronous function in Python. A lambda function has an only single expression with any number of parameters but can have only one expression.

Example:

my_function = lambda a, b, c : a + b +c
my_function(1, 2, 3)
# Output
6

A lambda function is used as an anonymous function inside another function.

def lambda_fn(n):
    return lambda a : a * n

multi_fn = lambda_fn(3)
print(multi_fn(11))
# Output: 
33

.     .     .

Leave a Reply

Your email address will not be published. Required fields are marked *

Python Tutorials

Python – List Comprehension

Python Set

Python : Assert Keyword

Python Dictionary

Python Tuple

Python List

Python: String

Python: String Formatting using f-string

Python: String Formatting – 2

Python: String Formatting – 1

Python Input/Output

Python Type Conversion and Type Casting

Python Comments

Python – Regular Expression (RegEx)

Python Iterators

Python File Handling

Python Exceptions

Python Loops

Python If…Else

Python Collections

Python Operators

Variables in Python

Python Syntax

Introduction to Python

Python OOPs Concepts

Study Machine Learning