0% found this document useful (0 votes)
24 views3 pages

PHYTON

The document describes the steps to design functions in Python: 1) Describe what the function should do with examples, 2) Specify the name, parameter types and return type, 3) Write the description and body of the function, 4) Test the function by running examples. It then provides examples of functions to convert temperatures between Fahrenheit and Celsius and calculate triangle properties by calling other functions.

Uploaded by

Martin Gelman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

PHYTON

The document describes the steps to design functions in Python: 1) Describe what the function should do with examples, 2) Specify the name, parameter types and return type, 3) Write the description and body of the function, 4) Test the function by running examples. It then provides examples of functions to convert temperatures between Fahrenheit and Celsius and calculate triangle properties by calling other functions.

Uploaded by

Martin Gelman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Function Design Recipe

The Six Steps


Examples
What should your function do?
Type a couple of example calls.
Pick a name (often a verb or verb phrase): What is a short answer to "What does
your function do"?
Type Contract
What are the parameter types?
What type of value is returned?
Header
Pick meaningful parameter names.
Description
Mention every parameter in your description.
Describe the return value.
Body
Write the body of your function.
Test
Run the examples.
Applying the Design Recipe
The problem:

The United States measures temperature in Fahrenheit and Canada measures it in


Celsius. When travelling between the two countries it helps to have a conversion
function. Write a function that converts from Fahrenheit to Celsius.

Examples
>>> convert_to_celsius(32)
0
>>> convert_to_celsius(212)
100

Type Contract
(number) -> number

Header
def convert_to_celsius(fahrenheit):

Description
Return the number of Celsius degrees equivalent to fahrenheit degrees.

Body
return (fahrenheit - 32) * 5 / 9

Test
Run the examples.

Putting it all together:


def convert_to_celsius(fahrenheit):
''' (number) -> number

Return the number of Celsius degrees equivalent to fahrenheit degrees.

>>> convert_to_ccelsius(32)
0
>>> convert_to_celsius(212)
100
'''
return (fahrenheit - 32) * 5 / 9

Function Reuse
Calling functions within other function definitions
The problem: Calculate the semi-perimeter of a triangle.

The approach: Function semiperimeter calls function perimeter.

def perimeter(side1, side2, side3):


'''(number, number, number) -> number

Return the perimeter of a triangle with sides of length


side1, side2 and side3.

>>> perimeter(3, 4, 5)
12
>>> perimeter(10.5, 6, 9.3)
25.8
'''
return side1 + side2 + side3

def semiperimeter(side1, side2, side3):


'''(number, number, number) -> float

Return the perimeter of a triangle with sides of


length side1, side2 and side3.

>>> semiperimeter(3, 4, 5)
6.0
>>> semiperimeter(10.5, 6, 9.3)
12.9
'''
return perimeter(side1, side2, side3) / 2
Calling functions within other function calls
The problem: One triangle has a base of length 3.8 and a height of length 7.0. A
second triangle has a base of length 3.5 and a height of length 6.8. Calculate
which of two triangles' areas is biggest.

The approach: Pass calls to function area as arguments to built-in function max.

max(area(3.8, 7.0), area(3.5, 6.8))

Visualizing Function Calls


We can explore how Python manages function calls using the Python Visualizer. (See
the Resources page.)

In the example below, function convert_to_seconds contains a call on


convert_to_minutes.

def convert_to_minutes(num_hours):
"""(int) -> int
Return the number of minutes there are in num_hours hours.
>>> convert_to_minutes(2)
120
"""
result = num_hours * 60
return result
def convert_to_seconds(num_hours):
"""(int) -> int
Return the number of seconds there are in num_hours hours.
>>> convert_to_seconds(2)
7200
"""
return convert_to_minutes(num_hours) * 60

seconds_2 = convert_to_seconds(4)
Here is what the memory model looks like just before the return statement inside
function convert_to_minutes looks like:

call stack before line 8 is executed


Note that there are three stack frames on the call stack: the main one, then
underneath that a frame for the call on function convert_to_seconds, and underneath
that the frame for the call on function convert_to_minutes.

Here is a link to the Python Visualizer at this stage of the execution so that you
can explore this yourself. We strongly encourage you to step backward and forward
through this program until you understand every step of execution.

When the return statement is executed, the call on convert_to_minutes exits. The
bottom stack frame is removed, and execution resumes using the stack frame for
convert_to_seconds:

call stack before line 16 is executed

You might also like