Python Function

 

Python Function

Functions are the most important aspect of an application. A function can be defined as the organized block of reusable code, which can be called whenever required.

Python allows us to divide a large program into the basic building blocks known as a function. The function contains the set of programming statements enclosed by {}. A function can be called multiple times to provide reusability and modularity to the Python program.

The Function helps to programmer to break the program into the smaller part. It organizes the code very effectively and avoids the repetition of the code. As the program grows, function makes the program more organized.

Python provide us various inbuilt functions like range() or print(). Although, the user can create its functions, which can be called user-defined functions.

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 built-in functions are those functions that are pre-defined in Python.


Advantage of Functions in Python

There are the following advantages of Python functions.

  • Using functions, we can avoid rewriting the same logic/code again and again in a program.
  • We can call Python functions multiple times in a program and anywhere in a program.
  • We can track a large Python program easily when it is divided into multiple functions.
  • Reusability is the main achievement of Python functions.
  • However, Function calling is always overhead in a Python program.

Function Calling

In Python, after the function is created, we can call it from another function. A function must be defined before the function call; otherwise, the Python interpreter gives an error. To call the function, use the function name followed by the parentheses

The return statement

The return statement is used at the end of the function and returns the result of the function. It terminates the function execution and transfers the result where the function is called. The return statement cannot be used outside of the function.

Arguments in function

The arguments are types of information which can be passed into the function. The arguments are specified in the parentheses. We can pass any number of arguments, but they must be separate them with a comma.

Call by reference in Python

In Python, call by reference means passing the actual value as an argument in the function. All the functions are called by reference, i.e., all the changes made to the reference inside the function revert back to the original value referred by the reference.

Types of arguments

There may be several types of arguments which can be passed at the time of function call.

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

Scope of variables

The scopes of the variables depend upon the location where the variable is being declared. The variable declared in one part of the program may not be accessible to the other parts.

In python, the variables are defined with the two types of scopes.

  1. Global variables
  2. Local variables

The variable defined outside any function is known to have a global scope, whereas the variable defined inside a function is known to have a local scope.

0 Comments