Description

In Python programming, a function is a block of statements that makes our code readable. If we do that function in the program, then we do not have to write the same statement of code again and again. The function divides the program into smaller blocks.

It is a collection of statement that performs an specific task.

It executes when it is called by its name. 

A large program is divided into a number of small building block for siplicity and this building block is called function.

We can call a function again and again.

The most important feratures of functio is code reusability.

def keyword is used to define function in python.

Syntax of function in Python

def name_of_function(parameter_list):
    #body of function

def

  • It is s keyword which is used to define function in python programming.

name_of_function

  • It is the actual name of the function.
  • It is also used at the time of calling the function.

parameter_list

  • It is the place where we can pass a number of parameter / variable.
  • These variables may be used in the program.
  • The value of parameter is passed from the calling of function.
  • It is optional.

There are mainly two types of functions.

  • User-define functions - The user-defined functions are those define by the user to perform the specific task.
  • Built-in functions - The build-in functions are those functions that are pre-defined in python.

Create a function in python

def name():
    print("John")

Call the function in Python

def name():
    print("John")
name()


'''
Output:
John

'''

Return Statement of function in Python

The return statement is used at the end of the function and it reutrns the result of the function. It terminates the execution of the function, trnsfers the result from where the function is called.

Syntax:

return [return_statement]

Example of Return statement function in Python.

# Define function
def sum(a, b):
    c = a + b
    return c

#Calling sum() function 

print(sum(4, 5))

'''
#Output:
9

'''

In the above code, we have defined a function named 'sum' whose statement c = a + b which computes the given value, which is our result and returns the function call from where.

Without return statement in Python

# Define the function
def sum():
    a = 5
    b = 6
    c = a + b
    print(c)

# Call function
sum()

'''
#Output:
11

'''

In the above code, we have created a function named 'sum', this function does not return object.

Arguments and Parameters in Function in Python

An argument is a type of information that is passed through a function, The arguments are specifed after the function name that there are inside the parenthesis. We can pass any number of arguments to a function. But they must be separated with commas. The terms parameter and argument can be used for the same thing: Information that are passed into a function.

If we look from the perspective of the function:

A parameter variable is wriiten inside the parenthesis in function definition and an argument is the value that is sent to the function when function is called.

Example 1: Function With Argument

#Define the function
def print_name_with_hi(name):
    print("Hi!! ", name)

#Call the function
print_name_with_hi("John")

'''
#Output:
Hi!! John

'''

Call by reference in Python

Call by reference means in Python, passing the actual value as an argument in the function. All the functions are called by reference, all the canges made to the refernece inside the function revert the back to the actual value referred by the reference.

Example 1:

# Passing Immutable ( List )

def c_list(l_ist):
    l_ist.append(2)
    l_ist.append(5)

    print("List Inside the function : ", l_ist)

l_ist = [1, 4, 6, 7]


c_list(l_ist)
print("List Outside the function : ", l_ist)


'''
#Output:

List Inside the function :  [1, 4, 6, 7, 2, 5]
List Outside the function : [1, 4, 6, 7, 2, 5]

'''

Example 2:

#Passing Mutable Object
def p_c_string(str):
    str = str + " Hello "
    print("Print the string Inside the Function : ", str)


s = "Mr. John"

p_c_string(s)

print("Print the string Outside the Function : ", s)

'''
#Output

Print the string Inside the Function :  Mr. John Hello 
Print the string Outside the Function :  Mr. John

'''

Docstrings In Python

The first string after the function header is docstring. For this he has to explain the functionalities of the function, why the function is created and what is its function.

For Example:

def greet(name):
    """This function greets to the person passed with the name of the person"""
    print("Good Morning! ", name)

print(greet.__doc__)


'''
#Output:

This function greets to the person passed with the name of the person

'''

Types of arguments in Python

  1. Required arguments
  2. Default arguments
  3. Variable-length arguments ( Arbitrary Arguments , *args)
  4. Keyword arguments (Arbitrary Keywrod Arguments, **kwargs)

Required Arguments

We pass arguments at the time of function calling. As far as the arguments are required. We pass the arguements to the function in such a way that the position of those arguments is absolutely accurate. If we pass the argument less of the required function, there will be error.

#Calculate Simple Interest Accepts the require arguments

def simple_interest(p, r, t):
    """ To Calculate the function simple Interest """
    return ( p * t * r ) / 100

p = float(input("Enter the Principle Amount : "))
r = float(input("Enter the Rate of Interest : "))
t = float(input("Enter the time in Years : "))

print("Simple Interest : ", simple_interest(p, r, t))


'''
#Output:
Enter the Principle Amount : 3000
Enter the Rate of Interest : 6
Enter the time in Years : 4
Simple Interest :  720.0

'''

Default Arguments

Python allows us to initialize the arguments at the function definition. If the value of any of the arguments is not provided at the time of function call, then that argument can be initialized with the value given in the definition even if the argument is not specified at the function call.

def de_arg(name, age="30"):
    print("Name is : ", name)
    print("Age is : ", age)

# Call the function with No pass the age argument
de_arg("John")

# Call the function with pass the age argument
de_arg("John", 24)



'''
#Output:
Name is :  John
Age is :  30
Name is :  John
Age is :  24

'''

 

Comments

Login is mandatory to comment Please login

Recommended Posts

What is Python? How Python is interpreted? What are the tools that help to find bugs or perform static analysis? What are Python Decorators?

Python Program is a gerneral purpose of high level, intereted, interative and object-oriented scription programming language developed by Guido Van Rossum in  1991.