Home Chemistry Physics HTML
Night More

Revise Now

GCE Subjects

Physics Chemistry CS Accounting

Coding Lessons

Learn HTML Learn SEO

More

What do you want
to do?

Contact us Support




more topic chapters

Procedures, Functions, Record Types & 2D Arrays


1D Array

We have seen what is an 1D array in the previous chapter

We will see how to access the elements in a 1D array

FOR i ← 1 TO 10
    OUTPUT Array[i]
    Array[i] ← 0
ENDFOR

This code first outputs the elements in the array and then sets the value of each element to 0


2D Array

We will see how to declare them

DECLARE ArrayName:ARRAY[1:10,1:20] OF STRING

This creates an array which has 10 rows and 20 columns and which can store only string values

If we want to store a value in the 2D array

ArrayName[1,5] ← 20

This stores the value 20 in 1st row and the 5th column

If we want to access the elements in the array

FOR Row ← 1 TO 10
     FOR Column ← 1 TO 20
              OUTPUT ArrayName[Row,Column]
     ENDFOR
ENDFOR

This displays all the elements in each row, row by row


Modules

These are functions or procedures which are able to do a sub task. These could be broken to further procedures and functions


Functions

A block of code which performs a specific task and returns a single value

You will need to know how to write them

FUNCTION functionName(parameter:datatypes,parameter2:datatypes) RETURN DATATYPE 
  .....
  RETURN Value
ENDFUNCTION

To call it from the main program

 variable ← functionName(arguments1,arguments2)

This returns a value

What are these parameters and arguments, they are the same thing and these are the values which must be passed from the main code to the modules

Also the datatypes of the parameters must be defined and also the return datatype

The parameters are entered in the same order as it is received by the procedure or function

Another point to remember is that the variables in a module are defined as local variables which means the variables in the module can be different from the main program code


Procedure

A block code which performs a specific task and can return either one or more values or no values at all

PROCEDURE ProcedureName(parameter:datatypes,parameter2:datatypes)
 .....
 .....
 .....
ENDPROCEDURE

To call this:

CALL procedureName(arguments1,argument2)

This doesn't return a value but values(parameters) are given to perform a task and output the result

Modules are considered to be self-contained and so the variables in the modules have no link with the main program

Let's see an example

PROCEDURE Totaller(value1:REAL,value2:REAL)
 DECLARE value3:REAL
 value3 ← value1+value2
 OUTPUT value3
ENDPROCEDURE

CALL Totaller(Num1,Num2)
Any variable created in the module, it must de declared inside the module

This reduces the size of the main code and make it easier to build a code

Also as, premade modules are tested it makes the code more reliable


BYVALUE AND BYREF

These are used to indicate how the parameters are supposed to be used

BYVALUE means only the value is passed to the module. So any changes to the variable has no affect in the main code

This is known as the local scope and by default python follows this

BYREF is when the value and the address of the variable is sent so any changes to the variable inside the module affects the main program

This is the global scope of a variable. We need to use a global keyword in python

PROCEDURE ProcedureName(BYVALUE parameter:datatypes, BYREF parameter2:datatypes)
 .....
 .....
 .....
ENDPROCEDURE

Record Type

These are also called userdefined types

These variables are very simialar to a record which can store different types of data of a particular item in one variable

In A2 we call it an instance and we use classes to implement these

So the variable can store many properties(field) of a particular item

We will see how to create a record type

TYPE RecordName
  DECLARE FIELDNAME:DATATYPE
  DECLARE FIELDNAME:DATATYPE
  DECLARE FIELDNAME:DATATYPE
  DECLARE FIELDNAME:DATATYPE
  .
  .
ENDTYPE

At first, even I was confused. But it's actually pretty simple

This defines a record. The fieldname defines a specific property of that particular record.

Let's take an example

TYPE Studentdetails
  DECLARE Name:STRING
  DECLARE Age:INTEGER
  DECLARE DateOfBIrth:DATE
ENDTYPE

So this record format tells us what a variable defined in this record type must have. So it is divided to age and all

Now if we declare a variable using the record type

DECLARE User1:Studentdetails

This is why it's called a user-defined type

So the variable John can store 3 different types of information under one identifier

User1.Name ← "MR Beast"
User1.Age ← 28
User1.DateOfBirth ← "7/5/1998"

This is very similar to an array but can hold different datatypes under one identifier and there is more structure to it than an array

Also remember that record datatypes are composite datatypes

Actually in advance python this is very similar to a dictionary in python


There are some points you need to remember

I'm not sure why but, the exams usually expect us to do this

Whenever we transfer a value from the array to a record or vice versa, we must use a temporary variable

Temporaryvariable ← User1.age
Agelist[i] ← Temporaryvariable

It is the same when you output values from a record. You need to have a temporary variable

Temporaryvariable ← User1.age
OUTPUT Temporaryvariable

Abstract Datatypes

A collection of data with associated properties

These are not actual datatypes but they are an idea or a concept

This is more of an A2 topic but, you need to know the basics

There are three types of abstract datastructures :


  • Stacks
  • These store data in a way that data is stored in a stack

    So data which is retrieved from the datastructure is the one which was recently stored( previously stored )

    In accounting terms, this is similar to the LIFO principle

    So this means that the data at the start remains at the bottom until the whole datalist is empty


  • Queues
  • Stores data in way that the data retrieved was the first one stored.

    This is similar to the FIFO principle

    So the data entered recenty will eventually be retrieved over time


  • Link lists
  • This has no proper order in which how data is stored or retrieved. This is defined by the user itself by using another array to define the order

    We will talk about it more in A2




    Recommended

    These are things you might like. Clicking these ads can help us improve our free services in the future...


    End of Chapter Questions
    Collection of Topic Questions to understand the topic more.

    Try out


    End of Chapter Videos
    Collection of Videos to Support Your Understanding.

    Remember these videos are handpicked by me and I feel these are the best ones out there. But I constantly update this list for each chapter. The Youtubers are more than welcome to contact me.

    Also, don't forget to Subscribe to our Youtube channel - Wiscus


    Watch