In our clan of gray haired AS400 and iSERIES programmers, we all have roots in the good old RPG LOGIC CYCLE. We have one foot in the old world of huge programs with tens of thousands of lines written in a very start-middle-end style, often with many subroutines. The other foot is in the modern RPG FREE FORMAT world and involves upgrading code from this older style to a more modern codebase.
This can be confusing as we step into the current programming paradigm, because we now get exposed to these programming styles called NOMAIN and LINEAR-MAIN for RPG ILE.
RPG Logic Cycle Example reading INPUT and writing OUTPUT:
// --------------------------------------------------------------
// program............. #cvtsavf
// function............ reroute save file data to/from *savf.
// --------------------------------------------------------------
FINPUT IP F 528 DISK
FOUTPUT O F 528 DISK
DRCDDTA DS 528
IINPUT NS 01
I 1 528 RCDDTA
OOUTPUT D 01
O RCDDTA 528
But you are not here to read about the RPG Logic Cycle.
You are here to checkout a more modern RPG programming idea – the idea of writing RPG code in the ILE style. So, let’s break down the advantages of using NOMAIN versus LINEAR in IBM RPG code.
**NOMAIN Modules
A NOMAIN module includes a Ctl-opt instruction with the NOMAIN keyword
- Modular Programming: NOMAIN modules allow you to create reusable procedures (Service Programs) that can be called by multiple programs. This promotes code reuse and modularity.
- No RPG Cycle: These modules do not include the RPG cycle, which means they don’t have the overhead associated with the cycle’s implicit logic.
- Flexibility: Since NOMAIN modules only contain subprocedures, they offer more flexibility in how you structure and call your code.
**Linear-Main Modules
- Simplified Control Flow: Linear-main modules have a straightforward control flow, as they do not include the RPG cycle. This can make the code easier to understand and maintain.
- Main Procedure: The main procedure in a linear-main module is coded as a subprocedure, which can simplify the structure of your program.
- Program Call: Linear-main procedures can only be called through a program call, which can be beneficial for certain types of applications where you want a clear entry point.
**Key Differences
- RPG Cycle: NOMAIN modules do not include the RPG cycle, while linear-main modules also exclude the cycle but have a main procedure.
- Calling Procedures: NOMAIN modules contain only subprocedures and cannot be the program-entry module, whereas linear-main modules have a main procedure that serves as the entry point.
Both approaches have their own advantages depending on the specific needs of your application. If you need reusable code that can be called from multiple programs, NOMAIN is a great choice.
If you prefer a simpler control flow with a clear entry point, linear-main might be more suitable.
Still Confused?
These coding styles seem very similar right?
So, let’s ask the question “Do RPG programs use linear main style use the logic cycle?“
No, RPG programs that use the linear main style do not use the RPG logic cycle. Instead, they have a straightforward control flow without the automatic features of the cycle. This means that linear main programs avoid the overhead associated with the RPG cycle, potentially improving performance.
Example of NOMAIN RPG code
Here’s an example of a NOMAIN RPG code module in AS400 that converts Fahrenheit to Celsius:
**free
Ctl-opt Nomain;
// Prototype for the Celsius procedure
dcl-pr Celsius int(5);
Fahrenheit int(5);
end-pr;
// Celsius procedure definition
dcl-proc Celsius export;
dcl-pi *n int(5);
Fahrenheit int(5);
end-pi;
// Local variable
dcl-s Temperature int(5);
// Conversion logic
Temperature = (5 / 9) * (Fahrenheit - 32);
// Return the result
return Temperature;
end-proc;
In this example:
- Ctl-opt Nomain: Indicates that this module does not have a main procedure.
- dcl-pr: Declares the prototype for the Celsius procedure.
- dcl-proc: Defines the Celsius procedure and marks it as exportable so it can be called from other modules.
- dcl-pi: Defines the procedure interface, specifying the input parameter.
- The conversion logic calculates the Celsius temperature from Fahrenheit and returns the result.
This module can be combined with other modules to create a commonly used service program.
Example of LINEAR RPG code
Here’s a simple example of a legacy style IBM-i RPG linear code program that calculates the sum of two numbers and displays the result:
**free
// Declare variables
dcl-s num1 int(10);
dcl-s num2 int(10);
dcl-s sum int(10);
// Assign values to variables
num1 = 10;
num2 = 20;
// Calculate the sum
sum = num1 + num2;
// Display the result
dsply ('The sum of ' + %char(num1) + ' and ' + %char(num2) + ' is ' + %char(sum));
// End the program
return;
In this example:
- dcl-s is used to declare variables.
- num1 and num2 are assigned values.
- The sum of num1 and num2 is calculated and stored in the sum variable.
- dsply is used to display the result.
This is a basic example to get you started.
If you have any specific requirements or need more complex examples, feel free to ask!
Thanks for the info. And you need a half adjust else 212°F converts to 99°C.
Eval(h)
Ringer
Pesky Medieval Temperature scales 😉