Вы находитесь на странице: 1из 18

Module

2
Basic Components of Informix 4GL

Objectives
At the end of this module, you will be able to:
„ Define the terms:
Š PROGRAM
Š MODULE
Š FUNCTION
Š FORM
„ Understand how these components make up an Informix 4GL program

Basic Components of Informix 4GL 1213 07-99 2-1


 2000 Informix Software, Inc.
The Program

Basic Components of Informix 4GL 1213 07-99 2

A program in Informix 4GL can handle a variety of application needs. It can


manage a small business or the finances of a major bank. An Informix 4GL
program contains all of the pieces necessary to perform a particular function.
Definition Program
A program is a logical sequence of operations to be performed by a computer in
solving a problem or in processing data.

Basic Components of Informix 4GL 2-2


A Detailed Look

Basic Components of Informix 4GL 1213 07-99 3

If you take a look at any program, there are basic things it will need to do. In the
case of a program to keep track of a sporting goods distributor, it will need to keep
track of customers, inventory, and orders. In Informix 4GL, this could be three
separate programs or one program that handles all three aspects. For the purposes
of this module, let us say you need to keep track of customers, and that this will be
a program by itself.
There are basic operations involved in keeping track of customers. These may
include adding new customers, deleting old ones, or changing the addresses of
existing ones. With these operations in mind, let us look at the Informix 4GL
components that would be required for a program to track customers.

Basic Components of Informix 4GL 2-3


The Module File

Program

cust_main.4gl cust_add.4gl cust_edit.4gl

Basic Components of Informix 4GL 1213 07-99 4

One of the building blocks of Informix 4GL is the module.


Definition Module
A module is a computer file of Informix 4GL instructions with a specific purpose or
function. It is one piece of the entire program. If you link a group of modules
together, you get an entire program. In the slide above, we have three modules
which, when linked together, make up the customer program.
How Many Modules?
How many of these files do you need to make up a program? The simple answer is
at least one. In general, you will need as many modules (files) as it takes to
accomplish your objective. This may seem like a vague answer, but the actual
number of modules is up to you.
In the slide above, the program to keep track of customers has been divided into three
modules. Each of these modules has a specific purpose. One module,
cust_main.4gl, will be used to drive or control the program. The module
cust_add.4gl will take care of all the code needed to add a new customer record. The
module cust_edit.4gl will contain all the Informix 4GL code needed to change or
delete a customer record.

Basic Components of Informix 4GL 2-4


Modules Contain Functions

Program
cust_edit.4gl
cust_add.4gl
.4gl
FUNCTION add_cust() FUNCTION delete_cust()
4GL code 4GL code
4GL code 4GL code
END FUNCTION END FUNCTION

FUNCTION insert_cust() FUNCTION update_cust()


4GL code 4GL code
4GL code 4GL code
END FUNCTION END FUNCTION

Basic Components of Informix 4GL 1213 07-99 5

A module contains a collection of program blocks or functions.


Definition Function
A function is a routine that performs a specific duty or job which may be required in
a program.
The function is the basic unit of Informix 4GL code. It is used to set off a series of
Informix 4GL statements which should be executed when called upon. It is possible
to have functions in an Informix 4GL program that are never used. For example, you
may never delete a customer, in which case the function delete_cust will never be
called upon to perform its given job.
How Many Functions?
How many of these functions do you need to make up a module? Again, the answer
is at least one. You will need as many functions as it takes to accomplish the goal of
the module.

Basic Components of Informix 4GL 2-5


In the case of the cust_edit.4gl module shown above, you will need one function to
delete the customer and one function to change the customer. In reality, you may
need many other functions to handle different operations. To delete a customer, you
might need a function to:
„ Find the customer.
„ Display the customer.
„ Verify that the customer is really to be deleted.
„ Delete the customer.
As you study the Informix 4GL language, you will become familiar with which
statements go together to form logical functions.

Basic Components of Informix 4GL 2-6


Program Blocks

Program
cust_main.4gl cust_add.4gl cust_edit.4gl

FUNCTION delete_cust()
GLOBALS FUNCTION add_cust() 4GL code
4GL code 4GL code 4GL code
END GLOBALS END FUNCTION END FUNCTION

FUNCTION update_cust()
MAIN REPORT cust_list()
4GL code
4GL code 4GL code
4GL code
END MAIN END REPORT
END FUNCTION

Basic Components of Informix 4GL 1213 07-99 7

In Informix 4GL executable statements are organized into larger units called
program blocks. Each block begins with a keyword, which tells Informix 4GL
what kind of program block to expect.
4GL modules can contain three types of program blocks:
MAIN
The MAIN statement defines the MAIN program block. Informix 4GL always starts
executing the MAIN program block first. It is the MAIN program block that drives
the rest of the program; therefore, every program requires exactly one MAIN
statement (otherwise Informix 4GL would not know where to start).
FUNCTION
The FUNCTION statement defines a FUNCTION program block. The function is
the basic unit for Informix 4GL code. It is the general purpose program block, unlike
MAIN or REPORT. You define what each function's purpose is going to be.
A function can be invoked from any module of your program.

