Sub Programs Flashcards
What can sub programs be used for?
Save time and to simplify code.
What are procedures?
- sets of instructions stored under one name
- when you want all the instructions executed you only need to call the name of the procedure
How are functions different from procedures?
Functions always return a value.
Why are procedures and functions useful when you have sets of instructions that you need to repeat in different places within a program?
They give your program more structure and readability whilst cutting down on the amount of code you actually need to write.
What has common procedures and functions built into them?
High level programming languages, but if you want something more specific you can create them yourself.
What are parameters?
Special variables used to pass values into a sub program. For each parameter you can specify a name, a data type and a default value.
What are arguments?
Arguments are the actual values that the parameters take when the sub program is called.
What do functions do?
Take at least one parameter and they must always return a value.
What should happen when a function is called?
It should be assigned to a variable or used in a statement otherwise the value that it returns will not be stored anywhere and will be lost.
What is a variables scope?
The scope of a variable tells you which parts of the program the variable can be used in.
What is a local variable?
Can only be used within the structure they’re declared in - they have a local scope.
What is a global variable?
Can used be any time after their declaration - they have a global scope.
What are variables declared inside a subprogram?
Local variables. They are invisible to the rest of the program. This means they can’t be used outside the function.
What is an advantage of a local variable?
The scope only extends to the subprogram they’re declared in, so they can’t affect or be affected by anything outside the sub program. It also doesn’t matter if you use the same variable name as a local variable defined somewhere else in the program.
How can variables in the main body of a program be made into global variables?
Using the ‘global’ keyword - these variables can then be used anywhere in the program. However it can be difficult to keep track of the value of global variables in larger programs.