Year 7 Subroutine Note
Year 7 Subroutine Note
Functions
&
Procedur
es
Subroutines
Introduction
• Imagine you are writing a program that has 100 users who want to be able to change their name.
Imagine that to do this you had to write your code like this:
• If user1 wants to change their name: <code that asks for user1's new name and then edits it>
• Doing this 100 times till you gets to the hundredth person
• Your program would be very long and the chances of making a mistake would be high. Finding errors
would also be very tedious.
• To avoid these, we can write one smaller program, within our larger program, that can be called
whenever any user needs to have their name changed.
• These smaller program within our larger program is what we called subroutines.
• A subroutine is a code that performs a specific task, that can be called from anywhere of your
program.
Subroutines
Characteristics of subroutine
All subroutines in Python require:
• A keyword define written as (def)
• An identifier (a name)
Adding new functions
• A function definition(def) specifies the name of a new function, the purpose of a function and the
sequence of statements that execute when the function is called.
• def is a keyword that indicates that this is a function definition.
• A subroutine can then be called in the rest of the program as often as is required by its identifier.
Types of Subroutines
There are Two main types of subroutine:
• Functions
• procedures
Subroutines- Functions
Calling a function
• To call a function, use the function name followed by parenthesis:
• The syntax for calling the new function is print_lyrics()
Note:
• If you type a function definition in interactive mode, the interpreter prints ellipses (. . . ) to let you know
that the definition isn’t complete:
• To end the function, you have to enter an empty line (this is not necessary in a script).
• Once you have defined a function, you can use it inside another function. For example, to repeat the
previous refrain, we could write a function called repeat_lyrics:
Subroutines- Functions
def repeat_lyrics():
print_lyrics() def print_lyrics():
print_lyrics() print("I'm a lumberjack, and I'm okay.")
And then call repeat_lyrics: print('I sleep all night and I work all day.’)
>>> repeat_lyrics() #print_lyrics()
Flow Of Execution
• This program contains two function definitions: print_lyrics and repeat_lyrics.
• Function definitions get executed just like other statements, but the effect is to create function
objects.
• The statements inside the function do not get executed until the function is called, and the
function definition generates no output.
• In order to ensure that a function is defined before it is first use, you must know the order in which
the statements are been executed. This order is called the flow of execution.
• Flow of execution is the order in which statements are executed during a program run.
• Execution always begins at the first statement of the program. Statements are executed one at a
time, in order from top to bottom.
Subroutines- Functions
When the function is called, we pass along a first name, which is used inside the function to print the full
name:
Subroutines- Functions
Keyword Arguments
You can also send arguments with the key = value syntax. This way the order of the arguments does
not matter.
Subroutines- Functions
Example
def my_function(child3, child2, child1):
print("The youngest child is " + child3)
my_function(child1 = "Emil", child2 = "Tobias", child3 = "Linus")
QUESTION TIME?