1.2.4 Introduction to programming Flashcards
Procedural programming paradigm
Procedural programming :
- Program in Subroutines (functions and procedures)
- Subroutines carries out defined and specified steps to achieve output (uses sequence, selection and iteration)
- Uses Top down analysis - Break down problem into sub-problems and code in self contained subroutines using local variables and structured code (sequence, selection and iteration within subroutines)
- Can be applied to wide range of problems and efficient for straightforward tasks.
- Easy to write and interpret due to clear flow of control.
Object orientated programming paradigm
Object orientated : Can develop solution modelled on real world objects.
Advantages:
- Easier to update and maintain due to modular structure and classes can be modified or extended
- Encapsulation limits how attributes can be changed – debugging and security better.
- Improved re-usability of code – classes can be used in other programs and inheritance allows for reuse of code
- Polymorphism allows for flexibility of code
- Better coding for part of team – classes distributed between team members
- Less code needed so errors less likely
Disadvantages :
- Where Few components reused - generally inefficient
- Generally Unsuitable for smaller problems
Machine code
Binary code that the computer can execute
Each line Consists of Opcode (instruction) and Operand (data)
High-Level languages (e.g. Procedural and OOP)
High Level languages :
- Closer to natural language / English –> easy to debug and build in team so faster development
- One instruction in high level can equate to many machine code instructions (one to many)
- Portable - Independent of processor architecture
- Translated with translator to machine code for different architectures.
- More abstracted - no knowledge of processor needed
- Come in many paradigms so can choose for problem.
Low-Level languages (assembly)
Low Level languages :
- Uses mnemonics
- One instruction equates to one machine code instruction –> can be much larger than high level. 1:1
- Translated with assembler
- Useful for embedded processors where speed and efficiency important.
Disadvantages :
- Fixed to specific processor architecture – not portable
- but more knowledge needed of processor - longer to code as more complex
Advantages :
- More control over registers- more efficient with CPU resources and direct control of hardware and memory (through addressing modes)
- Potential for very efficient code through optimised low-level operations.
Immediate addressing
Immediate addressing: Opcode acts on operand
- Data hard coded into instruction
- Nothing fetched from memory so very fast
- Value fixed at compilation so inflexible
Direct addressing
Direct addressing: Operand is the memory location data is stored in
- Code refers directly to memory address of data
- Fast as RAM quickly accessible
- Flexible as value at address that is loaded can vary
- Limited on address range by size of operand
Indirect addressing
Indirect Addressing : Operand gives address which holds address of where the data is located.
- Often used with libraries that get loaded into memory at run time, loader will likely place in different location in memory each time
- More flexible
- Increased address range as not limited by operand size.
- however multiple fetches required to access data.
Modes of addressing
Opcode - Specifies instruction
Operand - Data instruction is to be performed on
Addressing mode specifies how the operand should be interpreted
Indexed addressing
Indexed addressing : Operand is added to contents of Index Register (base address) to get memory location of value needed.
- Good for data that is stored contiguously such as in arrays.
Object Orientated Programming (classes and objects)
Classes - Blueprint for object - constructor method instantiates object from class
- Defines attributes and methods implemented
Objects - Instance of a class
- Has attributes and methods tied to instance and not spread out.
Methods - Actions performed by object that link to procedures and functions
Attributes - Value held by object that link to variables
OCR Syntax for OOP
Class dog()
Private name
Private colour
Public procedure new (MyName,MyColour)
name = MyName
colour = MyColour
Endprocedure
Inheritance
Inheritance : Where a subclass / child inherits attributes and methods of superclass / parent class
- Reduces volume of code needed
- Can override methods of parent class and have own methods and attributes
Polymorphism
Polymorphism : Where code can handle objects of different classes in the same way and they can behave differently based on their class and object.
- Reduces volume of code needed as can be reused and code can be more flexible
Encapsulation
Encapsulation: When attribute is private (can only be accessed from within class)
- Only Public methods can be used to read / amend the attributes value so can further validate inputs
- Objects can only change in intended way - Protects data from accidental changes and reduces chance of errors and inconsistencies due to unexpected changes to attributes.
- Likely fewer issues as the team combines their code .
- improved security
Create getter and setter methods :
Setter is method that sets the value of an attribute
Getter is method that returns the value of an attributes
Paradigm
Programming paradigms are established conventions and practices that dictate how computer programs are structured and developed.
- Different problems and tasks suited to different paradigms that have advantages and limitations.