---
# System prepended metadata

title: The `return` statement

---

# The `return` statement

([home](https://github.com/alexhkurz/introduction-to-programming/blob/master/README.md) ... [previous](https://hackmd.io/@alexhkurz/Hy48XsvpI) ... [next](https://hackmd.io/@alexhkurz/rJjfXqS08))


The purpose of a function is to **encapsulate** code that can be executed repeatedly. A function is an abstraction that **hides** details. Often, functions written by one programmar are used by another one who does not know the details of how the function is implemented. Examples of such functions we used are

    print, matplotlib.pyplot.show, matplotlib.pyplot.plot, Decimal, Fraction, ... 
    
as well as arithmetic such as

    +, *, -, /, //, mod, **, ... 
    
Examples of functions we wrote ourselves are

    parity, fib, ...
    

How do functions communicate with their environment?

The "environment" calls the function, see `f()` in the example below. Then the functions is executed. There are (at least) two ways a function can communicate back to the environment:

Printing an output or returning a value.

## The `print` statement

While running it can print something. For example,

	def f():
	    m = 0
        print(m)
        m = m+1
        print(m)
        m = m+2
        print(m)
    f()
    
prints the first 3 values of the sequence of triangular numbers. 
    
**Exercise:** Run the program above.
    
You see that the function can continue printing while it is running. We say that printing is a **side effect**. The program would run in exactly the same way if we took out the print statements, it would just not print. 
    
**Exercise:** Modify the function so that it prints the first $6$ elements of the sequence.
    
If your solution gets longer the more elements you want to print, you should do the next exercise. (We have done this already back in the session on the [`for` loop](https://hackmd.io/@alexhkurz/H1o4Mcr6L), but it can't harm to revise this.)
    
**Exercise:** Modify the function so that it has only one assignment in a for-loop and prints the sequence up to some given input `n`.

Printing has the disadvantage that the printed value cannot be processed further in the program. The printed values are "lost to the environment", so to speak. For example, if we want to do recursion as in 

    fib(n) = fib(n-2) + fib(n-1)
    
we need `fib(n-2)` `fib(n-1)` to return the respective values so that they can be added.

## The `return` statement

A function can also return a value. Returning a value automatically terminates the function. (After the function finds one value it stops.) To see that this is the case run the following program. 

	def f():
	    m = 0
        return m
        m = m+1
        return m
        m = m+2
        return m
	            
    print(f())
        
What is the result? 


**Remark:** While a function can continue to print and execute, it cannot return and continue to execute. 

If we want to return the $n$-th value of the sequence, we can write

	def f(n):
        sum = 0
        for i in range(0,n+1):
            sum = sum + i
	    return sum
                        
We see that there is only one return statement and that it is outside of the loop. If we want to print the $6$-th element of the sequence we call the function as `print(f(5))`.
    
## Summary
    
In general, functions may
- only have side effects (like `print`) and not return a value,
- only return a value and have no side effects,
- have side effects and return values.
    
**Activity:** Classify the functions mentioned in the introduction according to the three cases of the previous remark. Write three functions that are examples of each of the three cases.


    
