02 Section 5 - Sub programs Flashcards
What are procedures?
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
What are functions?
Functions are sets of instructions stored under one name WHICH WILL ALWAYS RETURN A VALUE
What are the advantages of procedures and functions?
- 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
Why would you create your own procedures or functions since high-level languages have common procedures and functions built into them?
-if you want one that will do something more specific
What do most sub-programs contain?
parameters
arguments
What are parameters?
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
What are arguments?
Arguments are the actual values that the parameters take when the sub program is called
What are the features of procedures?
- 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
What are the features of functions?
- 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
What are the two types of variables you can have?
Either local or global
-all variables have scope
What does a variables scope tell a programmer?
The scope of a variable tells you which parts of the program the variable can be used in
What are local variables?
Variables that can only be used within the structure they are declared in
-they have local scope
What are global variables?
Variables that can be used any time after their declaration
-they have global scope
When are variables local?
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)
What are the advantages of local variables?
- 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