Subroutines Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Why are subroutines used?

A

As your programs become larger and more complex, they need to be broken down into smaller, self-contained sections

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Two types of subroutine used in programming:

A

Functions and procedures

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What do subroutines allow you to do?

A

They allow code that you intend to use a number of times to be grouped together under one name
Both functions and procedures are subroutines
Values can be passed to both procedures and functions
Functions will return a value after processing has taken place

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Are these subroutines functions or procedures:
print(“Hello)
input(“Type in your name: “)

random(1,6)

A

print(“Hello) //a procedure to output text
input(“Type in your name: “) //a function that returns what the user enters
random(1,6) //a function that returns an integer between 1 and 6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are some examples of procedures

A

procedure showMenu()
print(“ Menu “)
print(“========================”)
print(“1: Play game”)
print(“2: Show key controls”)
print(“3: High scores”)
print(“4: Return to main menu”)
print(“5: Exit game”)
showMenu()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are some examples of functions

A

Functions work just like procedures, except at the end they will return a value
function sum(a, b)
total = a + b
return total
endfunction

answer = sum(5, 3)
print(answer)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can we pass parameters?

A

When you create a function with the statement:
sum(a, b)
a and b are known as parameters

e.g.
Create a function to calculate an average score
function averageScore(totalScore, numScores)
avg = totalScore / numScores
return avg
endfunction

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

List the advantages of subroutines

A

Each subroutine can be tested separately to make sure it works correctly
Many programmers can work on a large program at the same time, cutting down development time
Subroutines can be re-used in other programs
Subroutines can be stored in a subroutine library and used in different programs if required
Program maintenance is easier - if requirements changed then just the affected subroutines need to be modified

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the “scope” of a variable

A

The “scope” of a variable, constant, procedure or function defines the parts of the program in which it is recognised and can be used

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Explain the difference between a local and global variables

A

Globes are accessible anywhere in the program - locals only within the subroutine in which they were created

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How many parameters can functions and procedures have?

A

Zero, one or many

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are three reasons that you should try to use subroutines where possible?

A

Reuse code, decomposition, more maintainable code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly