Page 1 of 1

Subroutines (procedures and functions)

Posted: Sun Jan 05, 2025 6:59 am
by jewameb621
Conditional operators


Pascal supports conditional statements, which allow you to perform different actions based on conditions. Here is an example of a program that determines whether a number is even or odd:

In this example:

- `write('Enter a number: ');` is a command to display a prompt for a number.

- `readln(number);` is a command to read the spain telegram phone numbers number entered by the user and store it in the `number` variable.

- `if number mod 2 = 0 then` is a conditional statement. If the number is divisible by 2 without a remainder (`number mod 2 = 0`), the program displays a message that the number is even, otherwise - that it is odd.

Cycles


Pascal also supports loops, which allow you to execute a block of code multiple times. Here is an example of a program that prints the 5 multiplication table:

```pascal
program MultiplicationTable;
var
i: integer;
begin
writeln('5 multiplication table:');


for i := 1 to 10 do
writeln('5 ', i, ' = ', 5 i);
end.
```

In this example:

- `for i := 1 to 10 do` is a loop that executes a block of code from 1 to 10 times (the value of the `i` variable changes from 1 to 10).

- `writeln('5 ', i, ' = ', 5 i);` is a command to print a string with the results of the multiplication.



Pascal allows you to create subroutines (procedures and functions) that can be called from the main program. An example of a program that uses a procedure to greet a user:

```pascal
program GreetUser;
procedure Greet(name: string);
begin
writeln('Hello, ', name, '!');
end;
var
userName: string;
begin
write('Enter your name: ');
readln(userName);
Greet(userName); // call the Greet procedure
end.
```

In this example:

- `procedure Greet(name: string);` is a declaration of the `Greet` procedure, which takes a `name` argument of type `string` and prints a greeting with this name.

- `Greet(userName);` is a call to the `Greet` procedure with the `userName` argument.

These are just the basics of the structure of a Pascal program. Pascal also supports many other features, such as arrays, records, files, etc. It can be used to develop both small educational programs and more complex applications. I hope this article has helped you understand the basics of Pascal program structure.