You are on page 1of 13

10

ICT
(Special Science Class)
Computer Programming
Quarter 4 – Module 5:
Function as Subprogram

1
Information and Communications Technology– Grade 10
Special Science Class
Alternative Delivery Mode
Quarter 3 – Module 5: Function as Subprogram
First Edition, 2021

Republic Act 8293, section 176 states that: No copyright shall subsist in any work of
the Government of the Philippines. However, prior approval of the government agency
or office wherein the work is created shall be necessary for exploitation of such work
for profit. Such agency or office may, among other things, impose as a condition the
payment of royalties.

Borrowed materials (i.e., songs, stories, poems, pictures, photos, brand names,
trademarks, etc.) included in this module are owned by their respective copyright
holders. Every effort has been exerted to locate and seek permission to use these
materials from their respective copyright owners. The publisher and authors do not
represent nor claim ownership over them.

Published by the Department of Education


Secretary: Leonor Magtolis Briones
Undersecretary: Diosdado M. San Antonio

Development Team of the Module


Writer: ALVIN JAY O. ALVARADO
Editor: RENANTE R. ALESER
Reviewer: MARGIE B. ENANORIA
Layout Artist: CARLA L. KONG
Subject Area Supervisor: MERIAM T. ABADILLA
Management Team: RONALD G. GUTAY, ESTELA B. SUSVILLA,
MARY JANE J. POWAO, AQUILO A. RENTILLOSA,
CRISTINA T. REMOCALDO
Division ADM Coordinator: RYAN B. REDOBLADO

Printed in the Philippines by:

Department of Education –Region7, Central Visayas

