4.1 - Fundamentals of programming Flashcards
Define ‘subroutine’.
A named section of code that can be called by writing the subroutine’s name in a program statement
What are the 3 advantages of using subroutines?
Makes the code more readable and reusable - code is broken into smaller sections
Most programming languages come with a standard library of built-in subroutines to perform common functions
Pre-written libraries of code come from a reliable source - will hopefully be well-tested as they will have been used in a large number of different programs
Define ‘local variable’ and ‘global variable’.
Local variable - a variable that can only be accessed from the subroutine within which it is declared
Global variable - a variable that can be accessed from any part of a program and exist in memory for the entire duration of the program’s execution
What are 3 things that are stored using a stack frame?
Return addresses
Parameters
Local variables for each subroutine call that occurs during the execution of a program
What is a recursive subroutine and what is a base case?
Recursive subroutine - a function that calls itself until a base case is met
Base case - the condition(s) at which the subroutine is no longer called
What is the structured approach to programming? (3 points)
Designed from the top down
Most important elements of a problem are broken down into smaller tasks
Each of which can be solved in a block of code such as a procedure or module which goes on to form part of the overall solution
What are the 2 advantages of the structured approach to programming?
Makes maintaining the program easier - navigation of different elements of the overall solution is improved
Testing can be carried out on the individual modules before they are combined to form the overall solution
What is a class?
A blueprint for objects that specifies what properties (data) and methods (instructions) objects of their type will have
What is an object? (2 points)
Anything that we can name and describe
Things that we want to model within our programs - anything that exists in the real world that has some specific meaning
Define ‘instantiation’.
The process of making an object from the class definition
Define ‘encapsulation’.
The process of combining methods and procedures to form an object
Define ‘inheritance’.
Is-A - allowing one class to share the properties and methods of another class while having its own properties and methods too
Define ‘aggregation’. (2 points)
The weaker of the two kinds of association (Has-A)
The object can exist independently of the object it is associated with
Define ‘composition’.
A stronger relationship between classes (Has-A aka Part of)
The object is deleted if the object it is associated with is deleted
Define ‘polymorphism’.
Objects of different classes/types responding differently to the use of a common interface/the same usage
Define ‘overriding’.
When a method has the same name as a method in an inherited class but different implementation
What are the 4 advantages of OOP?
Usually easier for us to think in terms of objects than to think procedurally - we are surrounded by objects in the real world which have attributes which are easy to think of
Prewritten classes promote and support code reuse - in a large organisation, there are likely to be prewritten classes for common objects which programmers can make use of without rewriting any definitions
Classes are highly modular, making them easy to maintain - if an object needs an extra attribute, you only need to go to one place to add it
Encapsulation provides a high level of protection for data - preventing direct access to private attributes ensures that the data can never be modified carelessly
What are the 4 disadvantages of OOP?
OOP design techniques often make it easier to fully model a complete system, but it can also result in a much larger, more complex system being built in order to achieve the results
Detrimental to performance due to relying on
high volumes of message passing
Inheritance can have unintended consequences - objects can acquire the ability to do things that don’t make sense
Objects can take up a relatively large amount of memory - can be an issue if there are hardware restrictions
What are the 3 OOP principles?
- Encapsulate what varies - any requirements which are likely to change should be encapsulated in a class so that changes can be easily made
- Favour composition over inheritance - seen as a more flexible relationship between objects
- Program to interfaces, not implementation - allows unrelated classes to make use of similar methods
What are public, protected and private attributes/methods?
Public - can be accessed from anywhere in the program
Protected - can be directly accessed by a subclass
Private - can only be accessed from within the class