What is Function in Python?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
201186
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
201186
4.90
Start Learning
Topics Covered

Function refers to a named sequence of statements or a piece of code that performs a specific task. It is an organized block of code that promotes code reusability and reduces data redundancy. Using functions in our application allows us to develop organized code that is easy to read, maintain and debug.

If we imagine the whole software application as a big factory that produces a product, then functions can be thought of as little machines of that application. Each little machine (function) takes parameters as input, performs a specific task, and provides the required output. This way functions can be used to develop organized and efficient code.

Syntax of Python Functions

We can create our own functions in Python using the def keyword. The syntax of Python functions is given below:

Function definition in Python consists of the following components:

  • Keyword def : It marks the beginning of the function definition.
  • function_name : It is the name of the function.
  • parameters : These are the values provided to the function.
  • docstring : It is used to add the documentation of the function. It is an optional string that explains the functionality of the function.
  • Statements : These include the sequence of tasks that the function is required to perform. We can also use the pass keyword when we don't want to perform any task in the function.
  • Return Statement : It is an optional statement that is used to return values from the function to the calling code.

Creating a Function

Now that we have seen the syntax of a function in Python. Let's create our own function which adds two numbers in Python.

To create a function, we first have to define the function, and provide it with a unique identifier (function name), and parameters. Let's look at a simple function that adds two numbers:

In the above example, the function add_sum() takes two parameters a and b as input. It also contains the docstring which describes that the function add_sum() adds two numbers. This function returns the sum of the two parameters a and b.

Call a Function in Python

Congrats, we have created our first Python function !!! But, now you may ask how do we use this function. We can use functions in Python by calling them and passing the values according to the required parameters. Let's call the function we created earlier to understand this concept:

Here, we are calling the function add_sum() that takes two parameters a and b. Here, we are passing the numbers 3 and 2. These numbers are mapped with the parameters given in the function definition. Hence, a becomes 3, and b becomes 2.

Since, everything in Python is an object, the values that we pass to the function at the time of calling it are all considered object references. Hence, Python uses Pass by Object Reference while calling a function.

Arguments of a Function

Arguments are the values that are passed to the function when it is called by the driver code. Arguments can be of many types. Arguments are used to provide values according to the parameters of the function. Each parameter (input) that is defined in the function is assigned one of the argument values. Hence, a function must be called with the correct number of arguments.

Types of Arguments

There are 4 major types of arguments:

1. Required arguments (Positional Arguments) In such functions, the exact number of arguments are required to be passed in the same order as specified in the function definition. Each parameter (input) that is defined in the function is assigned the argument values given in order.

2. Keyword arguments Keyword arguments eliminate the need to remember the order of parameters to be passed to the function by specifying the name of the argument.

3. Default arguments A default argument is a parameter that is passed to a function with a default value i.e. if no value is provided for that argument, it will assume the default value provided.

4. Variable-length arguments When the exact number or type of arguments is not known, we use arguments with variable lengths. This is done with the help of two special symbols:

  • *args (Non-keyword arguments)
  • **kwargs (Keyword arguments)

Let's look at some examples to better understand the working of functions in Python.

Example

Example 1 : Python Function with arguments

Code:

Output:

Example 2 : Adding Docstring to the function

Docstring describes the functionality of a function. It is an optional string that describes all the details of what the function can do and all the parameters it requires.

Code:

Output:

Example 3 : Python Function Return Statement

We can use the return statement of a function to send a value to the calling code. In the given example, we are checking whether a number is even or not. If the number is even, the function returns True, otherwise, it returns False.

Code:

Output:

Example 4 : Recursion Example

Recursion is a process in which a function calls itself. Such functions that call themselves are known as recursive functions. A Recursive function consists of two parts:

  • Base Case - This statement is used to terminate the recursive function.
  • Recursive Case - This statement is used to call the same function with different parameters until the base case is satisfied.

Code:

Output:

Example 5 : Python Function within Functions

Code:

Output:

Conclusion

  • Function refers to a named sequence of statements or a piece of code that performs a specific task.
  • Functions promote code reusability and help us to develop organized code that is easy to read, maintain and debug.
  • We can create functions in Python using the def keyword.
  • Docstring is an optional string that describes the functionality of a function.
  • To use functions in Python, we first have to create them and then call them using arguments and parenthesis.
  • Arguments are the values that are passed to the function when it is called by the calling code.
  • Arguments are passed by Object Reference in Python.
  • Arguments are of 4 types: Required, Keyword, Default, and Variable-length.
  • Functions can be recursive too.

Read More: