Python Function

Define a function

Function is a reusable piece of code which handles one or several programming tasks. Python provides many built-in functions which allow you to use it anytime. A very basic function you’ve seen so far is the print() function that  output a string on screen. A request to execute a function is known as function call.

Besides built-in function, Python also allows you to define your own function. The syntax of defining a function is very simple as follows:

  • You use keyword def to define a function followed by function name and parameters.
  • Parameters are optional part of the function. A function can have many parameters or no parameter based on its designation. If a function has multiple parameters, each parameter is separated by a comma. When you call a function, you supply the values known as arguments corresponding to the parameters.
  • You define a function body which is reusable code to implement  logic based on passing arguments.

Let’s take a look at an example of defining a very simple function called power():

A function in Python always returns result value which is either a value or None. If you want to return value, you use return statement and a value. A function can have multiple return statements. Whenever the  first return statement is reached, the function stops and returns the value immediately.

Calling a function

To call a function, you can use the following syntax:

In the argument list, you can either specify the name of parameters with values or only arguments. Each argument separated by a comma (,). You pass the arguments in the order that it appears in the parameters list.

Let’s take a look at an example of calling power() function we’ve defined above:

Parameters in function

A function can have multiple parameters divided by so called formal parameters and optional parameters. Each parameter has to be separated by a comma (,).

  • A formal parameter is a parameter that requires an argument to pass to it.
  • An optional parameter is not required to pass argument to it. If you don’t pass an argument to it, the function use default value of that parameter. The optional parameter is defined by using following syntax:
Let’s take a look at an example of using formal and optional parameters:

We’ve defined a function called increase() with two parameters. The first parameter is x which is a formal parameter. And the second parameter is y which is an optional parameter. In the first function call, we didn’t pass argument to the optional parameter, so the function used default value to increase the k by 1. In the second function call, we specified the value for the optional parameter, at this time function used the optional parameter to increase k by 10.

You’ve learned how to define a function with formal and optional parameters. You also learned how to call a function. In the next tutorial, you will learn more advanced concepts of function and how it use effectively in Python.

  • Was this tutorial helpful ?
  • YesNo