What is recursion?
When a subroutine (often a function) calls itself within its own subroutine
What are the characteristics of a recursive subroutine?
-Contains a stopping condition which should be reachable within a finite number of times
-Subroutine should call itself (recursion) for any input value other than the stopping condition
What would happen if a recursive subroutine called itself indefinitely?
It would run out of memory causing the program to crash (STACK OVERFLOW)
What are the drawbacks of recursion?
High memory usage as a new version of the function is created every time it is called
What are some examples of when recursion is used?
-Tree traversal
-Performing a flood fill in a graphics application
What is scope?
Where a variable is accessible inside a code
What are local variables?
Variables declared inside a subroutine and are only accessible by that subroutine. They are created when the subroutine is called and destroyed when the subroutine ends.
What are global variables?
Variables that are declared outside of any subroutines. They are accessible throughout the program, created when the program starts and destroyed when the program ends.
What are the drawbacks of using global variables?
-Increases memory usage (as they are used until full program execution is over)
-Alterations within functions may have unwanted side effects elsewhere in the program
What are the advantages of using local variables?
-Easier for different programmers to work on the same program – procedures will not conflict if variables with
the same name are used
-Easier to debug programs (code in a procedure is self-contained)
What is a drawback of using local variables?
May slow execution as memory is allocated dynamically when needed
What is modularity?
The concept of breaking problems down into smaller chunks
What is the role of a procedure?
-Performs a set task
-Takes in zero, one or more parameters
What is the role of a function?
-Performs a set task
-Takes in zero, one or more parameters
-Returns a value
What are the PROs of procedures?
-Makes program easier to read & debug
-Facilitates use of local variables
-Enables different programmers to work on different parts of the code
-Allows routines to be reused in other programs
What are the PROs of functions?
-Can be called as often as needed, reducing the need for duplicated code
-Building blocks that enable larger programs to be written using reusable code routines
-Can be stored in libraries for other programmers to use in their programs. Library functions:
-Are already tested
-Make use of another programmer’s skill
-Can be written in other languages as they are already compiled
What happens when parameters are passed by value?
The value created for the subroutine being called is a copy of the original
Once it is passed in, the parameter is held in a separate memory location -> only available to the subroutine
Copy is now a local variable of that subroutine (if something is done to the value of the copied variable, the value of the original variable remains unaffected)
What happens when parameters are passed by reference?
A pointer that contains the memory address of the original variable is created -> any change to the value from within the subroutine will also affect the value of the original variable
What is an IDE?
A program that provides a set of tools & related functionality designed to maximise a programmer’s productivity
What are some features of IDEs?
Syntax highlighting —-> help identify key elements
Autocompletion —-> start typing command and it fills in the rest
Automatic indentation —-> indents code automatically within structures to avoid errors
Code editors —> text area where code can be entered directly into IDE (often supports additional features like syntax highlighting, autocomplete, & automatic indentation)
Error diagnostics —> reports errors in the code & where they can be found
Run-time environments —> software that supports the execution & running of programs; allows programmers to easily run code during development
Translators —> program that converts high-level code into executable, machine code
Auto-documentation —> tracks declared variables, modules, & other special programmer comments with a view to producing documentation that aids in program maintenance, debugging & support
Breakpoints —-> stop the program at a set point to check variables
Variable watch window —-> display the values of the variables while the program is run
What are the PROs of Object-Oriented Programming:?
Various game items can be easily represented as objects
Inheritance can be used to create specialised versions of classes
You can easily spin up extra copies of common objects (as code is organised into modular, reusable objects)
What are the benefits of using global variables?
-Variable doesn’t need passing as a parameter
-Don’t need to return a value
-Can be accessed from anywhere function/ anywhere in the program
What is a parameter?
An item of data passed to a subroutine, used as a variable within the subroutine
What is a procedural programming language?
A high-level language that gives a series of instructions in a logical order