02 Section 5 - Sub programs Flashcards

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

What are procedures?

A

Procedures are sets of instructions stored under one name

-when you want a program to do a whole set of instructions you only need to call the procedure

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

What are functions?

A

Functions are sets of instructions stored under one name WHICH WILL ALWAYS RETURN A VALUE

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

What are the advantages of procedures and functions?

A
  • help avoid repeating code
  • useful when you have sets of instructions that repeat in different places throughout a program
  • gives a program more structure
  • makes a program more reliable
  • cuts down the amount of code you have to write
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why would you create your own procedures or functions since high-level languages have common procedures and functions built into them?

A

-if you want one that will do something more specific

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

What do most sub-programs contain?

A

parameters

arguments

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

What are parameters?

A

Parameters are special variables used to pass values into a sub program
-you can specify a name, data type and default value for each parameter

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

What are arguments?

A

Arguments are the actual values that the parameters take when the sub program is called

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

What are the features of procedures?

A
  • don’t have to take parameters (sometimes will)
  • called by typing their name (and giving an argument is necessary)
  • procedures don’t return a value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the features of functions?

A
  • take at least one parameter and must always return a value
  • when a function is called it should be assigned to a variable or used in a statement, otherwise the returns will not be stored anywhere and will be lost
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the two types of variables you can have?

A

Either local or global

-all variables have scope

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

What does a variables scope tell a programmer?

A

The scope of a variable tells you which parts of the program the variable can be used in

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

What are local variables?

A

Variables that can only be used within the structure they are declared in
-they have local scope

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

What are global variables?

A

Variables that can be used any time after their declaration

-they have global scope

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

When are variables local?

A

When they are declared within a sub program

-they are invisible to the rest of the program (can’t be used outside of the function)

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

What are the advantages of local variables?

A
  • their scope only extends to the sub program they are declared in
  • they can’t affect and are not affected by anything outside of the sub program
  • it doesn’t matter if you use the same variable name as a local variable elsewhere in the program
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How can variables in the main body of a program be made into global variables?

A

using the keyword ‘global’

-they then can be used anywhere in the program

17
Q

What is a disadvantage of global variables?

A

It can be difficult to keep track of the value assigned to the global variable in larger programs