Unit 8 :
Programmin
g
Agenda
• Difference between Functions and
Procedures
• Local and Global Variables
• String Operations
• File Handling
• 2D Arrays
Functions and Procedures
• A procedure is a set of programming
statements grouped together under a single
name that can be called to perform a task at
any point in a program.
• A function is a set of programming statements
grouped together under a single name that can
be called to perform a task at any point in a
program. In contrast to a procedure, a function
will return a value back to the main
program.
Parameter
• Parameters are the variables that store
the values of the arguments passed to a
procedure or function.
Defining a Procedure
PROCEDURE Stars
OUTPUT "************"
ENDPROCEDURE
Calling a Procedure
PROCEDURE Stars
OUTPUT "************"
ENDPROCEDURE
CALL Stars
Procedure with Parameter
PROCEDURE Stars (Number : INTEGER)
FOR Counter ← 1 TO Number
OUTPUT "*"
NEXT Counter
ENDPROCEDURE
INPUT Num
CALL Stars (Num)
For IGCSE Computer Science the number of parameters used is limit
Defining and Calling a Function
FUNCTION Celsius (Temperature : REAL) RETURNS REAL
RETURN (Temperature – 32) / 1.8
ENDFUNCTION
MyTemp ← Celsius(MyTemp)
Because a function returns a value, it can be called by assigning the return value directly
Local and global variables
A global variable can be used by any part of a program –
its scope covers the whole program. Value of variable can be
affected anywhere in the program.
A local variable can only be used by the part of the
program it has been declared in – its scope is restricted
to that part of the program. Value of variable can be affected
only in the part it has been created.
String
Handling
Strings are used to store text. Every
string contains a number of characters,
from an empty string, which has no
characters stored, to a maximum number
specified by the programming language.
The characters in a string can be labelled
by position number. The first character in
a string can be in position zero or
position one, depending on the language.
LENGTH
length is used to find the number of characters in the string.
For example, the length of the string "Computer Science" is
16 characters as spaces are counted as a character.
SUBSTRING
SUBSTRING is used to extract part of a string. For example,
the substring "Science" could be extracted from "Computer
Science"
UPPER
UPPER is used to convert all the letters in a string to
uppercase. For example, the string "Computer Science" would
become "COMPUTER SCIENCE"
LOWER
LOWER is used to convert all the letters in a string to
lowercase. For example, the string "Computer Science" would
become "computer science"
Library routines
MOD – returns remainder of a division
DIV – returns the quotient (i.e. the whole number part) of a division
ROUND – returns a value rounded to a given number of decimal places
RANDOM – returns a random number.
Library routines
Value1 ¨ MOD(10,3) returns the remainder of 10 divided by 3
Value2 ¨ DIV(10,3) returns the quotient of 10 divided by 3
Value3 ¨ ROUND(6.97354, 2) returns the value rounded to 2
decimal places
Value4 ¨ RANDOM() returns a random number between 0 and
1 inclusive
PURPOSE OF STORING DATA
IN A FILE
Computer programs store data that will be required
again in a file.
Data stored in RAM will be lost when the computer is
switched off, when data is saved to a file it is stored
permanently.
Data stored in a file can thus be accessed by the same
program later or accessed by another program.
Data stored in a file can also be sent to be used on
other computer(s).
File
Handling
READ , WRITE A LINE OF TEXT TO A
FILE (PSEUDOCODE)
TWO DIMENSIONAL ARRAY
A two-dimensional array can
be referred to as a table, with
rows and columns. Here is an
example of a table with 10
rows and 3 columns, which
contains 30 elements. The first
element is located at position
0,0.
Declaring a 2D Array
When a two-dimensional array is declared in
pseudocode:
• the first index value for rows
• the last index value for rows
• the first index value for columns
• the last index value for columns
and the data type are included.
For example:
DECLARE MyTable : ARRAY[0:9,0:2] OF INTEGER
Populating a 2D Array
FOR ColumnCounter ← 0 TO 2
FOR RowCounter ← 0 TO 9
OUTPUT "Enter next value "
INPUT MyTable[RowCounter, ColumnCounter]
NEXT RowCounter
NEXT ColumnCounter
Thank you