Week 5_slides
Week 5_slides
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
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
Function
procedures
Function procedures
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
While loop
(repetition structure)
return statement
Case Study
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
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)
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