954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Difference between two procedure

By Endre Bende on Mar 11th, 2009 12:17 am

The arguments are passed by value and by reference

{Difference between two procedure}
Program Difference;
Uses Crt;
{we have two variables,this will A and B}
Var A,B:Byte;

{By value }
Procedure First(F1,F2:Byte);
   Begin
        F1:=F1+2;
        F2:=F2+2;
   End;{first}

{By reference}   {*}
Procedure Second(Var S1,S2:Byte);
   Begin
        S1:=S1+2;
        S2:=S2+2;
   End;{second}

Begin {main}
     A:=1;
     B:=1;
     WriteLn('A:=',A);
     WriteLn('B:=',B);
     First(A,B);
     WriteLn('After the -First- procedure,the result is: ',A+B);
     {now,call the 'Second'}
     Second(A,B);
     WriteLn('And after the -Second- procedure the result is: ',A+B);
     ReadKey;
End. {main}
{Note that the 'First' procedure won't change the value of 'A' and 'B'}
{they only changed inside the First procedure,and effectless for the}
{global variables of the main program}


{Created By FlamingClaw 2009.03.10}

you must use var if you wanth procedure tasks efect to main program
in 1st you dont use var in procedure and thats why no efect to main

est69dog
Newbie Poster
5 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 
...
{By reference}   {*}
Procedure Second(Var S1,S2:Byte);
   Begin
        S1:=S1+2;
        S2:=S2+2;
   End;{second}
...
...


This is the second procedure in my example.Its arguments accepts only variables,cause the second procedure make changes on these variables' memory addresses.

FlamingClaw
Posting Pro
559 posts since Feb 2009
Reputation Points: 132
Solved Threads: 138
 

Those are technically called passed by value and passed by reference, corresponding to your program.
With "passed by value", your passed variables remain the same no matter how you change it in your local scope (functions, procedures, block code). In this case, it's F1 and F2 .
With "passed by reference", every change you made to the local variables would be applied to your passed variables. In this case, it's S1 and S2 .

There's one more thing you should remember about this is that if you pass by value, you can call functions and procedures with constants (such as 5 for integers, 'abc' for strings, ...) but if you pass by reference, you have to provide the variable(s) when calling your functions/procedures.

CMPITG
Newbie Poster
4 posts since Dec 2009
Reputation Points: 10
Solved Threads: 1
 

thx

est69dog
Newbie Poster
5 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

thanks

mahmoud85
Newbie Poster
3 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You