Office Address: Department of Education – Carcar City Division (Learning


Resources Management Section
P. Nellas St. Población III, Carcar City, Cebu
Telefax: (032)77561
E-mail Address: carcarcitydivision@yahoo.com

2
10

ICT
(Special Science Class)
Computer Programming
Quarter 4 – Module 5:
Function as Subprogram

3
Introductory Message

This Self-Learning Module (SLM) is prepared so that you, our dear learners, can
continue your studies and learn while at home. Activities, questions, directions,
exercises, and discussions are carefully stated for you to understand each lesson.
Each SLM is composed of different parts. Each part shall guide you step-by-step as
you discover and understand the lesson prepared for you.

Pre-tests are provided to measure your prior knowledge on lessons in each SLM. This
will tell you if you need to proceed on completing this module or if you need to ask your
facilitator or your teacher’s assistance for better understanding of the lesson. At the
end of each module, you need to answer the post-test to self-check your learning.
Answer keys are provided for each activity and test. We trust that you will be honest in
using these.

In addition to the material in the main text, Notes to the Teacher are also provided to
our facilitators and parents for strategies and reminders on how they can best help you
on your home-based learning.

Please use this module with care. Do not put unnecessary marks on any part of this
SLM. Use a separate sheet of paper in answering the exercises and tests. And read
the instructions carefully before performing each task.

If you have any questions in using this SLM or any difficulty in answering the tasks in
this module, do not hesitate to consult your teacher or facilitator.

Thank you.

4
What I Need to Know

This module discusses the subprograms in Pascal Programming, specifically


Functions.

The module contains one lesson:


• Lesson 5 – Pascal Programming-Functions

After going through this module, you are expected to:


Define Functions
Identify parts of functions
Apply Functions in writing subprograms

What I Know

Let us determine how much you already know about subprograms as provided by
PASCAL. Take this test.

Direction: Multiple Choice: Choose the letter of the correct answer


1. Which is a function that tells the compiler about a function name and how to
call the function.
a. Declaration b. body c. header d. footer
2. A ___________is a group of statements that together perform a task.
a. function b. declaration c. body d. header
3. Which among that provides the actual body of the function?
a. body b. header c declaration d. definition
4. This establishes the linkage between the calling program and the function
identifiers and also called the formal parameters.
a. Arguments c. Return Type
b. Local Declaration d. Function Body
5. Which contains a collection of statements that define what the function does?
a. function body c. function declaration
b. function header d. function type

5
What’s In

Direction. Rearrange the jumbled word/s which is described in the following


statements.

Sbuorpgarm 1. A _____________is a program unit/module that performs a particular


task.
fionctuns 2. A kind of subprograms provided by Pascal which return a single
value.
prucodrees 3. Also, a kind of subprograms provided by Pascal which do not return
a value directly.
ararys 4. The formal parameters list appearing in the function statement could
be simple or subscripted variables, ________ or structured variables,
or subprograms.
fionctun-tepy 5. The __________ is the data type of the value the function returns. It
may be standard, user-defined scalar or subrange type but it cannot be
structured type.

Lesson Pascal Programming-


1 Functions

What’s New

Sometimes the need arises to reuse a part of the code. It is no good programming to
copy this part again into the code and use it this way. Instead, PASCAL has
components, that is subprograms called procedures and functions for this purpose.
These two components are not only used for this purpose, but also to make the
program easier to debug.

There are two advantages of using subprograms:


1. Programs are easier to write, debug and read.
2. Using subprograms to evaluate a task that must be performed more than once,
avoids the need for repetition of the same instructions.

6
What is It

Pascal Programming – Functions

Subprograms
A subprogram is a program unit/module that performs a particular task. These
subprograms are combined to form larger programs. This is basically called the
'Modular design.' A subprogram can be invoked by a subprogram/program, which is
called the calling program.

Pascal provides two kinds of subprograms −


• Functions − these subprograms return a single value.
• Procedures − these subprograms do not return a value directly.

Functions
A function is a group of statements that together perform a task. Every Pascal
program has at least one function, which is the program itself, and all the most trivial
programs can define additional functions.

A function declaration tells the compiler about a function's name, return type, and
parameters. A function definition provides the actual body of the function.
Pascal standard library provides numerous built-in functions that your program can
call. For example, function AppendStr() appends two strings,
function New() dynamically allocates memory to variables and many more functions.

Defining a Function
In Pascal, a function is defined using the function keyword. The general form of a
function definition is as follows −
function name(argument(s): type1; argument(s): type2; ...):
function_type;
local declarations;

begin
...
< statements >
...
name:= expression;
end;

A function definition in Pascal consists of a function header, local declarations, and


a function body. The function header consists of the keyword function and
a name given to the function. Here are all the parts of a function −
• Arguments − The argument(s) establish the linkage between the calling
program and the function identifiers and also called the formal parameters. A
parameter is like a placeholder. When a function is invoked, you pass a value

7
to the parameter. This value is referred to as actual parameter or argument.
The parameter list refers to the type, order, and number of parameters of a
function. Use of such formal parameters is optional. These parameters may
have standard data type, user-defined data type or subrange data type.

The formal parameters list appearing in the function statement could be simple
or subscripted variables, arrays or structured variables, or subprograms.
• Return Type − All functions must return a value, so all functions must be
assigned a type. The function-type is the data type of the value the function
returns. It may be standard, user-defined scalar or subrange type but it cannot
be structured type.
• Local declarations − Local declarations refer to the declarations for labels,
constants, variables, functions, and procedures, which are application to the
body of function only.
• Function Body − The function body contains a collection of statements that
define what the function does. It should always be enclosed between the
reserved words begin and end. It is the part of a function where all
computations are done. There must be an assignment statement of the type
- name:= expression; in the function body that assigns a value to the function
name. This value is returned as and when the function is executed. The last
statement in the body must be an end statement.

Following is an example showing how to define a function in pascal −


(* function returning the max between two numbers *)
function max(num1, num2: integer): integer;

var
(* local variable declaration *)
result: integer;

begin
if (num1 > num2) then
result := num1

else
result := num2;
max := result;
end;

Function Declarations
A function declaration tells the compiler about a function name and how to call the
function. The actual body of the function can be defined separately.

A function declaration has the following parts −


function name(argument(s): type1; argument(s): type2; ...):
function_type.
For the above-defined function max(), following is the function declaration −
function max(num1, num2: integer): integer;

8
Function declaration is required when you define a function in one source file, and
you call that function in another file. In such case, you should declare the function at
the top of the file calling the function.

Calling a Function
While creating a function, you give a definition of what the function has to do. To use
a function, you will have to call that function to perform the defined task. When a
program calls a function, program control is transferred to the called function. A called
function performs defined task, and when its return statement is executed or when its
last end statement is reached, it returns program control back to the main program.

To call a function, you simply need to pass the required parameters along with function
name, and if function returns a value, then you can store returned value. Following is
a simple example to show the usage −
program exFunction;
var
a, b, ret : integer;

(*function definition *)
function max(num1, num2: integer): integer;
var
(* local variable declaration *)
result: integer;

begin
if (num1 > num2) then
result := num1

else
result := num2;
max := result;
end;

begin
a := 100;
b := 200;
(* calling a function to get max value *)
ret := max(a, b);

writeln( 'Max value is : ', ret );


end.

When the above code is compiled and executed, it produces the following result −
Max value is : 200

Arguments of a Subprogram
If a subprogram (function or procedure) is to use arguments, it must declare
variables that accept the values of the arguments. These variables are called
the formal parameters of the subprogram.

9
The formal parameters behave like other local variables inside the subprogram and
are created upon entry into the subprogram and destroyed upon exit.
While calling a subprogram, there are two ways that arguments can be passed to the
subprogram −
Sr.No Call Type & Description
1 Call by value
This method copies the actual value of an argument into the formal
parameter of the subprogram. In this case, changes made to the parameter
inside the subprogram have no effect on the argument.
2 Call by reference
This method copies the address of an argument into the formal parameter.
Inside the subprogram, the address is used to access the actual argument
used in the call. This means that changes made to the parameter affect the
argument.
By default, Pascal uses call by value to pass arguments. In general, this means that
code within a subprogram cannot alter the arguments used to call the subprogram.
The example program we used in the chapter 'Pascal - Functions' called the function
named max() using call by value.
Whereas the example program provided here (exProcedure) calls the procedure
findMin() using call by reference.

10
What’s More

Enumeration: Provide what is asked in the following statement/s.

(1-2) Two kinds of subprograms provided by Pascal


(3-6) Parts of a function.
(7-8) Ways that arguments can be passed to the subprogram
(9-10) Components of a function header

What I Have Learned

Direction. Complete the general form of a function definition by filling in the numbered
blanks. Refer your answer in the box.

a. end b. declarations c. argument d. begin e. expression

function name(argument(s): type1; 1._______(s): type2; ...): function_type;


local 2._______;

3. ______
...
< statements >
...
name:= 4._______expression;
5.___;

11
What I Can Do

Direction: Describe the following terms: (5 points each)

1. Function/s
2. Return type
3. Call by value
4. Call by reference

Assessment

Direction: Choose the letter of the correct answer and write it on a separate sheet.

1. Which is a function that tells the compiler about a function name and how to
call the function?
a. declaration b. body c. header d. footer
2. A ___________is a group of statements that together perform a task.
a. function b. declaration c. body d. header
3. Which among that provides the actual body of the function?
a. body b. header c declaration d. definition
4. This establishes the linkage between the calling program and the function
identifiers and also called the formal parameters.
a. Arguments c. Return Type
b. Local Declaration d. Function Body
5. Which contains a collection of statements that define what the function does?
a. function body c. function declaration
b. function header d. function type

12
Answer Key

A 5.
function-type 5. A 4.
arrays 4.
D 3.
procedures 3.
functions 2. A 2.
Subprogram 1. A 1.

What's In What I Know

References
 https://www.cmpe.boun.edu.tr/~say/c150/pascal/procfunc.htm
 https://www.tutorialspoint.com/pascal/pascal_functions.htm
 https://www.pascal-programming.info/lesson7.php

For inquiries or feedback, please write or call:

Department of Education – Carcar City Division (Learning Resources


Management Section)

P. Nellas St., Poblacion III, Carcar City, Cebu Philippines 6019


Tel. No. 4878495

Email Address: carcarcitydivision@yahoo.com

13

You might also like