CS 100

Logo

Scientific Computing

Activity 5 - Practice with functions

Download demo_functions.py and open it in Thonny. Save it to your cs100/ch1 folder. Run the program by clicking the green ‘play’ button. You should see the following as a result in the shell area:

Hello students
3566.5

Remember that if you want to use comments to silence out parts of Python instructions, use #. For example,

#print("This is commented out by the # symbol")
print("This is not commented out")
print("This also is not commented out") # but this part is a comment

Part 1: Answer the following questions.

Write your answer at the top of the file as a comment.

  1. Which function has parameters? What are the parameters?
  2. Which function returns a value?

Part 2: Investigate the ways you invoke/call a function that has a return and a function that does not have return.

Experiment calling hello() function in several ways.

  1. Write in comments, what is the output as a result of the function call:
    hello()
    
  2. Write in comments, what is the output as a result of the function call:
    print(hello())
    
  3. Write in comments, what do you think happened? What is different? Where is None coming from?

Now, experiment calling avg() function in several ways.

  1. Write in comments, what is output to the shell as a result of:
     avg(4,8)
    
  2. Write in comments, what is output as a result of:
    print( avg(4,8) )
    
  3. Write in comments, what is output as a result of:
    rez = avg(4, 8)
    print(rez)
    
  4. Write in comments, what do you think happened? What is different?

Part 3: Define and test (call) the following functions.

  1. Modify the existing hello() function definition so that it appears as follows. Test the function to see that it works (i.e., run the program again)
    def hello():
       print("Hello students")
       print("Oh hi")
       print("How are you?")
       print("I'm good thanks.")
    
  2. Add code to compute the average of 50 and 75 and print the result.
  3. Define a function prod that takes two numbers as input and returns their product.
  4. Add the following function definition.
    def mystery():
       print("A")
       print("B")
       return "123"
       print("C")
       print("D")
    
  5. Call the function as follows. What is output to the shell? Explain in comments.
    myResult = mystery()
    print(myResult)
    

Part 4: Optional - if you have time

  1. Write a function weirdSum that takes four numbers as input and returns the sum of the first two multiplied by the sum of the last two.
  2. Write a function secondsToday with no parameters that prints out the number of seconds in a day (note: there are 24 hours in a day; 60 minutes in an hour; and 60 seconds in a minute). When called, your function should produce the following output:
    IN A DAY THERE ARE: 86400 seconds.
    
  3. Write a function that solves a generic quadratic equation of the form ax2 + bx + c = 0, where a, b, and c are passed as parameters. Note: to compute square root of 7 you may use:
    import math
    result = math.sqrt(7)
    

How to submit

Make sure you saved your work and that it runs without errors. Submit your demo-functions.py file to Activity 5 on Moodle.