0% found this document useful (0 votes)
14 views35 pages

2ch8 - Procedures Functions Arrays & File Handling

Uploaded by

unknownfighter9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views35 pages

2ch8 - Procedures Functions Arrays & File Handling

Uploaded by

unknownfighter9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

PROCEDURES AND FUNCTIONS

Procedures, functions and parameters


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.

Parameters:
Parameters are the variables that store the values of
the arguments passed to a procedure or function.
Some but not all procedures and functions will have
parameters.
When procedures and functions are defined, the
first statement in the definition is a header, which
contains:

» the name of the procedure or function

» any parameters passed to the procedure or


function, and their data type

» the data type of the return value for a function.

 Use of procedures and functions, with or without


parameters:
 Procedures without parameters

Example of a procedure without parameters in pseudocode :

 The procedure can then be called in the main part of the


program as many times as is required in the following way:
Procedures with parameters
Example of how a procedure with parameters can be
defined in pseudocode.
A procedure call must match the procedure definition. This
means that when a procedure is defined with parameters,
the arguments in the procedure call should match the
parameters in the procedure definition.

Note: For IGCSE Computer Science the number of parameters


used is limited to two.
Functions:
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.

 A function is just like a procedure except it always


returns a value.

Just like a procedure it is defined once and can be


called many times within a program.
Just like a procedure it can be defined with or
without parameters.
Unlike procedures, function calls are not standalone
and instead can be made on the right-hand side of
an expression.
Instead of naming them functions, different
terminology is used by some programming
languages.

Functions are known as:


» fruitful functions in Python
» functions in VB
» methods with returns in Java.
The keyword RETURN is used as one of the statements in a
function to specify the value to be returned.
 This is usually the last statement in the function definition.
For example, here is a function written in pseudocode to
convert a temperature from Fahrenheit to Celsius:
Just like with a procedure, a function call must
match the function definition. When a function is
defined with parameters, the arguments in the
function call should match the parameters in the
procedure definition.

Note: For IGCSE Computer Science the number of


parameters used is limited to two.
Difference between procedures and functions
 Procedure calls are single standalone statements. Function
calls are made as part of an expression, on the right-hand
side.

In contrast to a procedure, a function will return a value


back to the main program.

Similarities between procedures and functions


it is defined once and can be called many times within a
program.

 Just like a procedure,function can be defined with or


without parameters.
Local and global variables
A global variable can be used by any part of a program – its
scope covers the whole 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.

For example, in this algorithm the variables Number1,


Number2 and Answer are declared both locally and globally,
whereas Number3 is only declared locally.
 The final line is an error because the main program has tried to
access the variable Number3, which is local to the procedure.
Library Routines

Many programming language development systems include


library routines that are ready to incorporate into a program.
These routines are fully tested and ready for use.

 A programming language IDE usually includes a standard


library of functions and procedures.

 Each programming language has many library routines for


standard tasks that are commonly required by programs.
Creating a maintainable program
 Once a program is written, it may need to be maintained or updated
by another programmer at a later date.

 The programmer may have no documentation other than a copy of


the source program. Even a programmer looking at their own
program several years later may have forgotten exactly how all the
tasks in it were completed!

 A maintainable program should:


» always use meaningful identifier names for:
– variables
– constants
– arrays
– procedures
– functions
» be divided into modules for each task using:
– procedures
– functions
» be fully commented using your programming
language’s commenting feature.
Arrays
 An array is a data structure containing
several elements of the same data type
 These elements can be accessed using the
same identifier name.
 The position of each element in an array is
identified using the array’s index.
 Arrays are used to store multiple data items
in a uniformly accessible manner;
 All the data items use the same identifier
and each data item can be accessed
separately by the use of an index.
 In this way, lists of items can be stored, searched
and put into an order.

 For example, a list of names can be ordered


alphabetically, or a list of temperatures can be
searched to find a particular value.

The first element of an array can have an index of


zero or one.

Note: One-dimensional and two-dimensional arrays


are included the IGCSE Computer Science syllabus.
One-dimensional arrays
 When a one-dimensional array is declared in
pseudocode:

» the name of the array


» the first index value
» the last index value
» and the data type are included.

For example, to declare a new array called MyList:


Each position in the array can be populated in an array by
defining the value at each index position.
For instance, we can add the number 27 to the fourth position
in the array MyList as follows:

 To populate the entire array instead we can use a


loop:

 In this code we have used the variable Counter as the array index.
Two-dimensional arrays
A two-dimensional array can be referred to as a table, with
rows and columns.
Example of a table with 10 rows and 3 columns, which
contains 30 elements. The first element is located at
position 0,0.
 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:
The declared array can then be populated using a loop, just
like for one dimensional arrays – however this time there
need to be two nested loops, one for each index:
We can display the data that lies in a particular location in a
two-dimensional array as follows:

This would display the value 98.


 Notes on Python and arrays:
Instead of arrays, Python uses another object called a
list. The differences you need to know are:

» a list can contain different data types whereas arrays


must all hold the same type of data

» to achieve the same structure as a two-dimensional


array, Python embeds lists within another.
File handling
Importance of using files:
 Computer programs store data that will be
required again in a file. While any data stored in
RAM will be lost when the computer is switched
off, when data is saved to a file it is stored
permanently.
 Every file is identified by its filename.
 Data stored in a file can thus be accessed by the
same program at a later date or accessed by
another program.
 Data stored in a file can also be sent to be used on
other computer(s).

 The storage of data in files is one of the most used


features of programming.

Example:

 writing a line of text to a file

reading the line of text back from the file.


pseudocode
THANK YOU

You might also like