3.2.10 - Structured programming and subroutines (procedures and functions) Flashcards

(8 cards)

1
Q

Subroutines

A

A named block of code that performs a specific task and can be reused throughout a program.

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

Advantages of subroutines

A

Reduces repetition
Makes code easier to read, test, and maintain
Allows reuse in other programs

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

Parameters

A

A variable passed into a subroutine. It allows data to be passed from the main program to the subroutine.

def greet(name) - name is the parameter

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

Procedure

A

A procedure is a subroutine that performs a task but does not return a value.

python:
def greet(name):
print(“Hello”, name)

pseudocode:
PROCEDURE greet(name)
OUTPUT “Hello “ + name
ENDPROCEDURE

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

Function

A

A subroutine that performs a task and returns a value to where it was called.

python:
def square(num):
return num * num

pseudocde:
FUNCTION square(num)
RETURN num * num
ENDFUNCTION

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

Local variable

A

A variable declared inside a subroutine.
It can only be used within that subroutine.

def example():
x = 5 # local to this function

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

Global variable

A

A variable declared outside any subroutine and accessible from anywhere in the program.

x = 5

def show():
print(x) # accesses global x

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

Why should you use local variables instead of global ones?

A

Prevent accidental changes
Keeps subroutines self-contained
Improves readability and reduces bugs

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