Python Functions

Functions are the re-usable pieces of code which helps us to organize structure of the code. We create functions so that we can run a set of statements multiple times during in the program without repeating ourselves.

Creating functions

Python uses def keyword to start a function, here is the syntax:

Note: All the statements inside the function should be indented using equal spaces. Function can accept zero or more arguments(also known as parameters) enclosed in parentheses. You can also omit the body of the function using the  pass keyword, like this:

Let’s see an example.

Expected output:

Above we define a function called sum()  with two parameters start  and end , function calculates the sum of all the numbers starting from start  to end .

Function with return value.

The above function simply prints the result to the console, what if we want to assign the result to a variable for further processing ? Then we need to use the return statement. The   return statement sends a result back to the caller and exits the function.

Expected Output:

Here we are using return  statement to return the sum of numbers and assign it to variable s .

You can also use the return statement without a return value.

Expected Output:

In python if you do not explicitly return value from a function , then a special value  None  is always returned. Let’s take an example

Expected Output

as you can see test()  function doesn’t explicitly return any value. so None is returned.

Global variables vs local variables

Global variables: Variables that are not bound to any function , but can be accessed inside as well as outside the function are called global variables.

Local variables: Variables which are declared inside a function are called local variables.

Let’s see some examples to illustrate this point.

Example 1:

Expected Output:

Example 2

Expected Output:

You can bind local variable in the global scope by using the global keyword followed by the names of variables separated by comma ( ,).

Expected Output:

Note that you can’t assign a value to variable while declaring them global .

Expected Output:

In fact there is no need to declare global variables outside the function. You can declare them global inside the function.

Expected Output:

Argument with default values

To specify default values of argument, you just need to assign a value using assignment operator.

Above function has two parameter i  and j . j  has default value of 100 , means we can omit value of j while calling the function.

Expected Output:

Expected Output:

Keyword arguments

There are two ways to pass arguments to method: positional arguments and Keyword arguments. We have already seen how positional arguments work in the previous section. In this section we will learn about keyword arguments.

Keyword arguments allows you to pass each arguments using name value pairs like this   name=value . Let’s take an example:

Mixing Positional and Keyword Arguments

It is possible to mix positional arguments and Keyword arguments, but for this positional argument must appear before any Keyword arguments. Let’s see this through an example.

You can call the above function in the following ways.

Expected Output:

Returning multiple values from Function

We can return multiple values from function using the return statement by separating them with  a comma ( ,). Multiple values are returned as tuples.

Expected Output:

In the next post we will learn about Python Loops

Leave a Reply

4 Comments on "Python Functions"

Notify of

Sort by:   newest | oldest | most voted
1 year 2 months ago

Hi, I think there should be ‘greeting’ instead of ‘message’ in the 3rd example of keyword arguments.

1 year 3 months ago

Hi. in example 2: Global vs Local variables there are 2 mistakes in comments.
it says: #Displays 2
but should: #Displays 101