Functions

Python programming
Functions
Reproducibility

This lesson explains how functions work in Python, from calling built-in functions with arguments to defining your own reusable functions to organize and simplify code.

Authors

Noor Sohail

Will Gammerdinger

Published

March 16, 2026

Keywords

Define functions, Arguments

Approximate time: XX minutes

Learning Objectives

In this lesson, we will:

  • Describe and utilize functions in Python.
  • Modify default behavior of a function using arguments.
  • Identify Python-specific sources of obtaining more information about functions.
  • Demonstrate how to create user-defined functions in Python

Overview of lesson

Functions allow you to bundle up a task so that you can use it again and again. In real projects, you might write a function to clean a dataset or run a standard calcluation. Then, you can call that function on any multitude of datasets without having to rewrite the code. This keeps your code shorter, clearer, and easier to fix. It is very easy to make avoidable mistakes when you are copy-pasting code! Functions help you avoid that by letting you write the code once and reuse it as many times as you want.

In this lesson, you will learn how to call built‑in functions and define your own, so you can start building a small “toolbox” tailored to your own needs.

What are Functions?

A key feature of Python is functions. Functions are “self contained” modules of code that accomplish a specific task. Functions usually take in some sort of data structure (value, vector, dataframe etc.), process it, and return a result.

We have actually been using several functions already!

  • print(): takes in a value and prints it to the console
  • len(): takes in a value and returns its length
  • type(): takes in a value and returns its type

The general usage for a function is the name of the function followed by parentheses:

# DO NOT RUN
# Example syntax for using a function
function_name(input)

The input(s) are called arguments, which can include:

  • the physical object (any data structure) on which the function carries out a task
  • specifications that alter the way the function operates (e.g. options)

Most functions can take several arguments. If you don’t specify a required argument when calling the function, you will either receive an error or the function will fall back on using a default.

The defaults represent standard values that the author of the function specified as being “good enough in standard cases”. An example would be what symbol to use in a plot. However, if you want something specific, simply change the argument yourself with a value of your choice.

Basic Functions

Since Python is used for statistical computing, many of the base functions involve mathematical operations. For example, the sum() function takes in a vector of numbers and returns their sum:

# Sum of numbers
numbers = [2, 2.23, 0.77, 10, 40.2]
total = sum(numbers)
print(total)
55.2

Another example is the round() function, which rounds a number to a specified number of decimal places:

# Rounding a number
num = 3.14159
rounded_num = round(num)
print(rounded_num)
3

The output we get is 3, which is the default behavior of the round() function. However, if we want to round to a specific number of decimal places, we would need to specify more information, likely in the form of an argument.

Seeking Help on Arguments for Functions

If you want to know more about the arguments that a function takes in, Python’s built-in help() function is a great first step. This function provides documentation for any function you pass to it. For example, help(round) will give you information about the round() function, including its arguments and usage.

help(round)
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.
    
    The return value is an integer if ndigits is omitted or None.  Otherwise
    the return value has the same type as the number.  ndigits may be negative.

From this output we can see that the round() function takes two arguments: number (the number to be rounded) and ndigits (the number of decimal places to round to). The ndigits argument has a default value of 0, which is why we got 3 when we ran the example without specifying it.

  1. Round the num variable to 2 decimal places using the round() function.
  2. What happens when you specify a negative value for the ndigits argument in the round() function? Try it out and explain the output.
  3. What happens if you apply the round() function to the numbers list? Try it out and explain the output.
  4. Apply the sorted() function to the numbers list. What does it do? What are the arguments for the sorted() function? Use the help() function to find out.

Methods for Data Structures

When we were working with lists earlier, we made use of the list.append() function. This is an example of a method, which is a function that is associated with a specific data structure. In this case, the append() method is associated with lists and allows us to add an element to the end of the list.

Lists are not the only data structure that has methods. For example, strings have a method called upper(), which converts all characters in the string to uppercase:

my_string = "hello world"
my_string.upper()
'HELLO WORLD'

Each data structure has its own set of methods that are designed to perform specific tasks related to that data structure. To find out what methods are available for a particular data structure, you can use the dir() function. For example, dir(list) will show you all the methods that are available for lists.

dir(list)
['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

So now we can see that there are many methods available for lists, including list.sort(), list.reverse(), etc. Let us try out this sorting method on the numbers list.

numbers.sort()
numbers
[0.77, 2, 2.23, 10, 40.2]

In the exercise, we used the sorted() function to sort this list in both ascending and descending order with . However, there is also a list.sort() method that can be used to sort a list in place. The difference between the two is that sorted() returns a new sorted list, while list.sort() modifies the original list.

Even though there are slight differences between these two functions, they are both ultimately accomplishing the same objective of sorting the list. This is a good example of how there can be multiple functions that accomplish the same task, but they may have different arguments or operate in slightly different ways.

Most importantly, this is another reminder that there is no one “right” way to do something in Python!

User-defined Functions

One of the great strengths of Python is the user’s ability to add functions. Sometimes there is a small task (or series of tasks) you need done and you find yourself having to repeat it multiple times. In these types of situations, it can be helpful to create your own custom function. The structure of a function is given below:

def function_name(argument1, argument2, ...):
    # Code block that defines what the function does
    return output

When defining the function you will want to provide the list of arguments required (inputs and/or options to modify behaviour of the function). The argument(s) can be any type of object (like a scalar, a matrix, a dataframe, a vector, a logical, etc), and it’s not necessary to define what it is in any way.

Then, indented under the function definition, you will write the code that carries out whatever task the function is designed to do. This is where the function is executing code on the arguments supplied.

Finally, you can “return” the value of the object from the function, meaning pass the value of it into the global environment. The important idea behind functions is that objects that are created within the function are local to the environment of the function – they don’t exist outside of the function.

Creating a Function

Let’s try creating a simple example function. This function will take in a numeric value as input, and return the squared value.

# Create a function called square_it which takes the value x as input 
def square_it(x):
    # Squares the value of x and assigns it to the object called square
    square = x * x
    # Returns the value of square to the console
    return square

Now, we can use this function as any other base R functions. We type out the name of the function, and inside the parentheses we provide a numeric value x:

square_it(5)
25

Pretty simple, right? In this case, we only had one line of code that was run, but in theory you could have many lines of code to get obtain the final results that you want to “return” to the user.

  1. Write a function called multiply_it, which takes two inputs: a numeric value x, and a numeric value y. The function will return the product of these two numeric values, which is x * y.

For example, multiply_it(x=4, y=6) will return output 24.

More Practice with Functions

  1. Custom Functions - Create a function temp_conv(), which converts the temperature in Fahrenheit (input) to the temperature in Kelvin (output). Let’s perform a two-step calculation:
  • first convert from Fahrenheit to Celsius, then
  • convert from Celsius to Kelvin
# The formula for celsius to farenheight: 
temp_c = (temp_f - 32) * 5 / 9

# The formula for celsius to kelvin
temp_k = temp_c + 273.15

Test your function. If your input is 70, the result of temp_conv(70) should be 294.2611.

  1. Nesting Functions - Now we want to round the temperature in Kelvin (output of temp_conv()) to a single decimal place. Use the round() function with the newly-created temp_conv() function to achieve this in one line of code.

If your input is 70, the output should now be 294.3.


Next Lesson >>

Back to Schedule

Reuse

CC-BY-4.0