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

Week 5_slides

Uploaded by

carmi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Week 5_slides

Uploaded by

carmi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Introduction to Programming

ITPLA1-B44

Eduvos (Pty) Ltd (formerly Pearson Institute of Higher Education) is registered with the Department of Higher Education and Training as a private higher education institution under the
Higher Education Act, 101, of 1997. Registration Certificate number: 2001/HE07/008
Week 5: Lesson 9

Learning Outcome Assessment Criteria

1.Construct a program from a


design and use appropriate
2. Acquire the knowledge and skill to functions/procedures.
use modularization from the 2.Demonstrate the effect of scope
appropriate functions/procedures and the life-time of variables.
chosen programming language. 3.Demonstrate how to pass data
effectively between modules
Week 5: Lesson 9
Introduction

Topics
• modules and mudularization,
• hierarchy chart,
• value parameters and reference parameters,
• function procedures,
• call a function,
• functions with and without parameters.
What will be covered in
today’s lesson?
modules and mudularization

drawing a hierarchy chart


Week 5
Lesson 9 function procedures

value parameters and reference


parameters

functions with and without


parameters.
Modules and modularization
• Modularisation divides the solution to a problem into smaller pieces
called modules to improve the flexibility of a program and shorten its
development time.
• A module is therefore part of a program.
• Large and complex programs can be divided into independently
developed modules.
• Each module is made up of one or more statements to perform a specific
task.
• The various modules are then linked to form a working program.
Advantages
• Different programmers can design different program modules
independently, which is required in a large and complex program.
• it is easy to design code and test the program modules
independently.
• It is possible to use a single module in different places which reduces
program codes.
• Reduce redundancy
• Improving the readability of a program.
• Program maintenance becomes easier.
• Saving the time.
• Encourages re-use of code.
Modules and modularization (cont’d)

Modules can take several forms, depending on the programming language


used.
Two such forms are :
• function procedures and
• subprocedures.
What Happens Next?

Function
procedures
Function procedures

• A function procedure is a small, logical and functional part of a


program which performs the specific tasks and it returns a single
value to the main program or calling module.
• The returned value may be string or number.
• It is either called by using PRINT statement or storing a returned
value into a variable.
Function procedures (cont’d)

SYNTAX(Parameterized)
Declaration
DECLARE FUNCTION <name> (parameter list)
Definition
FUNCTION <name> (parameter list)
` procedure variable definitions and statements
<name> = Expression
END FUNCTION
Calling
PRINT <name>(argument list)
OR
variable = <name>(argument list)
Function procedures (cont’d)

SYNTAX(Non-Parameterized)
Declaration
DECLARE FUNCTION <name> ()
Definition
FUNCTION <name>
` procedure variable definitions and statements
<name> = Expression
END FUNCTION
Calling
PRINT <name>
OR
variable = <name>
Function procedures
Functions - Definition Structure
Function 'header' type function_name (type arg1, type arg2 )
Return data type
(if any) {
Name statements;
Descriptive
Arguments (or parameter list) }
Notice: data type and name
Statements
Variable declaration A function that calculates the product of two numbers
Operations
Return value (if any) double product(double x, double y)
{
double result;
result = x * y;
return result;
}
Functions - Example
Function prototype #include <stdio.h>
Like a variable declaration
Tells compiler that the function will be /* function prototype */
defined later
Helps detect program errors double product(double x, double y);
Note semicolon!!
Function definition int main()
See previous slide
Note, NO semicolon {
Function return double var1 = 3.0, var2 = 5.0;
return statement terminates execution of
the current function double ans;
Control returns to the calling function ans = product(var1, var2);
if return expression;
then value of expression is returned as printf("var1 = %.2f\n"
the value of the function call
Only one value can be returned this way "var2 = %.2f\n",var1,var2);
Function call printf("var1*var2 = %g\n", ans);
main() is the 'calling function'
product() is the 'called function' }
Control transferred to the function code /* function definition */
Code in function definition is executed
double product(double x, double y)
{
double result;
result = x * y;
return result;
}
Function used in C Code
Programmer’s block

Pre-processor directive

Main function (statements go between { } )

Declare and initialize


variables

While loop
(repetition structure)

return statement
Case Study

A company uses a clock card system to capture


hours that an employee worked. A receptionist uses
the card on which hours and days worked by an
employee have been recorded by the clock system
to manually calculate hours worked per day and
days worked per week. Overtime is paid one and
half of the pay rate.
Activity 1

Using the above scenario, write an algorithm to calculate the


