September 14: Functions

Learning Objectives

After today's class, you should be able to:

  • Implement short functions that solve logic problems.
  • Write short functions that have parameters and return values.
  • Replace existing duplicate code with equivalent function calls.

Lesson Outline

Quiz 1B: 25 minutes

  • Log in as student on the lab machine
  • Open web browser, go to canvas and log in with your eid
  • In canvas, go to Modules, Quiz1b
  • Do part one of the Module
  • The programming portion is given on sheet of paper
  • Open Thonny
  • Submit work using Canvas with Gradesope, Module Quiz1, Programming

Check this out - coding bat logic challenges

Mini Lecture with inclass activities:

  • Functions
  • Use descriptive names for functions and variables
  • Docstring Requirements for Functions
    """Example unit conversion functions.
    
    Author: Sharon Simmons
    Version: 09/13/2023
    """
    
    
    def c_to_f(celsius):
        """Convert celsius to fahrenheit.
    
        Args:
            celsius (float): degrees in celsius
    
        Returns:
            float: degrees in fahrenheit
        """
        return (celsius * 1.8) + 32
    
    
    def f_to_c(fahrenheit):
        """Convert fahrenheit to celsius.
    
        Args:
            fahrenheit (float): degrees in fahrenheit
    
        Returns:
            float: degrees in celsius
        """
        return (fahrenheit - 32) / 1.8
    
    
    if __name__ == "__main__":
    
        f = f_to_c(30)
        c = c_to_f(30)
    
        print(f"30 degrees F = {f:5.1f} degrees C")
        print(f"30 degrees C = {c:5.1f} degrees F")
    

Your To-Do List

By today

Complete by Monday September 18th

Due Wednesday September 20th at 11pm