In this section, we will explore the fundamental concepts of data types and variables in RPG programming. Understanding these concepts is crucial as they form the building blocks for any program you write.
What are Data Types?
Data types define the kind of data that can be stored and manipulated within a program. In RPG, data types are used to specify the type of data a variable can hold. Here are the primary data types in RPG:
- Character (CHAR): Used to store alphanumeric data.
- Numeric (NUM): Used to store numeric data, including integers and decimals.
- Date (DATE): Used to store date values.
- Time (TIME): Used to store time values.
- Timestamp (TIMESTAMP): Used to store date and time values together.
Table: Common Data Types in RPG
Data Type | Description | Example Values |
---|---|---|
CHAR | Alphanumeric characters | 'Hello', 'RPG123' |
NUM | Numeric values (integers, decimals) | 123, 45.67 |
DATE | Date values | '2023-10-01' |
TIME | Time values | '14:30:00' |
TIMESTAMP | Date and time values | '2023-10-01-14.30.00.000000' |
Declaring Variables
Variables are used to store data that can be manipulated by the program. In RPG, variables are declared using the DCL-S
(Declare Standalone) statement.
Syntax for Declaring Variables
<variable_name>
: The name of the variable.<data_type>
: The type of data the variable will hold.[initial_value]
: (Optional) The initial value assigned to the variable.
Example: Declaring Variables
DCL-S myChar CHAR(10) INZ('Hello'); DCL-S myNum NUM(5) INZ(123); DCL-S myDate DATE INZ(D'2023-10-01'); DCL-S myTime TIME INZ(T'14:30:00'); DCL-S myTimestamp TIMESTAMP INZ(TS'2023-10-01-14.30.00.000000');
Explanation
myChar
: A character variable with a length of 10, initialized to 'Hello'.myNum
: A numeric variable with a length of 5, initialized to 123.myDate
: A date variable initialized to '2023-10-01'.myTime
: A time variable initialized to '14:30:00'.myTimestamp
: A timestamp variable initialized to '2023-10-01-14.30.00.000000'.
Practical Example
Let's create a simple RPG program that declares variables of different data types and prints their values.
Code Example
**free // Declare variables DCL-S myChar CHAR(10) INZ('Hello'); DCL-S myNum NUM(5) INZ(123); DCL-S myDate DATE INZ(D'2023-10-01'); DCL-S myTime TIME INZ(T'14:30:00'); DCL-S myTimestamp TIMESTAMP INZ(TS'2023-10-01-14.30.00.000000'); // Print variable values dsply ('Character: ' + myChar); dsply ('Numeric: ' + %char(myNum)); dsply ('Date: ' + %char(myDate)); dsply ('Time: ' + %char(myTime)); dsply ('Timestamp: ' + %char(myTimestamp)); *inlr = *on;
Explanation
- The
DCL-S
statements declare variables of different data types. - The
dsply
statements display the values of the variables. - The
%char
built-in function converts numeric, date, time, and timestamp values to character format for display purposes. - The
*inlr = *on;
statement indicates the end of the program.
Exercises
Exercise 1: Declare and Print Variables
- Declare a character variable
greeting
with a length of 20 and initialize it to 'Welcome to RPG'. - Declare a numeric variable
age
with a length of 3 and initialize it to 25. - Declare a date variable
birthDate
and initialize it to '1998-05-15'. - Print the values of these variables.
Solution
**free // Declare variables DCL-S greeting CHAR(20) INZ('Welcome to RPG'); DCL-S age NUM(3) INZ(25); DCL-S birthDate DATE INZ(D'1998-05-15'); // Print variable values dsply ('Greeting: ' + greeting); dsply ('Age: ' + %char(age)); dsply ('Birth Date: ' + %char(birthDate)); *inlr = *on;
Exercise 2: Modify and Print Variables
- Declare a numeric variable
score
with a length of 4 and initialize it to 100. - Modify the value of
score
to 150. - Print the new value of
score
.
Solution
**free // Declare variable DCL-S score NUM(4) INZ(100); // Modify variable value score = 150; // Print new variable value dsply ('Score: ' + %char(score)); *inlr = *on;
Common Mistakes and Tips
- Uninitialized Variables: Always initialize variables to avoid unexpected behavior.
- Data Type Mismatch: Ensure that the data type of the variable matches the type of data you intend to store.
- Variable Naming: Use meaningful variable names to make your code more readable.
Conclusion
In this section, we covered the basics of data types and variables in RPG programming. We learned how to declare variables, initialize them, and print their values. Understanding these concepts is essential as they are the foundation for more complex programming tasks. In the next section, we will delve into operators and expressions, which will allow us to perform various operations on these variables.
RPG Programming Course
Module 1: Introduction to RPG Programming
Module 2: Core Concepts
Module 3: Working with Data
Module 4: Advanced Programming Techniques
Module 5: RPG IV and Beyond
Module 6: Integrating RPG with Modern Technologies
Module 7: Real-World Applications
- Building a Simple Application
- Case Study: Inventory Management System
- Case Study: Payroll System
- Best Practices and Code Review