Drop links or images here to add them to the editor.

The sub programs you have seen so far are sealed boxes. You call one, it does its job, and that is the end of the conversation. Neither side passes anything to the other.

Most useful sub programs are not like that. They need data to work on, and they have an answer to hand back. This activity covers both directions of that exchange: getting data out of a sub program with return, and getting data in with arguments.

return sends data back

A sub program can send a piece of data back to the main program as its final action. The keyword for that is return:

def adder():
    num1 = 10
    num2 = 15

    return num1 + num2

return num1 + num2 works out 10 + 15 and hands the answer, 25, back to whoever called adder. It also ends the sub program there and then: any lines after a return never run.

Store the returned value in a variable

The value comes back to the exact spot where the call was written, so a bare adder() on a line of its own throws the answer away. Store it in a variable instead:

def adder():
    num1 = 10
    num2 = 15

    return num1 + num2

output_num = adder()

print(output_num)
25

Read the output_num = adder() line right to left. adder() runs the sub program, the sub program returns 25, and the = stores that 25 in output_num. From then on output_num is an ordinary variable holding an ordinary number, and you can do anything with it that you could do with a number you typed yourself:

output_num = adder()
print("The total is " + str(output_num))
The total is 25

The str() is there for the same reason as always: you cannot join a number onto a piece of text without converting it first.

A sub program that returns a value is called a function. It is still a sub program, so everything from the previous activity still applies. “Function” is just the name for the kind that hands something back.

The two words get used loosely in other places, and other programming languages draw the line in slightly different spots. In this course the rule is simple: if it has a return, it is a function.

Arguments put data in

Data can travel the other way too. To let the main program hand something to a sub program, put a name inside the brackets of the def line:

def add_five(num1):
    print(num1 + 5)

add_five(42)
47

num1 is a parameter: a name the sub program uses for a piece of data it has been given. You can think of it as a variable that only exists inside the sub program, and that gets filled in fresh every time the sub program is called.

The 42 in add_five(42) is the argument: the actual data being passed in, just as 1 and 10 are the arguments in random.randint(1, 10). When the call runs, the argument 42 is put into the parameter num1, and the body works with it from there. Call add_five(100) instead and the same body prints 105, without a single character of the sub program changing.

A sub program can have as many parameters as it needs. Separate them with commas inside the brackets, on the def line and at the call:

def add_two_numbers(num1, num2):
    return num1 + num2

answer = add_two_numbers(3, 4)
print(answer)
7

The order matters. The first argument goes into the first parameter, the second into the second, and so on down the list.

Passing in what the user typed

An argument does not have to be a value you typed into the code. It can be anything that has a value, including a variable, so you can collect something from the user and pass that straight into a sub program:

def add_five(num1):
    print(num1 + 5)

print("Enter a number:")
user_input = int(input())
add_five(user_input)

There are three steps here, and it helps to say them separately. First the print asks the question, then input() collects what the user typed, int() turns that text into a whole number, and the = stores it in user_input. Then add_five(user_input) calls the sub program, and the value inside user_input is passed into the parameter num1.

Run it a few times with different numbers. The sub program never changes, but its answer does, because the data going in is different every time. That is the whole point of arguments.

Help! My code doesn't work!

Make sure that you check for the following things:

  • The sub program name is spelled identically everywhere it is used. Capital letters count.
  • There is a colon at the end of the def line.
  • The body of the sub program is indented, and every line of it by the same amount. Never mix tabs and spaces in one file.
  • The call is not indented inside the sub program.
  • The sub program is defined before it is called.
  • The number of arguments at the call matches the number of parameters on the def line, and they are in the same order.
  • A returned value is stored in a variable, or used straight away. A return on its own does not print anything.
  • Numbers are converted with str() before they are joined onto text with +.