Home Pelajaran Turbo Pascal Beberapa Contoh Program Pascal Sederhana Beberapa Contoh Program Pascal Sederhana Tuesday, February 5, 2013 4komentar Program Menghitung Jumlah Bilangan Perulangan FOR Dengan Nilai Statis. Skrip Program Pascal. Output Program. Program jumbil; uses wincrt; var i, jum,bil: integer; begin. Scrieti un program care converteste un nr in baza 2,rezultatul fiind un sir de car format din 0 si 1 Program2 scrieti un program care converteste un nr in baza 2.
- Pascal Tutorial
- Pascal Useful Resources
- Selected Reading
Subprograms
A subprogram is a program unit/module that performs a particular task. These subprograms are combined to form larger programs. This is basically called the 'Modular design.' A subprogram can be invoked by a subprogram/program, which is called the calling program.
Pascal provides two kinds of subprograms −
- Functions − these subprograms return a single value.
- Procedures − these subprograms do not return a value directly.
Functions
A function is a group of statements that together perform a task. Every Pascal program has at least one function, which is the program itself, and all the most trivial programs can define additional functions.
A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.
Pascal standard library provides numerous built-in functions that your program can call. For example, function AppendStr() appends two strings, function New() dynamically allocates memory to variables and many more functions.
Defining a Function
In Pascal, a function is defined using the function keyword. The general form of a function definition is as follows −
A function definition in Pascal consists of a function header, local declarations and a function body. The function header consists of the keyword function and a name given to the function. Here are all the parts of a function −
- Arguments − The argument(s) establish the linkage between the calling program and the function identifiers and also called the formal parameters. A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of parameters of a function. Use of such formal parameters is optional. These parameters may have standard data type, user-defined data type or subrange data type.The formal parameters list appearing in the function statement could be simple or subscripted variables, arrays or structured variables, or subprograms.
- Return Type − All functions must return a value, so all functions must be assigned a type. The function-type is the data type of the value the function returns. It may be standard, user-defined scalar or subrange type but it cannot be structured type.
- Local declarations − Local declarations refer to the declarations for labels, constants, variables, functions and procedures, which are application to the body of function only.
- Function Body − The function body contains a collection of statements that define what the function does. It should always be enclosed between the reserved words begin and end. It is the part of a function where all computations are done. There must be an assignment statement of the type - name := expression; in the function body that assigns a value to the function name. This value is returned as and when the function is executed. The last statement in the body must be an end statement.
Following is an example showing how to define a function in pascal −
Function Declarations
A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately.
A function declaration has the following parts −
For the above-defined function max(), following is the function declaration −
Function declaration is required when you define a function in one source file and you call that function in another file. In such case, you should declare the function at the top of the file calling the function.
Calling a Function
While creating a function, you give a definition of what the function has to do. To use a function, you will have to call that function to perform the defined task. When a program calls a function, program control is transferred to the called function. A called function performs defined task, and when its return statement is executed or when it last end statement is reached, it returns program control back to the main program.
To call a function, you simply need to pass the required parameters along with function name, and if function returns a value, then you can store returned value. Following is a simple example to show the usage −
When the above code is compiled and executed, it produces the following result −
- Pascal Tutorial
- Pascal Useful Resources
- Selected Reading
Before we study basic building blocks of the Pascal programming language, let us look a bare minimum Pascal program structure so that we can take it as a reference in upcoming chapters.
Pascal Program Structure
A Pascal program basically consists of the following parts −
- Program name
- Uses command
- Type declarations
- Constant declarations
- Variables declarations
- Functions declarations
- Procedures declarations
- Main program block
- Statements and Expressions within each block
- Comments
Every pascal program generally has a heading statement, a declaration and an execution part strictly in that order. Following format shows the basic syntax for a Pascal program −
Pascal Hello World Example
Following is a simple pascal code that would print the words 'Hello, World!' −
This will produce following result −
Let us look various parts of the above program −
- The first line of the program program HelloWorld; indicates the name of the program.
- The second line of the program uses crt; is a preprocessor command, which tells the compiler to include the crt unit before going to actual compilation.
- The next lines enclosed within begin and end statements are the main program block. Every block in Pascal is enclosed within a begin statement and an end statement. However, the end statement indicating the end of the main program is followed by a full stop (.) instead of semicolon (;).
- The begin statement of the main program block is where the program execution begins.
- The lines within (*...*) will be ignored by the compiler and it has been put to add a comment in the program.
- The statement writeln('Hello, World!'); uses the writeln function available in Pascal which causes the message 'Hello, World!' to be displayed on the screen.
- The statement readkey; allows the display to pause until the user presses a key. It is part of the crt unit. A unit is like a library in Pascal.
- The last statement end. ends your program.
Compile and Execute Pascal Program
- Open a text editor and add the above-mentioned code.
- Save the file as hello.pas
- Open a command prompt and go to the directory, where you saved the file.
- Type fpc hello.pas at command prompt and press enter to compile your code.
- If there are no errors in your code, the command prompt will take you to the next line and would generate hello executable file and hello.o object file.
- Now, type hello at command prompt to execute your program.
- You will be able to see 'Hello World' printed on the screen and program waits till you press any key.
Make sure that free pascal compiler fpc is in your path and that you are running it in the directory containing source file hello.pas.