U2b - Programming techniques Flashcards
Examples of subroutines
Functions, procedures
What’s the difference between a function and a procedure?
Procedures execute code in a subroutine but they don’t return anything. Functions will return a value after processing has taken place e.g. input returns what the user typed in, random returns a number
Define parameters
Values that you can pass through a subroutine
Advantages of subroutines (4)
- Each subroutine can be tested separately to make sure it works correctly
- Many programmers can work on a large program at the same time which cuts down development time
- Subroutines can be re-used in other programs
- Program maintenance is easier - if you make a mistake, then you only have to modify the subroutines affected
Difference between global and local variables
Global variables are outside of the subroutine, whereas local variables can only be seen and used inside the subroutine
What are global variables?
Variables that are accessible anywhere in the program. They’re written in pseudocode using the global keyword
What are local variables?
Variables that only exist while the subroutine is executing. They are only accessible within the subroutine.
Advantage of using local variables
They keep a subroutine self-contained, so they can be used in any program without variable names conflicting with those used in the calling program
Advantages of using structured programming techniques (2)
- The modularised approach of using subroutines breaks down programs into manageable chunks of code, and this helps to decompose larger problems
- By using structured programming techniques, programs will have fewer bugs and be easier to maintain
Define scope
The area of a program where an item is recognised
Define subroutine
A selection of code that can be grouped together under a name, and then called later in the program
Define data validation
Routines that ensure that data entered is of the right type
Define range check
Ensures that a number or date is within a sensible/allowed range
Define type check
Ensures that data is of the right type, such as integer, letter or text
Define length check
Ensures that text entered is not too long or too short e.g. a password between 8 and 15 characters
Define presence check
Ensures that data has been entered i.e. the field has not been left blank
Define format check
Ensures that the format of something is correct e.g. DD/MM/YY and not MM/DD/YYYY
What’s the difference between validation and verification
Validation can only check that the data entered is reasonable, but verification checks that the data entered is correct
Give an example of double-entry verification
A user setting a new password being asked to type it in twice. If the two passwords don’t match, they will be asked to enter the password again
Define authentication
A routine used to make sure a person is who they claim to be e.g. username and password
Ways to make code easier to understand and maintain (5)
- Write comments at the top to show who wrote the program and when they wrote it
- Comments to explain what harder parts of the code do
- Each subroutine should have comments to explain their functionality
- Using meaningful identifiers
- Using a modular structure
Why is a trace table useful (3)
- For determining the purpose of an algorithm
- For finding the output of an algorithm
- For finding errors in an algorithm
Define syntax error
An error where the code written doesn’t conform to the rules of the language. The program cannot be run until all syntax errors are fixed
Define logical error
An error where the program will run, but it won’t work as the programmer intended