Search Header Logo
Subroutines

Subroutines

Assessment

Presentation

Computers

10th Grade

Medium

Created by

Roy Duguid

Used 20+ times

FREE Resource

14 Slides • 11 Questions

1

​Do Now:

Teams > Coding > Subroutines > Do Now

2

​# Length: Find number of characters in a string print(len("hello world"))
# Output: 11
# Case Conversion text = "hello world" print(text.upper()) # UPPERCASE -> HELLO WORLD print(text.title())
# Title Case -> Hello World
# Slicing word = "computer" print(word[0:3])
# Output: com
# Substring Search print("cat" in "concatenate")
# Output: True
# Find Position print("banana".find("a")) # Output: 1

3

​Big Question

​How do we define a subroutine?

How do we describe the purpose of parameters in subroutines?

How do we use procedures that accept arguments through parameters?

​Key words: subroutine, function, procedure, parameter, argument

4

media

Sub programs are used to avoid unnecessary code duplication in

programs and to make programs easier to read.

Sub Programs

© ZigZag Education, 2020

Procedures

A sub program is a section of code that performs a specific task and

can be called when needed at any point in the program.

There are two different types of sub program:

Functions

5

media

A procedure is a sub program that doesn’t return a value back

to the main program.

Procedures

© ZigZag Education, 2020

def message():

print("Computer")
print("Science")
print("Rocks")

message()

This is an example procedure that will output the text "Computer",

"Science", "Rocks" whenever it is called.

A procedure or function won’t run unless it is called.

Output

Computer
Science
Rocks

Procedure call

6

media

Sometimes procedures and functions need data in order to carry out

their job. This is where parameters come in.

Parameters

© ZigZag Education, 2020

def areaCalc(width,height):

area = width * height
print(area)

areaCalc(10,5)

Parameters are like placeholders for values that a procedure or

function expects to be passed when it is called.

In this example the values 10 and 5 are passed into the Width and

Height parameters when the areaCalc procedure is called.

Output

50

Parameters

width

10

height

5

area

50

7

media

A function is a sub program that returns a value to the main program.

Functions

© ZigZag Education, 2020

def areaCalc(width,height):

area = width * height
return area

print(areaCalc(10,5))

The returned value takes the place of the original function call in the

main program.

This is the areaCalc sub program written as a function.

Output

50

width

10

height

5

area

50

8

media

Where a variable can be accessed within a program depends on the

variable scope. There are two types of variable:

VariableScope

© ZigZag Education, 2020

def speedCalc(distance, time)

speed = distance / time

return speed

answer = speedCalc(90, 2)

Local
Declared within a sub program and only accessible within

that sub program.

Global
Declared outside any sub programs and accessible

throughout the program, including inside sub programs.

Local Variable

Global Variable

This example

program features

a procedure to
calculate speed.

It uses both a

local and a global

variable.

​Global

​Local

9

media

10

media

11

media

12

media

13

​Questions ahead....

14

Multiple Choice

Which of the following is not an example of a subroutine?

1

Procedure

2

Function

3

List

15

Multiple Select

What is the purpose of using subroutines in your programming? (select more than 1)

1

Avoiding repeated sections of code

2

Making your code shorter and easier to read

3

Enabling you to split your code into sections

4

Avoiding making errors in your code

16

Multiple Choice

Question image

What is the name of the subroutine?

1

happyBirthday

2

person

3

def

17

Multiple Choice

Question image

What is the name of the parameter?

1

happyBirthday

2

person

3

def

18

Multiple Choice

Question image

What is the name of the subroutine?

1

addNumbers

2

num1

3

num2

4

total

19

Multiple Choice

Question image

num1 and num2 are the parameters

1

True

2

False

20

Multiple Choice

Which subroutine will return a value back to the program?

1

Procedure

2

Function

21

Multiple Choice

Which subroutine will not return a value back to the program?

1

Procedure

2

Function

22

Multiple Choice

A procedure can return a value to be used later in the program

1

True

2

False

23

Multiple Choice

A function can return a value to be used later in the program

1

True

2

False

24

Multiple Choice

Question image

What is the name of the subroutine?

1

username

2

firstName

3

secondName

25

Activity 1

​Do Now:

Teams > Coding > Subroutines > Do Now

Show answer

Auto Play

Slide 1 / 25

SLIDE