Pascal - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Pascal Functions

Pascal Functions

shape Description

Functions can be defined as a partitioned blocks of code in a program, which can be called any number of times during single execution of a program. The information is passed to the functions by using arguments. The following are the advantages of the functions.

Defining a Function

shape Description

The function is a keyword that is used to define functions in pascal coding, the following is the syntax. [c]function name(argument(s): type1; argument(s): type2; ...): function_type;[/c] In the above syntax, argument is used to pass the values, these arguments will provide interface between calling program and functions identifiers so it is called as formal parameters. The following is an example for the functions. SPlesson.pas [c]program SPlesson; var x, y, ret : integer; (*function definition *) function max(n1, n2: integer): integer; var (* local variable declaration *) result: integer; begin if (n1 > n2) then result := n1 else result := n2; max := result; end; begin x := 10; y := 20; (* calling a function to get max value *) ret := max(x, y); writeln( 'Max value is : ', ret ); end.[/c] In the above example, x and y are the variables and ret is another variable to store the two variables. Where max is the function to return max value from the two variables. Output:Now compile the code result will be as follows. [c]Max value is : 20[/c]

CRT Functions

shape Description

While working on pascal coding then there may be a situation to add library function o the code then uses is the keyword will be used that will call procedures, system functions. The following are the various CRT functions.

Clrscr;

The Clrscr is used to clear the screen. The following is an example. [c]Writeln('When you press enter, the screen would be cleared!'); Readln; ClrScr;[/c]

GotoXy(_,_);

The GotoXy(_,_) function will move the cursor point to the predefined position. The following is an example. [c]GotoXY(100,100); Writeln('The position is 10 pixels from the left of the screen, and ten pixels'); Writeln('from the top of the screen.'); Readln;[/c]

Textbackground();

The Textbackground() is used to set the background color. The following is an example. [c]Textbackground(red); {word - red} Writeln('Note the difference'); Textbackground(5); {integer - 5} ClrScr; Writeln('Note the difference'); Readln;[/c]

Readkey;

The Readkey function is used to read the key. The following is an example. [c]Writeln('Press ANY key'); Keypress := Readkey; {keypress is a DECLARED string variable(can be an integer variable)} Writeln(Keypress);[/c]

Textcolor();

The Textcolor() is used to give the color for the test. The following is an example. [c]Textcolor(red); {word - red} Writeln('Text colour'); Textcolor(5); {integer - 5} Writeln('Text colour'); Readln;[/c]

Delay();

The Delay() is used to wait for some time. The following is an example. [c]Writeln('1'); Delay(1000); {1000 milliseconds} Writeln('2'); Delay(1000); Writeln('3'); Readln;[/c]

Halt; / Halt()

The Halt() is used to destroy the code. The following is an example. [c]Writeln('Press enter and the program terminates!); Readln; Halt(0); [/c]

Summary

shape Key Points

  • The uses is the keyword to include library functions.
  • The procedures do not return any value.
  • The function sub procedure will return the values.