Please enable JavaScript.
Coggle requires JavaScript to display documents.
TOPIC 3 - Advanced Programming (Functions & Procedures (Functions…
TOPIC 3 - Advanced Programming
Functions & Procedures
Functions
Functions are called from an expression in the main program and return a value to it
Used because they use less lines of code, increasing the efficiency and reducing the size of the program.
Procedures
set of coded instructions that tell a computer how to run a program or calculation
The main difference between functions and procedures is that a function prints or returns something (outputs), but a procedure does not
Scope
The scope of a variable is where it can be accessed/loaded from
Local scope - accessible in the function it was declared
Data Structures/Arrays
1D arrays
Allows related data to be held in a list
Saves data in memory
Sort of like postboxes
Mutable - it can be changed.
2D arrays
Arrays inside an array
E.g. my2DArray = [[7, 3, 2, 8], [1, 4, 5, 6]]
Typing print(my2DArray[1][0]) would output the number 1
Manipulating arrays
.append()
.insert[]
.remove()
Tuples
Tuples
Tuples are used in Python to store several related
pieces of data. For example, Book name, Author, Genre.
Tuples are immutable. They can not be changed.
You can use the 'in' operator to check if an element
exists inside the tuple.
"An Inspector Calls" is the key, "J.B. Priestley" is value1 and "Murder Mystery" is value2
To create a Tuple, you must put it in a bracket and have each item be separated by a comma, as shown below:
Tuple = ("An Inspector Calls","J. B. Priestley","Murder Mystery")
Named Tuples
You can import
namedtuples
in python from
collections
The difference from other tuples is you can put multiple hold on let me get my booklet
The positives and negatives of subroutines
Negatives: :red_cross:
:red_cross: If one function is lost, then the whole code could be ruined.
:red_cross: If one function is coded slightly wrong the entire program is ruined.
Positives:
:check:
:check: When a section of code is used several times (repeatedly), there is no need to constantly rewrite the code.
:check: More efficient programs as less typing is required and this less mistakes.
:check: More efficient as there are less lines of code to decode and execute. This also means that there will be less memory requirements. The code will execute quicker.
:check: Subroutines help to improve the structure and organisation of code; breaking the code into smaller and well defined sections, which makes the program easier to read and understand.
:check: The program will be quicker to run.
Classes
Used to store lots of data
Can be called as many times as your like
Can hold as many different types of data as you like