Python Function

Facebooktwitterredditpinterestlinkedinmailby feather

Python Function

A Python function is a reusable, organized block of code that is used to perform the specific task. The functions are the appropriate way to divide an extensive program into a useful block. It also provides reusability to our program. A code block can be reused by calling the function.

All functions are treated as an object in Python, so it is more flexible than other programming languages.

 In Python, there are two types of functions:

  • Built-in functions – These functions are the part of the Python libraries and packages. These are also called as pre-defined functions. You can learn it from here.( https://www.tutorialandexample.com/python-built-in-functions/)
  • User-defined functions – These functions are defined by the user as per their requirement. We will learn the user-defined function in this tutorial.

Creating a function

Below are the basic steps to create a user-defined function.

  • The def keyword is used to define function followed by the function name.
  • Arguments should be written inside the opening and closing parentheses of the function, and end the declaration with a colon.
  • Write the program statements to be executed within the function body.
  • The return statement is optional. It should be written at the end of the function.

The syntax is given below:

The argument list can contain none or more arguments. The arguments are also called parameters. The function body contains indented statements. The function body gets executed whenever the function is called.  The arguments can be optional or mandatory.

Calling a Function

After declaring a function, it must be called using function name followed by parentheses with appropriate argument.

Note: It is necessary to define a function before calling; otherwise, it will give an error.

Consider the following examples:

Example-1

Output:

Example-2

Output:

Parameter Passing

There are two most common strategies of passing an argument to a function.

  • Call by Value

This strategy is used in C, C++ or Java but not used in Python. In call by value, the values of actual parameters are copied to function’s formal parameters, and both types of parameters are stored in separate memory locations. So if we made any changes in formal parameters, that changes will not be reflected in actual parameters of the caller function.

  • Call by Reference

The functions are called by reference in Python, which means all the changes performed to the inside the function reflected in an actual parameter.

Consider the following examples:

Example 1:

Output:

In the above program, defined function is mul(m,n), where m and n are formal arguments, and caller function is mul(a,b), where a and b are the actual argument.

Example-2

 Write a program to take a list and return list with a unique element

Output:

Types of arguments

There may be several types of arguments, which are listed below

  1. Required arguments
  2. Default arguments
  3. Keyword arguments
  4. Variable-length arguments
  1. Required Arguments

The required arguments are those arguments which are mandatory to pass at the time of function calling with exact match their positions in the function call and function definition. If the argument is not provided in the function call or made any changes in arguments position, then Python interpreter will show an error.

Example 1

Output:

 Example 2.

Output:

  •  Default argument

The default arguments are those arguments that assign a value with the argument at the time of function definition. If the argument is not specified at the time of function call, then it will be initialized with the value which was given in the definition. For example

Example 1

Output:

  • Keyword arguments

The benefit of keyword arguments is that we can pass the argument in the random order, which means the order of passing arguments doesn’t matter. Each argument treated as the keyword. It will match argument names in the function definition and function call.  If

 Example-1

Output:

In the above program, we have passed the arguments in a different order in function calls. The name of arguments must be the same as function definition; otherwise, it will show an error.

  • We can pass keyword argument and required argument together. For example:

Output:

In the above program, the first argument is the required argument followed by two keyword arguments.

Note: It is important to remember that required argument must not pass after keyword argument; otherwise, it will show an error.

  • Variable-length arguments

Sometimes we are not sure about numbers of argument that can be passed to a function, for such scenario, we use variable-length arguments.

There are two types of variable-length argument in a function:

  • *args (Non-Keyword argument)
  • *kwargs (Keyword argument)

*args (Non-Keyword argument)

Python provides *args which allows to pass the variable number of argument in a function.

We should use an asterisk ( * ) before the argument name to pass variable length arguments. The arguments are passed as a tuple, and these passed arguments make tuple inside the function with the same name as the argument excluding asterisk *. For example:

Output:

**kwarg arguments

We cannot pass the keyword argument using *args. Python provides **kwargs; It allows us to pass variable-length of keyword argument to the function.

We must use the double-asterisk ** before the argument name to denote this type of argument. The arguments are passed as a dictionary, and these arguments make a dictionary inside the function with name same as the parameter without double asterisk **. For example:

Output:

Lambda (Anonymous) Function

Python lambda is also known as an anonymous function, which means function is declared with no name. Lambda functions are different than a regular function. It has a more concise syntax. The syntax is following

The lambda is used to define an anonymous function. It can consist number of argument, but can consist only one expression.

Output:

Scope of Variable

The scope of variables can be defined by the place where the variables have declared. Variables can be defined with two types of scope.

  1. Global Variable

The global variables are defined outside the function, hence global variables can be accessed throughout the program.

  • Local Variable

The local variables are defined inside the function, hence local variables can be accessed only inside the function.

Consider the following example:

Example-1 Global Variable

Output:

Example-2 Local Variable

Output:

Facebooktwitterredditpinterestlinkedinmailby feather