Basic Components of Informix 4GL 2-7


REPORT
The REPORT statement defines a REPORT program block. REPORT contains 4GL
report formatting statements. Report formatting statements cannot be included in
other types of program blocks, since MAIN and FUNCTION will not understand
these report statements.
Note In all of the code examples, we have elected to use the convention of making 4GL
keywords upper case. Please note that 4GL keywords, functions and variable names
are not case sensitive.

Basic Components of Informix 4GL 2-8


The GLOBALS Statement

Program

cust_main.4gl cust_add.4gl cust_edit.4gl

FUNCTION delete_cust()
GLOBALS FUNCTION add_cust() 4GL code
4GL code 4GL code 4GL code
END GLOBALS END FUNCTION END FUNCTION

FUNCTION update_cust()
MAIN REPORT cust_list()
4GL code
4GL code 4GL code
4GL code
END MAIN END REPORT
END FUNCTION

Basic Components of Informix 4GL 1213 07-99 9

The GLOBALS statement allows you to extend the visibility of program variables
beyond the source module in which they are defined.
Defining global variables
„ The definition of a global variable must be contained in a globals file, a source
module that contains the GLOBALS statement. For any given global variable
there should be only one file which defines the variable.
The definition statement takes the form:
GLOBALS
DEFINE variable1 INTEGER,
variable2 CHAR(5),
...
END GLOBALS

„ The globals file cannot contain any other executable statements.


„ The globals file must be compiled and linked with the other object files.

Basic Components of Informix 4GL 2-9


Using global variables
„ Although there should be only one file which defines the variable there can be
many which refer to it. Every module which references the global variable
must contain a GLOBALS statement specifying the name of the globals file.
„ For this purpose, the GLOBALS statement takes the form:
GLOBALS "globalfilename.4gl"

„ The GLOBALS statement must appear before the first MAIN, FUNCTION or
REPORT program block.
Note
All versions of 4GL have allowed disjoint sections of an application to use disjoint
globals files as long as all the global object files were linked into the application.

Basic Components of Informix 4GL 2-10


The Form

cust_add.4gl
FUNCTION add_cust()
OPEN FORM customer
4GL code
Cust # [ ] END FUNCTION
Name [ ]
Add. [ ]
City [ ] FUNCTION insert_cust()
State [ ] 4GL code
Zip [ ] 4GL code
END FUNCTION

--customer.per
SCREEN
{
Cust # [ ]

Basic Components of Informix 4GL 1213 07-99 11

The form is a separate file that is used to specify what the appearance of the visual
screen display will look like during data entry and manipulation. There is quite a
bit of flexibility in how a form can look, depending on your needs.
Separate Entity
Forms are a separate entity from the rest of your program. They do not fit into the
categories of program, module, program block or function. Instead they become
their own category. Forms are compiled separately and are used by your program.
Forms can be used to display information from a database as well as to update,
delete, and insert information.

Basic Components of Informix 4GL 2-11


The Big Picture

Program
cust_add.4gl
cust_main.4gl cust_edit.4gl
FUNCTION add_cust()
4GL code
4GL code
END FUNCTION

FUNCTION insert_cust()
4GL code
4GL code
END FUNCTION

Forms Help Files

Basic Components of Informix 4GL 1213 07-99 12

A program in Informix-4GL contains all of the pieces necessary to perform a


particular function, such as managing a small store or analyzing scientific data.
The Modules
One of the basic building blocks of Informix-4GL is the module. A module is a
computer file of Informix-4GL instructions. It is one piece of the entire program. If you
link one or more modules together, you get the entire program.
The Functions
A module contains a collection of one or more program or functions. The function
is the basic container of 4GL code. It is used to set off a series of 4GL statements
which should be executed when called upon.
The Forms
Forms are used by your Informix-4GL program. They reside in files by themselves and
are compiled separately from your program.
Help Files
Custom Help files, like forms, are also independent of the application. They will be
discussed in another module.

Basic Components of Informix 4GL 2-12


True or False

You are only allowed one module to a program in


Informix-4GL. ______
There are three different types of program blocks. ______
You have no control over the layout of a form. ______
Forms are used by an Informix-4GL program. ______
There can be only one file which contains the
definition for a given global program variable. ______

Basic Components of Informix 4GL 1213 07-99 13

Classify the statements shown above as true or false.

Basic Components of Informix 4GL 2-13


Solutions
True or False Solutions

You are only allowed one module to a program in


Informix-4GL. F
There are three different types of program blocks. T
You have no control over the layout of a form. F
Forms are used by an Informix-4GL program. T
There can be only one file which contains the
definition for a given global program variable. T

Basic Components of Informix 4GL 1213 07-99 16

The solutions to the true or false statements are shown above.

Basic Components of Informix 4GL 2-16


You Should Now Complete

Case Study

1
Basic Components of Informix 4GL 1213 07-99 17

Basic Components of Informix 4GL 2-17