Understanding Functions in Python: A Comprehensive Guide

Rumman Ansari   Software Engineer   2024-07-24 05:14:51   550  Share
Subject Syllabus DetailsSubject Details 5 Questions 2 Program
☰ TContent
☰Fullscreen

Table of Content:

What is function?

A function is a named block of statements that can be invoked by its name. A function is organized and reusable code that is used to perform a single, given action. Functions provide better modularity for your application and a high degree of code reusability.

A function is a piece of code, capable of performing a similar task repeatedly.

It is defined using def keyword in python.

A function is an organized and reusable code that is used for some single, given action. It begins by writing the keyword def. followed by the function name and parenthesis eg. def add ():

Types of a function :

Types of Python Functions

1. Built in functions :
These functions are built into Python library and can be accessed by programmer. The built-in functions of Python are always available, one needs not import any module for them. The math module of Python provides mathematical functionality.

2. Functions in module :
It is a separately saved unit whose functionality can be reused by importing the corresponding module.

3. User defined function :
They are declared with a keyword def. followed by the function name. User defined functions are those that we define ourselves in our program and then call them wherever we need.

 

Syntax of a function :

 
def <function_name>(<parameter1>, <parameter2>, ...):
     'Function documentation'
     function_body
     return <value> 
  • Parameters, return expression and documentation string are optional.

Sample function - square

def square(x):
    'Returns square of a number.'
    return x**2

Function arguments

1. Required argument :
Are the arguments passed to a function in correct position and order.


2. Keyword argument :
It is a function call where the caller function identifies the argument by the parameter name.


3. Default argument :
It is an argument that assumes a default value, if a value is not provided in a function call for that argument.


4. Variable length argument :
An is placed before the variable name that holds the value of all non - keyword variable arguments.

Scope of variable

The scope of a variable determines the portion of the program where you can access a particular identifier. There are two basic scopes of variables in Python :

1. Local variable :
They are defined and declared inside a function.


2. Global variable :
They are defined and declared outside a function.

Variables that are defined inside a function body have a local scope, and those defined outside have a global scope. All variables in a program may not be accessible at all locations in that program. This depends on where you have declared a variable or the scope of variable

Common Built-in Functions

There are many built-in functions Python offers. Here are some of them listed.

  • len : Returns the length of an object.

    • e.g : len('hello') -> 5
  • type : Returns the type of an object.

    • e.g : type([2,3]) -> <type list>
  • range : Returns a iterator of a number sequence.

    • e.g : list(range(10, 13)) -> [10, 11, 12]

    • list(range(3)) -> [0, 1, 2]

User Defined function module

inza = "Inzamamul Hoque"

def message(message):
    print("Hello, ", message)

def addWithOutParameter():
    a = 12
    b = 13
    print(a+b)

def addWithParameter(a, b):
    print(a+b)

def addWithParameterAndReturn(a, b):
    return a+b

function call

import module

# How to call a variable
print(module.inza)

# How to call a function
module.message("Rumman")

# How to call a function without parameter
module.addWithOutParameter()

# How to call a function with a parameter
module.addWithParameter(12, 53)


# Call a function with parameter and return a value
returnSumVariable =  module.addWithParameterAndReturn(2, 6)
print("Sum value After Return: ", returnSumVariable)