function procedure c6 c7

22
Pascal Programming Language Omar ElSabek & Fayez G hazzawi IT Engineering 3 th year UNKNOWN Department

Upload: omar-al-sabek

Post on 14-Apr-2017

301 views

Category:

Education


0 download

TRANSCRIPT

Pascal Programming Language

Omar ElSabek & Fayez GhazzawiIT Engineering3th year – UNKNOWN Department

•Quiet!!!!!!!!!!

• Pay the fine! :P :D

•Being late!

•Talking!

• Procedures and Functions

• The most important material

• which are very important and you should keep on practicing more often.

• Pascal provides two kinds of subprograms:

• Functions : these subprograms return a single value.

• Procedures : these subprograms do not return a value directly.

• Procedures are just like small programs.

• Sometimes they are called sub--programs. They help the programmer to avoid repetitions.

• A procedure start off with a begin and ends up with an end;.

• It can also have its own variables, which cannot be used with the main-program.

Procedure Procedure name( Var Variable Name : Type );

Program Lesson1_Program1 (input,output);

Var

Procedure Square(X : Integer; Var y : Integer);

var

Begin

…………

End;

Begin

………….

End.

New knowledge about Variables !

There is a Two type of Variables :

Global Variables :

That which we define in Program Var

Local Variable :That Which we define inside of our procedure/function or

in procedure/function signature

What is the signature !!!

What is the signature !!!

Procedure Procedure name( Var Variable Name : Type );Var Variable Name : Type

What is the prototype {define} !!!

What is the prototype !!!

Procedure Procedure name( Var Variable Name : Type );

Program Lesson1_Program1 (input,output);

Var

Z :integer ;

Procedure Square(X : Integer; Var y : Integer);

var

moxo :real ;

Begin

…………

End;

Begin

………….

End.

Program Lesson1_Program1 (input,output);

Var

Z :integer ;

Procedure Square(X : Integer ; Var y : Integer);

var

moxo :real ;

Begin

…………

End;

Begin

………….

End.

When you declare parameters to a procedure, variables must separated

by a semi-colon ";".They are put in between the brackets, following the

procedure name.

Program Lesson1_Program1 (input,output);

Var

X , Z : integer ;

Procedure Square(R : Integer ; Var Result : Integer);

Begin

Result := R * R ;

End;

Begin

Readln(X);

Square(X,Z);

Writeln('The square of X is: '); Writeln( Z );

End.

Program Lesson1_Program1 (input,output);

Var

X , Z : integer ;

Procedure Square(R : Integer);

Var

Result : Integer;

Begin

Result := R * R ;

writeln ('The square of X is: ' , Result);

End;

Begin

Readln(X);

Square(X);

End.

• Let’s Talk a little bit a bout our built in function !!

• ABS( -5 ) 5

• Chr( 97 ) a

• Ord( ‘ a ’ ) 97

• Sin(x) Cos(x)

• Pred( 6 ) 5

• Sqr( 3 ) 9

• Sqrt( 9 ) 3

• Odd( 3 ) True

• Even( 3 ) False

• Succ ( 5 ) 6

• Round( 3.14 ) 3

• Round( 3.64 ) 4

• Function are The second type of sub-program .

• The only difference from the procedure is that the Function return a value at the end.

• A Function start off with a begin and ends up with an end;.

• It can also have its own variables, which cannot be used with the main-program.

Function FunctionName(Var VariableName:Type): Return Type;

Program Lesson1_Program1 (input,output);

Var

Function Square(X : Integer): Integer ;

var

Begin

…………

End;

Begin

………….

End.

Program Lesson1_Program1 (input,output);

Var

X , Z : integer ;

Function Square(R : Integer) : Integer;

Begin

Square := R * R ;

End;

Begin

Readln(X);

Z:= Square(X);

Writeln('The square of X is: '); Writeln( Z );

End.