- Give examples of what your function should do.
- Think about what your function should do. (i.e. - Convert pounds to kilograms.)
- Come up with a name for your function - usually a verb or action phrase. (i.e.
lbs_to_kg
) - Type a couple of examples which will be stored as future reference in the built-in
help()
function. These are recreations of what will happen at the Python prompt when you run your function.- >>> lbs_to_kg(2.2)
1
>>> lbs_to_kg(100)
45.36
- >>> lbs_to_kg(2.2)
- Create the type contract.
- What are the parameter types used in your function (i.e. number, int, float, str, etc.)
- Make sure to include the value returned. Here is our type contract: (number) -> number
- This is what our function looks like so far (''' indicates block comment in Python also called a docstring):
- ''' (number) -> (number)
>>> lbs_to_kg(2.2)
1
>>> lbs_to_kg(100)
45.36 '''
- ''' (number) -> (number)
- Define the Function Header.
- The function header format is a keyword, name of the function, and function parameters in parantheses followed by a colon.
- Function Header Format - def function_name(parameters):
- Choose your input parameter names and try to make them self explanatory.
- def lbs_to_kg(lbs)
- Our function code so far:
- def lbs_to_kg(lbs)
''' (number) -> (number)
>>> lbs_to_kg(2.2)
1
>>> lbs_to_kg(100)
45.36 '''
- def lbs_to_kg(lbs)
- The function header format is a keyword, name of the function, and function parameters in parantheses followed by a colon.
- Write a concise description of your function.
- Include between the type contract and examples.
- This will be a key part of this function's comments in
help()
- Description: Return the number of kilograms equivalent to the input parameter pounds (lbs).
- Updated function code:
- def lbs_to_kg(lbs)
''' (number) -> (number)
Return the number of kilograms equivalent to the input parameter pounds (lbs).
>>> lbs_to_kg(2.2)
1
>>> lbs_to_kg(100)
45.36 '''
- def lbs_to_kg(lbs)
- Body of the Function
- Includes the
return
and/orprint
statement followed by the function formula. return
will send the value of parameter(lbs)
to the caller or memory address even if it's None. To display the value from areturn
statement store the value in a variable and call the variable with aprint()
statement.return
will also exit the current function. Ifreturn
is skipped an implicitreturn None
is included at the end of the function.print
will display to standard output the value of a parameter, in this case(lbs)
.print
does not affect the control flow of execution in any way or exit the function.- This function's body: return lbs / 2.205
- Includes the
- Test
- Save your function to a .py file (i.e. lbs_to_kg.py)
- Load the file and restart your kernel to ensure the new function is loaded to Python.
- You can check this by typing
help(lbs_to_kg)
to make sure everything is working.
- You can check this by typing
- Recreate the examples in the function help file
help(lbs_to_kg)
to ensure everything is working correctly.
Filename: lbs_to_kg.py
def lbs_to_kg(lbs)
''' (number) -> (number)
Return the number of kilograms equivalent to the input parameter pounds (lbs).
>>> lbs_to_kg(2.2)
1
>>> lbs_to_kg(100)
45.36
'''
return lbs / 2.205
*References
Coursera Function Design Recipe - Link
University of Toronto Function Design - Link
How to Think Like a Computer Scientist: Learning with Python - Link