2.2.1 - Programming techniques Flashcards
What is a sub routine?
A named Block of code that performs a specific task within a program
What is a function?
A subroutine that returns a value and produces information from a given task.
What is a procedure?
A subroutine that performs a task that does not return a value.
Define passing by value
The value of the variable is passed into the subroutine and treated as a local variable within. If the parameter value is changed it will not affect the original variable.
This is often the default in programming languages.
Define passing by reference.
Passing by reference passes the actual memory address of the variable into the subroutine - therefore the actual/original value of the variable can be changed within the subroutine.
Explain global variables
Variables that are declared in the main section of the program.
Can be used anywhere they have “Global scope”
Explain local variables
LOCAL SCOPE.
Only local to the subroutine that they are declared within.
They have no effect whatsoever on variables outside of that subroutine.
How is encapsulation useful in sub routine programming
If all needed variables are encapsulated within the subroutine then it becomes modular and can be used elsewhere independently.
Define modular programming
A long complex program can be broken down into smaller subroutines/subtasks and this can help design an algorithm that is easier to manage anymore efficient.
Advantages of modular programming with subroutines
Relatively easy to understand, debug and maintain - especially if its purpose is clearly defined and well documented.
Subroutines can be tested independently, thereby shortening the time taken to get to a large program working
Once a subroutine has been thoroughly tested, it can be reused with confidence.
Define a recursive subroutine
The process of a subroutine calling itself.
A subroutine written in terms of itself.
Recursion has three main characteristics:
For input values, the routine must call itself
A stopping condition - when the condition is met the subroutine will stop calling itself and unwind.
The stopping condition must be reached after a finite amount of calls.
Difference between an iterative approach and recursive approach
Recursive algorithms are often shorter; but more difficult to trace.
If a recursive routine is called a very large number of times before the stopping condition is reached, the program may crash due to a stack overflow.
An iterative routine has no limits to the number of times it repeats or how many times it can be called - to some extent
what is stack memory?
Memory where the return addresses, parameters and local variables of a subroutine are temporarily stored when called.
This follows the Stack data structure framework.
The data held in the stack frame is then popped once the subroutine is finished executing.
What is an IDE?
Integrated development environment
A software package that helps write code easily.
Provides tools that may help edit, enter, compile, test and debug your program.