## CS 115 Lab 8 Functions and Return values (Individual)

### 40 Points

This is an individual lab. Every student is expected to do the activity by themselves and submit it by Sunday October 29 midnight.

Educational goals of this lab - verify that every student can

• write a function definition
• call a function properly which returns a value and which does not return a value
• understand arguments and parameters
• understand scope

INSTRUCTIONS:

• (40 points) Individual problem: Functions and return values
• This exercise is about how functions return values to their caller. You use the return statement inside the function. Printing to the screen is NOT returning the value to the caller.
• A particular equation known as a logistic curve is often called a sigmoid curve and is used to express population growth. The letter e is a known constant (Euler's number) with a value of approximately 2.71828... Hint: the constant e is available from the math library (just like pi). The graph of the logistic curve looks like this.
• (15 points) Part 1: Write a function called pop1 which will calculate the value of this equation for a given value of t. T is the parameter of the function. This function outputs to the screen the value of the equation. It does not return any value to the calling function. Use this function with the main program below. Add code to the main program so the function is called properly. When run, it prints out the values of t from -6 to 6, with the value of the equation at each value of t.
```def main():
for t in range(-6, 7):
print(t, end=" ")
# put a line here that calls pop1 with t as the
#  argument

main()
```

You can check your results by looking at the given graph.
• (20 points) Part II:
In the same program, write another function called pop2, which will calculate the same equation for a given value of t. But it does not output anything to the screen at all. Instead it returns the value calculated, using a return statement. Add the code below to your main function, just after the loop given above. (In other words, leave the pop1 function in your program also.) Your main function is going to use pop2 to calculate the total of the function values over the numbers from -6 to 6.
```     # initialize total
for t in range(-6, 7):
# put a line here which calls pop2 with t
# and puts the value into a variable called result
# add the result to the accumulator, total
print(t, result)
print("Total is", total)
```

Hint: total should be 6.5.

• You should end up with one program, which has 2 pop functions (pop1 and pop2) and a main function. The main function has 2 loops in it. Each loop uses one of the pop functions.
• Do put in a header with name, section, email. The three P's are not necessary.
• (5 points) Add in a comment at the bottom of your program to answer this question:
Why was the second loop (using pop2) able to calculate the total of the values of the function, while the first loop (using pop1) was not able to?
• Submit your finished program with the link at the top. Use the Lab 8 and Code menu choices.

Log off properly - you don't want your account misused by someone else!

Remember not to leave files on the local hard drives in this lab or anywhere else on campus! Make sure you save your projects on a portable storage device you take with you!