NEPTUNIAN ANALYTICS

Functions

In programming, a function is a block of code that performs a specific task. For example, you can have a function that takes 2 values and multiplies them. Functions are often used to improve code reusability.

Imagine you want to create a program, but to create that program you have to perform the same task multiple times. Instead of writing out the full code multiple times, you could write a function and simply call that function when needed.

Python has many built-in functions. Some of which you are already be familiar with. For example, print is an example of a built-in Python function. Click here for a full list of Python's built-in functions.

User-defined functions

As well as Python's built-in functions. You can also write your own functions. The syntax to write a function is shown below:

def function_name(function_parameters):
   #action to perform
   return value

As shown in the example above, you create a function in Python using the def keyword. You also indent the line of code following the colon. There is the optional keyword return; this specifies the output value of the function. Once a function has been created, you can execute the code by calling the function as shown below.

function_name(function_arguments)

When defining a function, you can set parameters that serve as templates for user input. When the function is called, the values passed to it are known as arguments.

Below is an example of a function called multiplier. This function takes two arguments (a and b) and multiplies them storing the value in another variable called c. The function then returns c.

Multiplier

A function that takes two arguments, multiplies them, and then returns the result of the multiplication.

def multiplier(a, b):
   c = a * b
   return c

Now that we have created our multiplier function, we can call the function. In the below example, we will call the function using 3 and 4 as the arguments. The function will then take 3 and 4, multiply them, store the result in another variable, and then return that variable.

Calling the multiplier function

multiplier(3, 4)

Output

12

The result of calling the multiplier function was 12, as 3 multiplied by 4 is 12.

Default Function Arguments

Function arguments can have default values. In this case you do not need to pass a value to that argument. And if you don’t pass a value for that argument, the default value is taken. However, if you do provide a value for that argument, it will override the default value defined in the function. Let's see an example below.

In this example we will redefine our multiplier function, this time providing the default value 5 for parameter b.

Multiplier function with parameter b set to 5 by default.

A function that takes two arguments, one required and one optional, multiplies them and then returns the result of the multiplication.

def multiplier(a, b = 5):
   c = a * b
   return c

Now that we have redefined our multiplier function with b defaulted to 5 when we call the function, we only need to supply one value: a value for a. b will use the value 5 unless we override this by specifying a value for b.

Calling the new multiplier function without specifying b.

multiplier(3)

Output

15

In the example above the output of calling the function is 15 as the function uses the default value 5 for b.

Now, let’s try calling the multiplier function again, but this time, we will pass a value for b instead.

Calling the new multiplier function specifying a value for b

multiplier(3, 4)

Output

12

In this example, the output of calling the function was 12, as we overwrote the default argument by specifying 4 instead. Try creating your own functions and experimenting with both default and non-default arguments.