# 2. Functions

• Feel free to use your laptop if you have it

• Ensure I have recorded your completion — failure to do so will result in a grade of 0

• I strongly encourage you to work with others in the lab

• When you get stuck, do me a favour and ask those sitting around you for help

• I want people to get used to working together in the labs

• Peer teaching and peer learning is super effective

Note

To obtain full marks for the lab, you must:

1. Have completed the pre-lab exercises

2. Have been working on the lab content

3. Demonstrate competency in the topics

## 2.1. Pre Lab Exercises

Warning

You must have completed the specified exercises prior to the start of the lab. If you have not come to lab prepared, you will be asked to leave and you will obtain a grade of 0 for the lab.

1. Chapter 2 exercise(s)

• 5 but have it in a function that returns the result

2. Chapter 4 exercise(s)

• 8

## 2.2. Before Kattis

1. Write a function called `add_five_print(some_integer)`

• The code you write must be within a function

• The function will take one parameter `some_integer`

• The function adds five to the parameter and `print`s out the result

• The function will not return anything

• Call the function a few times to verify that it works properly

```1def add_five_print(some_integer):
2    # stuff goes here
```
2. Write a function called `add_two_numbers_print(some_integer, some_other_integer)`

• The code you write must be within a function

• The function will take two parameters `some_integer` and `some_other_integer`

• The function will calculate their sum and `print` out the result

• The function will not return anything

• Call the function a few times to verify that it works properly

```1def add_two_numbers_print(some_integer, some_other_integer):
2    # stuff goes here
```
3. Write a function called `add_two_numbers_return(some_integer, some_other_integer)`

• The code you write must be within a function

• The function will take two parameters `some_integer` and `some_other_integer`

• The function will calculate their sum

• The function will `return` the result

• Call the function a few times to verify that it works properly

4. Run the following code and take note of the output

```1result = add_two_numbers_return(4, 5)
2print(result)
```
5. Run the following code and take note of the output

```1result = add_two_numbers_print(4, 5)
2print(result)
```
6. Why do these two functions behave differently when called?

• Take note of when and where `print` is called

7. Write a function called `this_is_tough(n1, n2, n3, n4)`

• The code you write must be within a function

• This function will take four parameters `n1`, `n2`, `n3`, and `n4`

• This function will sum all four numbers

• The function will `return` the result

• You can not make use of the addition operator (`+`) directly within this function

• You must make use of `add_two_numbers_return` three times

• Verify correctness by running the function a few times

## 2.3. Kattis Problems

• Do not forget the code we used last time to read input on Kattis

```1data = input()       # Read a WHOLE, SINGLE line of input
2data = data.split()  # Split string into individual pieces
3a_var = int(data) # Take string from data[X], convert it to int...
4b_var = int(data) # ... And store it in some variable
```

Warning

The above code will only work when the input is 2 integers on the same line. You may need to hack this code to make it work for your particular problem.

• Skip any of the following problems if you did them already.

Warning

Ensure that your your completion has been recorded. Failure to do so may result in a grade of 0.