net salary of the employee. Your program must ask input (hours
worked and hourly rate) from the user and calculate the net
pay of employee from a function procedure.
Quiz
1. returns a value after performing its specific task.
a) Function Procedure
b) Sub procedure
c) Sub block
d) Structure
2. The functions header and footer contains the
keyword.
a) Function
b) Struct
c) Sub
d) Class
3. A function’s header includes the data type.
a) As
b) To
c) From
d) By
Quiz
4. A function can receive information either by value or by
.
a) Reference
b) Address
c) Pointer
d) Value
5. Between the function’s and you enter
the instructions to process when the function is invoked.
a) Header and footer
b) Parameters and arguments
c) Call and return
d) Return type and function name
Quiz
6. statement is the last statement in the function.
a) Footer
b) Header
c) Return
d) Call
7. Each memory location listed in the parameterList in the
procedure header is referred to as .
a) an address
b) a constraint
c) a parameter
d) a value
Quiz
8. Which of the following is false?
a)The sequence of the arguments listed in the Call statement
should agree with the sequence of the parameters listed in the
receiving procedure’s header.
b)The data type of each argument in the Call statement should
match the data type of its corresponding parameter in the
procedure header.
c)The name of each argument in the Call statement should be
identical to the name of its corresponding parameter in the
procedure header.
d)When you pass information to a procedure by value, the
procedure stores a copy of each value it receives in a separate
memory location.
Quiz
9. To determine whether a variable is being passed to a
procedure by value or by reference, you will need to
examine .
a) the Call statement
b) the procedure header
c) the statements entered in the procedure
d) the procedure footer
10. Which of the following instructs a function to return the
contents of the decStateTax variable?
a) Return decStateTax
b) Return ByVal decStateTax
c) Send decStateTax
d) SendBack decStateTax
What Happens Next?

Subprocedures
Week 5: Lesson 10
Introduction

Topics
• the use of subprocedures,
• writing a statement to call a subprocedure, and writing the
corresponding subprocedure header and statements in the
subprocedure, and
• writing subprocedures with or without parameters.
What will be covered in
today’s lesson?

subprocedures

Week 5
Lesson 10 call a subprocedure

subprocedures with or
without parameters
Subprocedures

• SUB procedure is a block of statements which is identified by a


unique name that performs a specific task.
• The block of statements is placed with a pair of SUB … END SUB
statements and can be called by its name.
• A SUB-procedure is a small, logical and manageable functional part of
program which performs specific task and does not return any value.
Subprocedures (cont’d)

Features of SUB Procedures


• SUB procedure does not return any value.
• SUB procedure can be recursive. They may call themselves.
• Sub procedure do not have data types.
Subprocedures (cont’d)

SYNTAX (Parameterized)
Declaration
DECLARE SUB <name> (parameter list)
Definition
SUB <name> (parameter list)
` procedure variable definitions and
statements
END SUB
Calling
CALL <name>(argument list)
Subprocedures (cont’d)

SYNTAX (Non-Parameterized)
DECLARE SUB <name> ()

CALL <name>

SUB <name>
procedure variable definitions and
statements
END SUB
Subprocedures (cont’d)

Declaring SUB Procedure


The DECLARE statements is used to declare a SUB procedure.

The syntax for the DECLARE statements is as follows:


DECLARE SUB <name>(parameter list)
Example:
DECLARE SUB AOR(L,B)
Sub procedure AOR with two parameter L and B
DECLARE SUB AOR
Sub procedure AOR with no parameters
Subprocedures (cont’d)

Defining a SUB procedure


The SUB …… END SUB statements is a procedure statements that marks
the beginning and ending of a sub program.

The syntax is:

SUB <name>(parameter list)


procedure variable definitions and statements
END SUB
Subprocedures (cont’d)

Calling a SUB Procedure


The CALL statements is used to transfer control to another procedure, a
BASIC SUB program.

The syntax is :
CALL <name> (argument list)

Example:
CALL AOR(L,B)
Subprocedures
Differences
SUB Procedure Function Procedure

The block of statements placed under SUB/END SUB The block of statements placed under
statements and can be called by its name. FUNCTION/END FUNCTION statements and can be
invoked from the reference of the function name.

It does not return any value to the calling module It returns a single value to calling module.

It does not have any datatype in its name. It has datatype in its name which is the return value
of a function.
ARGUMENT AND PARAMETER
• The constants or variables enclosed in the parentheses are supplied to a
procedure are known as Arguments.
• Argument are also known as Actual Parameter.
• The variables in a procedure which accepts data or variable passed to
them from the calling module are known as Parameters.
• Parameter are also known as Formal Parameter.
LOCAL AND GLOBAL VARIABLE
• A variable which is declared inside a module and cannot be
accessed by other module is known as local variable.
• The variable in main module which ca be accessed from any
module or procedure of a program is known as Global variable.
Problem-Based Learning

What will the exact output be after each of the


following program segments been executed?
Activity 2
sub Lovely (valA, refB, valC, refD)
refD = valA
if refB mod 3 = 0 then
refB = refB + 2
endif
refD = valA + (valC mod 5)
valA = valA + 2
valC = valC + 1
end sub
MainAlgorithm
w = 0, x = 3, y = 8, z = 6
for m = 4 to 9 step 3
w=m
call Lovely(w,x,y,z)
display “w = ” , w , “ x = ” , x , “ y = ” , y , “z = ”, z
next m
end
Problem-Based Learning

What will the exact output be after each of the


following program segments been executed?
Activity 3
Activity 4
Activity 5
What Happens Next?
Sequential Text Files

You might also like