Functions in Python with Examples

A function in Python is an organized block of reusable code, which avoid repeating the tasks again and again. If you want to do a task repeatedly in a code, then just make a function, set the task in it and call the function multiple times whenever you need it. Functions in Python begins with the keyword def and is followed by the name of the function and parentheses.

Create a function in Python

Functions in Python, use the def keyword as in the below syntax:

Above,

  • The def keyword is used to define a function name,
  • The parameters are optional to add,
  • To return a value, the return statement is used.

Note: We will run below example programs in Python to understand the concept of Functions in Python.

Call a function in Python

Code is structured when we define a function. Execution of this function is done by calling. Functions in Python work the same as in any other programming language. To call, the function name is followed by parentheses.

Let us see an example and create a function demo and call it in Python:

The output is as follows:

In the above example, we created a function named demo()  and added two print statements to it. The text in this statements is visible only when the demo() function is called:

Function Parameters

The names entered in the function at the time of defining it, are Function Parameters. Adding Parameters to a function in Python is optional. Let us see the syntax for Function Parameters in Python:

Above, parameter1 and parameter2 are the parameters which is added to the function while defining it. Let us now see the above example and add a parameter to it:

The output is as follows:

The above function takes string str as a parameter. The string “Device:” is passed along the device name i.e. Laptop and Desktop.

Function Arguments in Python

In Python,

  • Required arguments
  • Keyword arguments
  • Arbitrary Keyword Arguments
  • Default arguments
  • Variable-length/ Arbitrary arguments

Let us learn about the above types of Function Arguments, one by one,

Required arguments in Python

The required arguments as the name suggests are the required number of arguments to be passed on a function call. When you call a function, the arguments in the call are to match with the function definition for the program to run correctly. An error occurs even if the position of the argument is changed.

Let us see an example wherein we have two arguments and we are calling with the same number and position of arguments:

The output is as follows:

Keyword arguments in Python

Call a function with keyword arguments in Python. Through this, you can skip arguments or even pass them in any order while calling a function. As we saw above, this is definitely different that Required arguments wherein the order in which the arguments are passed is the same while calling.

Let us see an example wherein we are calling the function with two arguments passed in random order:

The output is as follows:

Let us see another wherein we are calling the function with three arguments passed in random order:

The output is as follows:

Default arguments in Python

While calling a function, if the value isn’t passed, a default value is assumed for that argument. Let us see an example and display the default value. We have a function below with total 2 arguments:

The output is as follows:

Let us now see another example wherein the default value is assumed for the argument, whose value isn’t specified. We have a function below with total 3 arguments:

The output is as follows:

Variable-length/ Arbitrary arguments in Python (*args)

Arbitrary arguments work when you have no idea about the number of arguments to be passed. These are variable length arguments, allowing you to pass any number of arguments, defined as * (asterisk). That means, include a * before the parameter name while defining the function. Let us now see an example:

The output is as follows:

We passed *sports as variable-length/arbitrary argument above.

Let us see another example, wherein we will use for loop to display all the variable-length argument values:

The output is as follows:

Arbitrary Keyword Arguments in Python (**kwargs)

Add two asterisks ** before the parameter name in a function definition, if you don’t know in advance how many keyword arguments will be passed to your function. A dictionary format gets created for such arguments i.e. a Dictionary of arguments

Let us see an example:

The output is as follows:

Recursion in Python

When a function calls itself, it is called Recursion.  In another sense, with Recursion, a defined function can call itself. Recursion is a programming approach, which makes code efficient and reduces LOC. Let us see an example wherein we will be calculating factorial with Recursion:

The output is as follows:

The above program calls in a recursive way. To get the value of 7!, the function calc() works like:

Recursion Functions in Python

The return statement in Python

The return statement in Python is used to terminate/exit the function and is placed at the end. It can have an expression or can be used without any expression. A return statement with an expression gets evaluated first and then returned to the function called. However, a return statement with no expression returns None. Let us now see an example:

The output is as follows:

Let us see another example:

The output is as follows:

Let us now see the above Python program without using a return statement, since using a return is optional:

The output is as follows:

In this lesson, we learned Functions in Python with several examples. We also saw how to work with function arguments, including default, required, keyword, arbitrary arguments, etc.

Python Tutorial (English)

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Read More

Python DateTime Tutorial with examples
Python Multiple Choice Questions (MCQs)
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading