Arguments

In the last section we talked about functions as a way to reuse code, for example to greet someone.

If this greeting can vary depending on who it's for we can still use the same function. We're going to see an example here.

Arguments

Consider the print function. It technically performs the same task every time, but it may print different messages every time. In the same way, we can vary our function code with the help of one or more arguments. These arguments are written inside the parentheses of the function head, and can then be used as variables inside the function:

def greet(name):
    print("Welcome, " + name)

When we call this function we must now always include a name between the parentheses:

greet("Marcus")
greet("Janne")

The argument name is a variable that only exists within that function, and its value is decided when the function is called.

We can also have more than one argument (randint, for example takes two):

Return value

A function can also produce a result that it returns back to wherever it was called. Consider random.randint - we provide it with two arguments (a smallest and largest value) and we get something back - a random number inside that interval that can be stored in a variable.

We then say that the function returns or gives a return value. The keyword is return.

Let's take an example from mathematics: The average of two values \(a\) and \(b\) is computed with the formula \(\frac{a+b}{2}\). We can keep track of this in a function:

def average(a, b):
    return (a+b)/2

We can then use it in a program like this:

print("Enter your age and then a friend's age:")
age_1 = int(input("Age 1: "))
age_2 = int(input("Age 2: "))
result = medelvärde(age_1, age_2)
print(result)

Try it out! Can you also create a function that returns the average of three numbers? (You'll need a function with three arguments)

The 'None' datatype

If we try to receive something from a function that doesn't return anything, or if we forgot the return keyword in our function, we can see a special value called 'None':

def greet():
    print("Hello!")
result = greet()
print(result)

Python will call the function greet(), which prints "Hello" to the screen. After that, the variable result is created and stores whatever is returned from the greet function. But since nothing is returned, the variable is technically empty. This is marked by 'None', a special datatype which just means the absence of anything else.

Read page in other languages

In this video, we create more advanced functions using arguments and return values, allowing us to simulate the roll of dice with different number of sides.

  • Argument: One or more variables that must be included when a function is called. A function could be without arguments, i.e. does not need to have arguments.
  • Return value: A value that the caller of a function gets back when it is done. Randint is an example of a function that gives a return value.
  • None: A special data type with only one value and with one purpose, to denote nothing.
  • Function head: Describes the name of the function and how it can be used.
Difficulty level