Procedural Programming Language Structure (PPLS)


PPLS:   SOLD       




In general, a procedure-based programming language consists of blocks/sub programs. which have two main parts, namely:

  1. Declaration Section
  2. Statement Section

1. Declaration Section

The declaration section is a part of the program to define the data type of a variable, constant, and function and procedure that will be used in the program. In addition, the declaration section can also be used to give an initial value to a variable. In other words, the declaration is used to introduce a name to the program compiler. Here is an example of a declaration:

a. Variable Declaration:

To declare a variable in Pascal, the reserved word var is used, followed by the name of the variable (identifier) ​​to be used, and then the data type of the variable. While in C, the declaration begins with the data type of the new variable followed by the variable name (identifier).

An identifier must begin with a non-numeric character, but must not contain special characters such as * , - + / \ = < > . ?  & and so on. In Pascal, identifiers are not case sensitive, meaning that uppercase and lowercase letters are considered the same. On the other hand, in C, identifiers are case sensitive, so that variables s and S will be considered two different identifiers.

b. Constant Declaration in Pascal and C:

const phi = 3.14;

Constant is a fixed value. So if we refer to the example above, the value of phi cannot be changed and will always be 3.14.

c. Pascal (left) and C (right) Data Type Declarations:

Data types can be grouped into:

1. Simple Data Type

Simple data type is the smallest data type, which only involves one data item, for example integer, string, real, Boolean, and so on. We can also define this data type ourselves. The self-defined data type is called enumerated data type (in the example is the day type).

2. Structured Data Types

Structured data type is a data type that consists of several data items. The form of this data type can be 

in the form of an array (consisting of items that have the same data type) or a record (consisting of items that may have different data types). In the example above, DataSiswa is a structured data type.

3. Pointer Data Type

Pointer data type is used to point to the memory address of another data. So the pointer data type basically does not store the data value directly, but only stores the address where the data is located. For example in Pascal language, TDataSiswa is a pointer data type. In C language, to declare a pointer for the DataSiswa data type in a variable named TDataSiswa, it can be written as follows:

DataSiswa *TDataSiswa;

d. Procedure/Function Declaration

If you look at the example of declaration in C language, you might ask what is the difference between procedure and function? In C language, all sub programs are considered functions, unlike Pascal which includes reserved words procedure and function to differentiate between the two.

Actually, the main difference between a procedure and a function is: a procedure is a function that does not return a value. On the other hand, a function is a procedure that returns a value. Referring to the example above, the add function will return a value of type integer, while the Print procedure does not return any value. In C Language, a procedure is basically a function that returns void, aka does not return any value.

2. Statement Section

The statement section is the part of the program that contains the commands to be executed/run. In Pascal, the statement section always begins with the reserved words begin and end. If the statement block is the main block of the program, then the reserved word end must be ended with a dot (.), conversely if the statement block is not the main block of the program then the reserved word end is ended with a semicolon (😉 . Conversely, in C, starting from the variable declaration to the end of the statement begins and ends with curly brackets { and }. The following is an example of a code snippet for the implementation of calculating the area of ​​a circle with Pascal (left) and C (right).

Here is a line-by-line explanation of the Pascal code snippet:

1. uses crt;

Reserved word uses are used to include libraries into a program. There are quite a lot of standard libraries/units in Pascal, including those for handling mathematical calculations, string manipulation, and so on.

2. const phi = 3.14;

This second line is used to declare the constant phi.

3. var diameter, radius,

Area: real;

This third line is used to declare the variables diameter, radius, and area with real data type (fractional numbers).

4. begin

This line indicates the start of the statement block.

5. readln(diameter);

The fifth line contains the readln command which functions to request input from the user, and then save it to the diameter variable.

6. radius := diameter / 2;

This sixth line performs the operation of dividing the diameter by two, then the result is stored in the radius variable (to get the radius from the diameter).

7. Area := phi radius radius;

This line again performs a mathematical operation that serves to calculate the area.

8. writeln(Area);

This line is used to print the contents of the Luas variable to the screen.

9. end.

This line marks the end of the main statement block.

Here is a line-by-line explanation of the C code snippet for the example above.

1. #include <stdio.h>

The line at the beginning of this program includes the stdio library header into the program. Like Pascal, C also has quite a lot of standard libraries that can be used.

2. void main()

This second line marks the beginning of the main statement block. In C, the main program block is a function/subprogram named 'main'.

3. { const phi = 3.14;

At the beginning of the third line, there are curly brackets as the opening of the statement block. Then the reserved word const is used to declare the constant phi.

4. float diameter, radius, area;

This fourth line is used to declare the variables diameter, radius, and area with the data type float (fractional number).

5. scanf("%f", &diameter);

The fifth line contains a command that functions to request float type input from the user, and then the value is saved to the diameter variable.

6. radius = diameter / 2.0;

7. Area = phi radius radius;

The sixth and seventh lines perform mathematical operations to calculate the area of ​​the circle.

8. printf("%f",Area);

This line is used to print the contents of a wide variable of type float.

9. }

This line marks the end of the statement block.


Post a Comment

Previous Next

نموذج الاتصال