0% found this document useful (0 votes)
3 views

Year 10 Python Subroutines Revision

Uploaded by

r4ryanjohn
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Year 10 Python Subroutines Revision

Uploaded by

r4ryanjohn
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Year 10 Python Subroutines Revision:

All subroutines are used to provide an identifier (name) to lines of code.

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.

A procedure will not have a return value

A function must have a return value

Example code of both:

Complete all of the questions below and insert screenshots underneath


each:

(1) Create a procedure called greet() that prints a simple greeting


message. Call this procedure to demonstrate how procedures
work.

(2) Extend the greet() procedure to have a


parameter identified as name, and print a
personalized greeting message. Call the
procedure with different names to
demonstrate a working parameter.

(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.

You might also like