124-introduction-to-programming Flashcards
Programming Paradigms
- way of thinking. Different approaches to using a
programming language to solve a problem. - Different programming languages have similar underlying concepts.
- The paradigm used depends on the type of problem that needs solving.
Why do we need different programming paradigms
-different paradigms better suited for different problem types
-allows to choose the most appropriate one to create efficient and effective code
- each has its own strengths and weaknesses
-different levels of abstraction.
-different trade-offs between readability, maintainability, and performance
- some languages support only one paradigm e.g Small Basic, some support multiple e.g Python
Using multiple paradigms
- can lead more robust and versatile code - by combining them, developers can take the advantage of each one, improving readability, maintainability, and reusability.
- using multiple paradigms helps reduce the language barrier between different systems and applications - easier to work together
- each paradigm can be used in multiple programming languages, so developers can communicate more effectively and collaborate on projects that use different languages
Declarative Language
- focus on stating the desired result, rather than the exact series of instructions to get to the result.
- The programming language determines how to obtain the result, and the details are abstracted from the user.
- can be split into functional and logic programming
Functional programming
- Functions form the core of the program
- Code consisting of function calls combined with each other
- Closely linked to mathematics
Logic programming
-uses code to define facts and rules based on the problem, with queries used to find answers.
Imperative Languages:
- Uses statements to execute in a specific sequence, conditionally, or repeatedly, altering the program’s state.
- code specifies actions clearly, directing what to do, how to operate, and in what order.
- Focuses on giving specific instructions step-by-step code development to achieve the desired result.
- can be split into procedural and object-oriented approaches.
Procedural Programming:
- uses predefined and well-structured subroutines to build a program
- by specifying all steps that a computer must take to reach a desired state or output
- procedures or functions are implemented on data and variables to perform a task
- characteristics include instructions, sequence, selection, iteration, procedures, functions
- follow a systematic order of statements, functions and procedures to solve a problem or complete a task.
- can be applied to a variety of problems and are easy to write and interpret.
- program instructions are executed step-by-step.
Object-Oriented Programming:
- develops a solution which consists of objects which have been modelled on the real world, objects then interact with eachother
- characteristics: instantiation, encapsulation, inheritance, polymorphism, methods, attributes
-focuses on a modular approach using objects, classes, and encapsulation - involve solutions being constructed by means of objects that interact with each other
- Applicable to certain types of problems with lots of reusable components
- Built on entities (objects formed from classes) which have attributes and methods
- Focuses on making programs that are reusable and easy to update and maintain
Comparison of Procedural vs Object-Oriented Paradigms:
- OOP uses classes, methods, and instances for program structure, while procedural programming uses procedures and functions.
- Both use sequence, selection, and iteration.
- OOP has inheritance allowing the creation of new classes based on existing classes, while procedural programming does not but can use libaries or modules to reuse code.
- OOP uses encapsulation to store data in attributes and conceal it from other parts of the program, while procedural programming uses local variables inside subroutines, or global variables that can be accessed from any part of the program.
Procedural programming is relatively easy to write and interpret, while OOP is useful for reusable components with similar characteristics.
High level languages
- Designed to be easier for humans to use and are more abstracted from hardware than low-level languages.
- Have built-in functions and libraries that allow for complex operations without understanding computer hardware.
- 2 main categories of high-level languages: Imperative and Declarative.
- Examples include Python, Java, C++, and Ruby.
pros of High level language
- Easy to learn and read: simpler syntax and closer to natural language, beginners. Intuitive
- Portability: can be compiled or interpreted into machine code for execution on different systems. Can be recompiled for different architectures
- come in variety if paradigms, programmer can choose according to their preference
- Efficient coding: higher-level abstractions and functions, classes, and libraries, allows programmers to write more complex programs in less time - developed faster, and easier to find programmers who specialise in high-level languages
- Less prone to errors: designed to be user-friendly, easier for programmers to write code with fewer errors + easier to debug / maintain / improve
cons of high level languages
- slower execution time - requires translation, not suited for real-time applications w/ speed and precision.
- less control over hardware, cannot communicate directly with hardware, limited access to low-level functionality- abstracts hardware details, so challenging to control hardware + certain hardware or system functions require low-level programming knowledge
- not memory efficient, requires more memory resources to run
Assembly language
- low level language
- each assembly mnemonic corresponds to one machine code instruction
- each mnemonic represents an opcode and an operand
- used in embedded systems to interact directly with hardware
- must be translated by an assembler before it can be executed
Pros of assembly language
-requires less memory and same execution efficiency as machine code. useful in device drivers when fast execution is required
-complete control of the code, individual statements can manipulate specific hardware components and can work directly on memory locations
- direct control of hardware and how memory is used via addressing mode
- provides direct access to system-level features w/o having to go through an abstraction layer provided by a software interface (e.g library/OS/framework - e.g open( ) for file system abstraction)
- easier to optimise and is optimised for a specific architecture
- programmers can choose the exact instructions so can write code that is highly efficient
- can make use of special machine-dependent instructions e.g. in the instruction set for the chip.
- best suited for when compilers or interpreters don’t exist for target CpU, & embedded systems highest possible performance is critical as memory limited
cons of assembly language
- tedious to write and very prone to bugs - larger code/more lines to perform same task of high few lines. and programmer has to manage memory and hardware resources directly
- difficult to understand and debug, making it hard to modify and maintain - no high-level abstraction to help identify errors
- machine-dependent and code is difficult to port, specific to a particular processor architecture and os
- programmers who can write efficient assembly code are rare as difficult to learn
Little man computing
- conceptual computer used in education theory to teach assembly language programming.
- 11 simple instructions (assembly mnemonics) in its instruction set
- abbreviations used to represent machine code instructions: ADD, SUB, STA, LDA, BRA, BRZ, BRP, INP, OUT, HLT, and DAT.
- uses label names as substitution for addresses in code.
Little Man Computing Instructions/Assembly Mneumonics
0 - HLT - Stop program
1 - ADD - Add the contents of the memory address specified to the accumulator
2 - SUB - Subtract the contents of the memory address specified from the accumulator
3 - STA/STO - Store the value currently stored in the accumulator in the memory address specified
4 - Unused instruction that generates an error message
5 - LDA - Load the contents of the memory address into the accumulator
6 - BRA - jump always to the designated memory address/point in program
7 - BRZ - jump to the designated memory address/point in program if the accumulator is zero
8 - BRP - jump to the designated memory address/point in program if the accumulator is zero or positive
9 - INP - allows the user to input a value which will be held in the accumulator
10 - OUT - prints the value held in the accumulator
11 - DAT - reserves the memory address followed by DAT for storing data when the instruction is compiled