login

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 print function to output a message on screen. A request to execute a function is known as function call. Beside built-in function, Python also allows you to define your own function. The syntax of defining a function is very simple as follows:

def function-name(parameters):
  function-body

You use keyword def to define a function followed by function name and parameters.
Parameters are optional part of function. A function can have parameters or no parameters based on its designation. If you have multiple parameters, you need to separate them 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 some logic based on passed arguments.

Let’s take a look at an example of define a very simple function to define power function:
 

def power(x,n):
    result = x
    for i in range(1,n):
        result = result * x
    return result

A function in Python always returns result value which either value or None. If you want to return value, you use the return statement followed by a value. A function can have multiple return statements. Whenever the 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:

function_name(arguments)

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 function power we’ve defined above:
 

a = power(2,3)
print a
b = power(x=10,n=3)
print b

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 (,).
Formal parameter is a parameter that requires you to pass argument to them. This is mandatory.

Optional parameter is not required you to pass argument to it. If you don’t pass argument to it, the function use default value of that parameter. The optional parameter is defined by using following pattern:

parameter=value

Let’s take a look at an example of using formal and optional parameters:

def increase(x,y = 1):
    return x + y

# call function without passing optional parameter y
for k in range(1,10):
    ret2 = increase(k)
    print(ret2)

# call function passing optional parameter y
for k in range(1,10):
    ret2 = increase(k,10)
    print(ret2)

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 don’t pass argument to the optional parameter, so the function uses 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 uses it to increase k by 10.

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