Year 10 Python Subroutines Revision
Year 10 Python Subroutines Revision
When that identifier is then called the lines of code are executed.
This is why you need to declare (def) a subroutine before attempting to call the subroutine.
This is also why you need to call the subroutine before the lines of code will be executed.
Remember that subroutines are split into two parts: procedures and functions. They are mutually
exclusively.
(3) Create a function called add() that takes two parameters and returns their sum. Call this
function and print the result.
(4) Extend the add() function to return both the sum and the product of the two numbers.
(5) Modify the greet() procedure to have a default parameter greeting="Hello", so if no greeting
is specified, it defaults to "Hello".
(6) Write two functions: square() and double(). Then, create a third function called
square_of_double() that calls both square() and double() functions to return the square of a
doubled number.
(7) Write a function called average() that takes a variable number of arguments and returns
their average. Test the function with different numbers of arguments.
Extension:
(8) Create a function called apply_func() that takes another function and a list of numbers as
arguments. Apply the given function to each number in the list and return the resulting list.
Test this function with various input functions like square(), double(), etc.
(9) Find out what a global variable, what a local variable is and add both to example
subroutines.
(10) Add comments to improve the maintainability (make it easier to maintain by yourself and
other developers) of your code.