Week2 Lecture1
Week2 Lecture1
def function_name(parameters):
Indent body
return (parameter1,
parameter2, …)
What is in the body?
What are we
returning?
Function Definitions def function_name(parameters):
body
my_function()
name is local to the function
print(name) and not accessible outside.
celsius = convert_to_celsius(32)
celsius = convert_to_celsius(212)
celsius = convert_to_celsius(98.6)
Design Recipe
§ Write a function that converts from Fahrenheit to Celsius.
2. Type Contract (Specify the type(s) of parameters and return values)
def convert_to_celsius(degrees_f):
""" What types are
... passed in?
"""
... Do something
... Do something
... Do something
return degrees_c
Design Recipe
§ Write a function that converts from Fahrenheit to Celsius.
4. Description (Write a short description of what the function does)
def convert_to_celsius(degrees_f):
"""
(number) -> number
Return the temperature in degrees Celsius corresponding to
the degrees Fahrenheit passed in.
"""
... Do something
return degrees_c
Design Recipe
§ Write a function that converts from Fahrenheit to Celsius.
5. Body (Write the code that actually does the thing that you want)
def convert_to_celsius(degrees_f):
"""
(number) -> number
Return the temperature in degrees Celsius corresponding to
the degrees Fahrenheit passed in.
"""
return degrees_c
Design Recipe
§ Write a function that converts from Fahrenheit to Celsius.
6. Test (Verify the function using examples)
- Run all the examples that you created in Step 1.
- Testing is so important.
- In industry, you’ll be expected to provide tests for everything.
celsius = convert_to_celsius(32) # celsius should be 0
Design Recipe
§ How do we do about writing a function?
§ You should follow these six steps.
Open your
1. Type
2. Contract
notebook
3. Header
4. Description Click Link:
4. Design Recipe
5. Body
6. Test
Docstring
§ A Python documentation string, commonly known as docstring,
helps you understand the capabilities of a function (or module, class).
def convert_to_celsius(degrees_f):
"""
This is the (number) -> number
docstring Return the temperature in degrees Celsius corresponding to
the degrees Fahrenheit passed in.
"""
return degrees_c
Docstring
§ As we saw before, help() prints information about a function.
§ The help function actually prints out the “docstring” that we write as
part of a function definition.
§ For the function we just wrote, we could type:
help(convert_to_celsius)
>>>
Help on function convert_to_celsius in module __main__:
convert_to_celsius(degrees_f)
(number) -> number
Return the temperature in degrees Celsius corresponding to the degrees
Fahrenheit passed in
Docstring
§ These are the most popular Docstrings format available.
Click Link:
6. Breakout Session 1
https://utoronto.zoom.us/my/bussmann
https://utoronto.zoom.us/my/bussmann
Design is iterative.
users
A Design Process for Programming
§ In the next lecture, we are going to talk about a detailed design
process for programming, based on the engineering design
processes that are key to any engineering.
§ The steps are as follows:
§ Define the Problem.
§ Define Test Cases.
§ Generate Multiple Solutions.
§ Select a Solution.
§ Implement the Solution.
§ Perform Final Testing.
A Design Process for Programming
§ Define the Problem.
§ Write down what the problem actually is.
A Design Process for Programming
§ Define Test Cases.
§ Create some examples that reflect your code solving the problem:
input and output.
A Design Process for Programming
§ Generate Multiple Solutions.
§ At this point a "solution" consists of an algorithm plan (the high-level
sequence steps defining what your algorithm will do) and a
programming plan (the high-level sequence of steps that you will
take to code the algorithm).
A Design Process for Programming
§ Select a Solution.
§ Based on the different algorithm and programming plans, decide
which is the most promising.
A Design Process for Programming
§ Implement the Solution.
§ Start to execute your programming plan.
§ Test as you go!
§ You may realize that your algorithm plan doesn't solve the problem,
or even that you do not understand the problem.
§ If so, go back to earlier steps.
A Design Process for Programming
§ Perform Final Testing.
§ Make sure that your original test cases as well as any others that you
have thought up work.
A Design Process for Programming
§ It is critical to realize that programming is:
§ Iterative: you will go back and change your algorithm/programming plan.
You will write some code during Step 3: you might not be able to define a
solution without writing some code to solve part of the problem. You will
move back-and-forth in this process.
§ This process is a lot about finding your own mistakes: even for good
programmers, most of their time is spent testing and debugging!
Lecture Recap
Practice!
§ The syntax of function definitions.
§ Variable Scope.
§ A design recipe for writing functions.
§ Nested function calls.
§ Calling functions from within functions.
§ An Engineering Design Process for Programming.
writing your own function.
Week 2 | Lecture 1 (2.1)