Fundamentals of Programming Flashcards
Algorithm
a sequence of steps to complete a task
Definite iteration
For loop
the number of iterations is known (at the time of execution);
Indefinite iteration, condition at the start
While loop
Indefinite iteration: the number of iterations depends on a condition (that is tested before / after each iteration);
Indefinite iteration, condition at the end
Repeat until
Indefinite iteration: the number of iterations depends on a condition (that is tested before / after each iteration);
Global variable
Global variable can be used anywhere in program // global variable is declared in outmost block / outside subroutines;
global variables use memory the entire time the program is executing;
global variables exist the entire time the program is running;
Local variable
Local variable can only be used in the block / subroutine in which it is declared;
Local variables only use memory when the program block they are in is executing;
Local variables only exist when the program block they are in is executing;
State one difference between local and global variables and give two reasons why it is good practice to use local variables.
Difference:
global variables accessible to all parts of the program
// declared in main program block
// local variables declared in subroutine
// accessible only in the program block/subroutine in which it was declared;
Reason:
memory allocated to local variables can be reused when subroutine not in use;
local variable only exists while the program block/subroutine is executing;
using local variables makes subroutines self-contained;
A. prevents accidental changes;
A. easier debugging/maintenance;
Why is it good practice to use local variables?
1. makes a subroutine self-contained;
2. releases storage when subroutine terminates;
3. able to test / debug subroutine independently;
4. easier to re-use subroutine in another program;
5. local variable values cannot be inadvertently/accidentally altered by a subroutine call from the subroutine;
Explain one difference between a procedure and a function.
Functions always return some value when called;
Procedures may return a value;
Functions appear in expressions;
Procedures do not appear in expressions;
Procedures name alone makes up the statement / call <name></name>