Functions

Written by Ferhawn Shaheen (edited on 8/8/2020)

Introduction

Functions are an essential aspect of Python and programming in general. They are the framework for larger and more complex programs. Up to this point, you have learned ways to make small and simple programs that probably span a few lines. With functions, you will have a strong footing into making programs that you would typically have to make for more practical purposes. Functions can bind names to a set of statements as a whole.

Built-In Functions

There are several functions that Python comes with. These functions can do a variety of tasks. Some examples include print(), sum(), min(), max(), type(), etc. These are just a small number of built-in functions, but their uses are prolific when paired together.

You should be familiar with at least one of these functions. Inside the of these functions are the arguments that a function accepts. These arguments are often restricted to specific data types. For example, abs(), which gets the absolute value of a number, only accepts numbers (float or int). Any other data type would yield an error.

Here is an example of using the min() function:

# The arguments to `min` are 1, 2, and 3.
print(min(1, 2, 3))

# Output:
1

Functions from Modules

In some cases, there will be a function that is taken from a module that has to be imported. This was already covered in the previous lesson, but it is worthwhile to briefly re-examine this. The math module in Python has many useful functions. One such function is sqrt(). Here is how it can be used.

import math
print(math.sqrt(4))

# Output:
2

# Here is another way that only gives you the sqrt() function:
from math import sqrt
print(sqrt(4))

# Output:
2

In the first example, math.sqrt() is used rather than sqrt() since this makes Python know that sqrt() is from the math module. In the second case, Python already knows that sqrt() is from the math module based on the from math import sqrt statement.

Once again, there are many built-in functions that come from modules, and it is encouraged that individuals search for some on the internet to aid them in creating specific programs.

User-Defined Functions

As the name suggests, a user-defined function is a function that does a specific task based on the definition a programmer assigns. The format is as follows:

# Note that the < and > signs would be omitted.
def <function_name>(<formal parameters>):
    return <return expression>
    
# This is an example with docstring:
def <function_name>(<formal parameters>):
    """
    docstring
    """
    return <return expression>

The key parts to note are the def keyword, the name of the function followed by parentheses and a colon, and the return statement. Without these, the function would be essentially useless. The body houses all of the code along with the return statement.

Docstring (short for documentation string) is placed immediately after the function name, and this is intended to describe what the function does. When using this, it is ideal to be succinct using only a couple of lines. The return statement simply returns a value. This is important when a function is called and you would like to return the value of something that was operated upon.

Formal Parameters

While the parentheses may be empty for some function, they often contain values that the function is to accept. By accepting these values, the function will then work with these values to then eventually return some other value. An example follows:

def add_nums(num1, num2):
    total = num1 + num2
    return total
    
# Here is a shorter way to write this:
def add_nums(num1, num2):
    return num1 + num2

In this example, two numbers, num1 and num2, are accepted as formal parameters in the add_nums() function. Both are added to return the total. The function signature, the top or first line, is where the formal parameters are taken.

Note that formal parameters can be many different values, from other functions to numbers and variables. Typically having fixed values such as a specific integer (2) is not ideal since the function is no longer as applicable.

Calling the Function

After a function is defined, it must be called in order for it to be used. Here is how a function is typically called:

# Note that the < and > signs would be omitted.
<function_name>(<arguments>)

If the function is not called, then none of the code in the body would execute.

The add_nums() function from earlier can exemplify a call statement.

def add_nums(num1, num2):
    return num1 + num2
    
print(add_nums(1, 2))

# Output:
3

You can see the binding of the arguments 1 and 2 to num1 and num2 respectively when add_nums() is called. Finally, 3, the sum, is returned to the print statement that called on add_nums(), which outputs 3.

Conclusion

In this lesson, the basics of functions were discussed. This includes both built-in functions and user-defined functions. Calling functions were discussed along with arguments and formal parameters. Functions are essential to programming and will be important moving forward.

Last updated