Programming Constructs |
|
This Help section explains basic programming constructs using the building blocks described under Language Elements. See Directives to access information on the syntax elements.
While PxPlus is an extensible language that has the flexibility to incorporate new functionality and sophisticated coding techniques, the more advanced capabilities are built upon universal concepts. Therefore, learning PxPlus begins with some programming fundamentals (described below).
The PxPlus tools for writing and modifying program code are described under Development Tools.
Order of Execution
Directives can be executed individually from the command line, or they can be entered as numbered statements to be saved in memory before execution. See Directives, Statements and Programs.
Program statements are grouped into the constructs for receiving and manipulating data, doing calculations, and printing output. During execution, the statements are evaluated and processed in the order they are read by the system. Logic flows through all the statements in one pass, from left to right and top to bottom, until the last statement is processed - this direction remains fixed unless specific commands are used to change this sequence.
Changing the Sequence
PxPlus employs various Flow Control mechanisms in order to perform conditional or repetitive instructions or to improve structure and maintainability of a program. Statements may be subject to certain conditions (decisions), executed repeatedly (loops), or packaged into code modules (subroutines/subprograms) that can be accessed from different points in the main program. See Modular Programming Facilities.
Like most programming languages, PxPlus maintains a type of data buffer called a stack, which is used to store dynamic information associated with active counters, loops and subroutines during execution of a program. For example, the primary purpose of a subroutine stack is to keep track of the location in the program (address) to which each procedure will return control when it is completed. At the start of every subroutine, a new return address will be placed on the top of the stack. When the procedure finishes, it pops the return address off the stack and transfers control to that address.
This type of information is continuously stacking up and unstacking the buffer as the program requires. An operational error that causes a stack to exceed its buffer allocation is called a stack overflow.
Input/Output Operations
Input/Output (I/O) in PxPlus programming refers to activities where source data is accepted into a program for processing or where resultant data is sent from a program for storage or display. Depending on the process, I/O may take place to or from a device or file, with or without user interaction. Interfaces can include the keyboard, mouse, monitor, printer, and a variety of other connected devices.
For an introduction to input/output operations (at the PxPlus console), see Basic Input and Output. For an introduction to graphical user interface development, see Graphical User Interfaces.
Data can take different forms: defined as Numeric Values or String Values, presented as Literals, or stored in Variables. Once a session is terminated, the processed data also disappears. However, if a more permanent storage solution is required, the data can be saved to a data file. For information on the PxPlus file system and file I/O operations, see File Handling.
During execution, files, as well as devices, are opened for access via the OPEN directive. See Opening/Closing Devices and Files.
Modular Programming Facilities
As an application becomes larger and more complex, it becomes increasingly difficult to keep track of where certain procedures are to be executed. PxPlus allows you to organize and extend the built-in capabilities of the language using modular programming facilities, referred to as Called Procedures. Commonly used expressions or calculations can be packaged into user-defined functions for reference by name elsewhere in the program. A similar approach includes transferring execution to subroutines (inside) or separately written subprograms (outside) from points in the main program.
The PxPlus language supports programming techniques where the coded lines are not necessarily written and executed as a fixed series of statements. This flexibility enables the use of different constructs that are better-suited for incorporating new user functionality and advanced programming paradigms; i.e. Event-Driven Methodology, Graphical User Interfaces and Object-Oriented PxPlus.
Event-Driven Programming
Traditionally, programs operated in a sequential fashion: they received some data, did some processing, produced output, received more data, and so on. In event-driven programming, processing is invoked only in response to specific inputs or events. Each call to a single subroutine or function is a sequential process, but the application as a whole does nothing until it is triggered by an event.
Graphical User Interfaces are event driven by nature. Graphical user interface based programs must run continuous event loops to check for, capture and process the different sources of user input; e.g. dragging a mouse, clicking a button, and pulling down a menu.
Object-Oriented Programming
While the functionality of a graphical user interface based application appears simple and natural to the user, the algorithms behind this type of interface design can be extremely complex. One of the more efficient ways in which graphical, event-handling operations can be expressed in PxPlus is via Object-Oriented Programming (OOP).
The idea of object orientation is to keep data and processing methods together as a single indivisible thing - an object. As an example, each element of a graphical user interface can be represented by one object. The object determines both the state and the behavior of the corresponding elements. For example, an object representing a window would have data for its position, size and title. If the user closes the window, a message is sent to the window object telling it to close itself. The window then executes a process that erases its image from the desktop.