A function is a specific block of code that performs a distinct and singular task. For instance, an electric fan above our heads rotates, performing a specific task, just like a tube light illuminates, each carrying out a unique function by converting electrical energy into mechanical energy.

Types of Function

We have already used some functions, like using the print() function to display results on the console. There are two types of functions:

  1. Built-in Functions: These are provided with the language itself, such as the print() function we’ve been using.
  2. User-Defined Functions: Sometimes, built-in functions are not sufficient for all tasks, so users need to create their own functions, known as user-defined functions.

In this section, we will discuss user-defined functions, i.e., how to create and use them.

Different Parts of a Function

A function generally has four parts:

  • Function Name
  • Arguments
  • Function Body
  • Return Type

Let’s identify these parts with an example:

# This program is an example of function in python
#Function Definition
def multiply_function(x,y):
    return z
print(multiply_function(5,10)) #calling a function



In the program above, a complete function is shown. In Python, a function is defined using the def keyword, followed by the function name. The function name can be anything, but it’s better if it is meaningful. Here, multiply_function is the function name, indicating that the function performs multiplication. Note the use of the colon : after the function name.

The variables x and y inside the parentheses are function parameters, acting like local variables within the function. The statement z = x * y is the function body, where the actual work is done. The return statement indicates what the function will return; here, it returns the product z.

The function needs to be called to execute. This is done by writing the function name followed by arguments in parentheses. In line 8, the function is called with 5 and 10 as arguments, and its result is printed using the print() function.

Types of Arguments

Python supports three types of arguments:

  • Keyword Arguments
  • Default Arguments
  • Variable-Length Arguments

Keyword argument

In the previous example, we used two arguments to get their product. Sometimes, the order of arguments matters, such as in division. To avoid errors, we use keyword arguments where we specify the value of variables explicitly.

# This program is an example of keyword argument in python
def multiply_function(x,y):
    return z
print(multiply_function(5,10)) #normal function call

print(multiply_function(y=5,x=10)) #function call using keyword argument



Notice the difference when using keyword arguments. In a normal function call, the values are assigned in order. But when using keyword arguments, the values are assigned to the respective variables based on the specified keys.

Default Arguments

Usually, functions must be called with arguments if they have parameters. However, we can set default values for parameters, allowing functions to be called without arguments.

# This program is an example of default argument in python
def multiply_function(x=10,y=5):
    return z
print(multiply_function()) # function call using default argument

print(multiply_function(100,5)) #normal function call



In line 7, the function is called without arguments, using the default values specified in the function definition. In line 8, the function is called with specific arguments, overriding the default values.

Variable-Length Arguments

Sometimes, the number of arguments a function receives is uncertain. Python provides variable-length arguments to handle such situations.

# This program is an example of variable length argument in python
def multiply_function(*values):
    return z
print(multiply_function(100,5)) # function call



Here, the arguments are received as a list. The * symbol before the parameter name indicates variable-length arguments.

Variable-length arguments can also be passed using a key-value pair:

# This program is an example of variable length argument in python
def multiply_function(**values):
    return z
print(multiply_function(x=100,y=5)) # function call



In this case, x and y act as keys, and their values are accessed using the keys within the function. The ** symbol before the parameter name indicates that the arguments are passed as a dictionary.

Variable Scope

The scope of a variable determines where it can be accessed within a program. Variables can be local or global.

Local Variable

A local variable is accessible only within the function or code block where it is defined.

Global Variable

A global variable is accessible throughout the entire program.

Example of local and global variables:

# This program is an example of local and global variable in python

def division_globalvar_function(value):
    z=x/value # Division using global variable x
    return z
def division_localvar_function(value):
    z=x/value # Division using local variable x
    return z
print(division_globalvar_function(5)) # function call

print(division_localvar_function(5)) # function call



In line 3, x is a global variable. In line 6, x is used within the function, accessing the global variable. When the function is called, the result is 20.0. In line 10, x is redefined within the function, making it a local variable. When the function is called, the local variable takes precedence, resulting in 4.0.

Global variables have a wider scope, but local variables take precedence within their scope.

0 0 votes
Article Rating
Would love your thoughts, please comment.x