0% found this document useful (0 votes)
4 views

8.1.6 Procedures and functions, Library Routines, Local and Global Variables

The document discusses the use of procedures and functions in programming, highlighting their definitions, differences, and how to implement them with parameters. It also covers library routines, local and global variables, and emphasizes the importance of creating maintainable programs through meaningful naming, modular design, and thorough commenting. Examples in pseudocode illustrate the concepts of functions for various tasks, such as temperature conversion and summation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

8.1.6 Procedures and functions, Library Routines, Local and Global Variables

The document discusses the use of procedures and functions in programming, highlighting their definitions, differences, and how to implement them with parameters. It also covers library routines, local and global variables, and emphasizes the importance of creating maintainable programs through meaningful naming, modular design, and thorough commenting. Examples in pseudocode illustrate the concepts of functions for various tasks, such as temperature conversion and summation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

8.1.

6 Procedures and functions


8.1.7 Library routines
8.1.8 Creating a maintainable
program

Computer Science 2210


Compiled By: Bilal Khan
8.1.6 Procedures and functions
When writing an algorithm, there are often similar
tasks to perform that make use of the same
groups of statements.
Instead of repeating these statements and writing
new code every time they are required, many
programming languages make use of
subroutines, also known as named procedures or
functions.
These are defined once and can be called many
times within a program.
Computer Science 2210
Compiled By: Bilal Khan
Procedures and functions
Procedure or 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.

Computer Science 2210


Compiled By: Bilal Khan
Difference between Procedures and
functions
• Procedures • Functions
perform a specific manipulate data
task but do not and return a value.
return a value.

Computer Science 2210


Compiled By: Bilal Khan
Procedures

Procedure Name

PROCEDURE Stars
OUTPUT "************"
ENDPROCEDURE

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


program as many times as is required in the following way:

CALL Stars
Computer Science 2210
Compiled By: Bilal Khan
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.

Computer Science 2210


Compiled By: Bilal Khan
Procedures with parameters
Parameter with data type
Procedure Name

PROCEDURE Stars (Number : INTEGER)


DECLARE Counter : INTEGER
FOR Counter ← 1 TO Number
OUTPUT "*"
Procedure with parameters are
NEXT Counter called like this – in this case to
print seven stars:
ENDPROCEDURE
CALL Stars (7)
OR
Number ← 7
CALL stars (Number)
Computer Science 2210
Compiled By: Bilal Khan
Function:
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.

Computer Science 2210


Compiled By: Bilal Khan
Example: Function written in pseudocode to convert a
temperature from Fahrenheit to Celsius:
Function name parameter with data type

FUNCTION Celsius (Temperature : REAL) RETURNS REAL

RETURN (Temperature – 32) / 1.8


data type of the return value
ENDFUNCTION for a function

Note:
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 Computer
value for a function.
Science 2210
Compiled By: Bilal Khan
Because a function returns a value, it can be
called by assigning the return value directly into
a variable as follows:

MyTemp ← Celsius(MyTemp)

Computer Science 2210


Compiled By: Bilal Khan
Practice Question 1:

Write a Function in pseudocode to


convert inches to centimeters:

Inches * 2.4

Computer Science 2210


Compiled By: Bilal Khan
Function in pseudocode to convert
inches to centimeters:

FUNCTION ConvertToCm(Inches: REAL) RETURNS REAL

RETURN Inches * 2.4

ENDFUNCTION

Computer Science 2210


Compiled By: Bilal Khan
Practice Question 2:
Write a Function in pseudocode to
calculate the sum of two numbers:

Computer Science 2210


Compiled By: Bilal Khan
Function in pseudocode to
calculate the sum of two numbers:

FUNCTION Sum(Num1, Num2 : INTEGER) RETURNS INTEGER

RETURN Num1 + Num2

ENDFUNCTION

Computer Science 2210


Compiled By: Bilal Khan
DECLARE Number1, Number2, Total : INTEGER

FUNCTION Sum(Num1, Num2 : INTEGER) RETURNS INTEGER

RETURN Num1 + Num2

ENDFUNCTION

Main Program

OUTPUT "Enter two numbers"

INPUT Number1, Number 2

Total ← Sum(Number1, Number2)

OUTPUT Total
Computer Science 2210
Compiled By: Bilal Khan
Practice Question 3:

Write a Function in pseudocode to


calculate the square of a number.

Computer Science 2210


Compiled By: Bilal Khan
Function in pseudocode to calculate
the square of a number:

FUNCTION CalculateSquare(Number : INTEGER) RETURNS INTEGER

RETURN number * number

ENDFUNCTION

Computer Science 2210


Compiled By: Bilal Khan
DECLARE Num, SquareResult : INTEGER

FUNCTION CalculateSquare(Number : INTEGER) RETURNS INTEGER

RETURN number * number

ENDFUNCTION

Main Program

OUTPUT "Enter a number"

INPUT Num

SquareResult ← CalculateSquare(Num)

OUTPUT SquareResult
Computer Science 2210
Compiled By: Bilal Khan
8.1.7 Library routines
Library routines are fully tested and ready for use in a
program. An IDE includes a standard library of functions
and procedures.
Library routines Pseudocode Description
returns remainder of a
MOD Value1 ← MOD(10, 3) division.
(10 divided by 3, Outputs 1)
returns the quotient (that
is, the whole number part)
DIV Value2 ← DIV(10, 3)
of a division.
(10 divided by 3, Outputs 3)
returns a value rounded to
a given number of decimal
ROUND Value3 ← ROUND(6.97354, 2)
places.
(Outputs 6.97)
returns a random number
RANDOM Value4 ← RANDOM()
Computer Science 2210
between 0 and 1 inclusive
Compiled By: Bilal Khan
Local and global variables
• A global variable • A local variable
can be used in any can only be used
part of a program by the part of the
– its scope covers program it has
the whole been declared in;
program. it is restricted to
that part of the
program.
Computer Science 2210
Compiled By: Bilal Khan
Computer Science 2210
Compiled By: Bilal Khan
8.1.8 Creating a maintainable program

A program should be understandable to another


programmer.
A maintainable program should:

▪ always use meaningful identifier names for variables, constants,


arrays, procedures and functions

▪ be divided into modules for each task using procedures and


functions

▪ be fully commented using your programming language’s commenting


feature.
Computer Science 2210
Compiled By: Bilal Khan

You might also like