Please enable JavaScript.
Coggle requires JavaScript to display documents.
Programming Concepts (4.1.1.2) & (4.1.1.16) - Coggle Diagram
Programming
Concepts
(4.1.1.2)
& (4.1.1.16)
Variables
& Constants
Variable Definition
hold values that can be modified by program
(except constants); Assign value - declare name, data type
then assign value (as 1/2 actions)
Declaration
- creating space in memory for var/const
Assignment
- setting contents of memory space to value
Initialisation
- set value before processing
Constant Advantage
Keeps code consistent, easier debug, maintain code quality
Avoid mitsakes; possible changes done only at init.
Indentifiers
Name (given to variable, constant, subroutine, class)
meaningful indentifiers = self-documenting code
Naming Conventions
Most languages don't allow space in identifiers
for readability: (lower camel / upper camel / snake) case
Otherwise, raw convention used (lower camel aka Pascal)
Sequence
Selection
& Iteration
Definite vs Indefinite
Definite
- action repeated spec. no. of times ('for')
Indefinite
- action repeated until condition ('while')
Nested
- action repeated inside repetition, useful
for complex data structures, can be definite/indefinite
Sequence & Selection
Sequence
- instruct. series program executes in order
Florw of Control
- order of instruct. execution
Selection
- instruct. executes on condition
Selection block
- can be scaled to multi-conditions etc.
Nested Selection
- implement branching logic
(if condition True, sub-conditions inside condition enacted )
Subroutines
Defining & Calling
Define
- named block of code
Calling
- write identifiers as part of program statement
Flow of Control
- determines order of execution
(usually sequential, calling transfers control 'out of line')
User-defined
- users can define subroutines, libraries/
modules; can be imported, used on other programs
(often shared in organisations)
Pre-defined
- for common tasks, built into language/
external code libaries imports (mathlib) (usually well-tested)
it saves "reinventing the wheel"
Definition
Named section of code aka procedures/functions
procedures execute, functions return values
some languahges allow multiple return values (tuples)
Parameters & Arguments
Parameters
- data passed to subroutine when called,
(local), subroutine value placeholder; defined when subr.
declared (can have multiple / no parameters)
Arguments
- data given to subroutine, used as parameter
Subroutine Interfaces
- method by which data is passed
in/out of subr. thru parameters, returns values
Passing Arguments
By value
- parameter is copy of origin. argument
parameter only has local scope, original remains unchanged
By reference
- creates pointer, memory address at origin,
changes w/in subr. affect original variable
Local & Global Variables
Local
- variable only accessed in spec. code part
locals in diff. regions can have same identifiers, they
occupy diff. memory spaces, unchanged by each other
Global
- can be accessed anywhere, distinct RAM place
local takes preference over global in subr, local won't overwrite global
Advantageous
shorter program, easier read, modularisation
module tests, split coding tasks, less changes
recyclable usage, customisable arguments
library production
Call Stack & Stack Frames
Call Stack
- active subroutine stack (active = unfinished)
Stack has finite size, dependent on program memory access
infinite subroutine recursion leads to stack overflow 'error'
Stack Frames
- created when subr. called; memory for
local, parameter values, return pointer (exe. point of subr.)
Return?
- on return, stack frame removed from call stack,
subr. should always return; recursive subr. have multiple
returns for base & general cases;
Heap
- storage created for dynamic structures;
Data Section
- storage for global
Recursive
Techniques
Basics
A subroutine defined in terms of itself; subR that calls itself
Problem's solutions are in smaller instances of same problem
General case - solution to problem in terms of smaller vers.
Base case - answer known to this version of the problem
aka Stopping Condition; Unwinding goes base => 1st general
1) define base 2) define general (that shrinks)
3) always reach base
Parameters & Return Values
If parameter is local var. in subroutine, must be passed by value, not reference; local var. don't share memory location
Need multiple return statements for base & general case
Tracing
On call, entry made on 'call stack' in form of 'stack frame'
Trace Table - Call Number, Variables, Return Value
Alternative & Purpose
Every recursive subR has an iterative alt.
However most data structures are naturally recursive
Fewer lines of code; harder to trace, more memory
for stack frames, slower for stack operations