Class 27 — Monday March 28
Reaching an understanding with functions
Functional living — Problem-solving strategy — Embrace this future
Look both ways
Agenda
- Another try at functions
Last chance
- Mentoring support
Example de jeur — module primal
- Definition:
x
is a factor ofy
if the remainder ofy
/
x
is 0.
-
Code: primal.py
Function
is_factor( x, y )
- Parameters
x
andy
are integers. The function returns whethery
is a factor ofx
.
Function
are_relative_primes( x, y )
- Parameters
x
andy
are integers. The function returns whetherx
andy
are relatively prime; i.e., whethery
is not a factor ofx
and vice-versa.
Function
is_prime( x )
- Parameter
x
is an integer. The function returns whetherx
is prime; i.e., its only factors are 1 and itself
-
Slides: PDF
-
Tester: factoid.py
Questions about homework take_it_away.py
- Function
donut( d, h )
- Function parameters
d
andh
are numerics. The function returns the volume of a donut whose width isd
and whose hole width ish
.
-
Function
signum( x )
- Function parameter
x
is a numeric value. The function returns integer -1, 0, or 1 depending respectively whetherx
is negative, zero, or positive. The function does not print any output.
-
Function
mid( s )
- Function parameter
s
is a string. If the length of strings
is odd, the function returns the middle character ofs
; otherwise, the function returns the two middle characters ofs
. The function does not print any output.
To do
- Review artifacts and prior function materials
- Complete homework