passing values betw procedures help 
Author Message
 passing values betw procedures help

Can someone please look at this program I did and tell me if this is the
way to pass a local variable between procedures.  Any help appreciated.
Please email or post to this board.  Thanks.  Mike

Program valid;
procedure getID;
var
   ID:integer;
   begin
       writeln('Enter your ID number ');
       readln(ID);
   end;
procedure validation(ID:integer);
   begin
       if (ID>99) AND (ID <800) then
          writeln('valid') else
              writeln('invalid');
   end;
begin
   getID;
   validation;
   readln;
end.

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Wed, 18 Jun 1902 08:00:00 GMT  
 passing values betw procedures help
Hi,

Quote:

> Can someone please look at this program I did and tell me if this is the
> way to pass a local variable between procedures.

This should be done in the following way:

Quote:
> function getID : integer;
> var
>    ID:integer;
>    begin
>        write('Enter your ID number ');
>        readln(ID);

         getID := ID;
Quote:
>    end;
> procedure validation(ID:integer);
>    begin
>        if (ID>99) AND (ID <800) then
>           writeln('valid') else
>               writeln('invalid');
>    end;

  var
     ID : integer;

Quote:
> begin
>    ID := getID;
>    validation(ID);
>    readln;
> end.

For more informations on functions and parameter passing techniques,
see the documentation, a good book or one of the tutorials that are
listed in the FAQ.

 - Sebastian



Wed, 18 Jun 1902 08:00:00 GMT  
 passing values betw procedures help
Try this,

Program valid (input, output);    { tells compiler there will be input and
output }

VAR  id_num : integer;               { allocates mem address }

Procedure getID (VAR ID : integer);     { makes formal parameter a
variable }

Begin { valid }
    writeln(' Enter your ID ');
    readln(ID);                            { sends value to mem address }
  End; { valid }

Procedure validation (ID : integer);      { value parameter }

  Begin { validation }
    IF (ID > 99) AND (ID < 800) THEN
      writeln('valid')
    ELSE
      writeln('invalid');
  End; { validation }

BEGIN { main }

  getID(id_num);       { id_num is the actual parameter sent to the
procedure }
  validation(id_num);        { the procedure returns the value of ID back to
the address of id_num
                                          validation uses the new value of
id_sum }

END. {main}


Quote:
> Can someone please look at this program I did and tell me if this is the
> way to pass a local variable between procedures.  Any help appreciated.
> Please email or post to this board.  Thanks.  Mike

> Program valid;
> procedure getID;
> var
>    ID:integer;
>    begin
>        writeln('Enter your ID number ');
>        readln(ID);
>    end;
> procedure validation(ID:integer);
>    begin
>        if (ID>99) AND (ID <800) then
>           writeln('valid') else
>               writeln('invalid');
>    end;
> begin
>    getID;
>    validation;
>    readln;
> end.

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Wed, 18 Jun 1902 08:00:00 GMT  
 passing values betw procedures help

Quote:

>Can someone please look at this program I did and tell me if this is the
>way to pass a local variable between procedures.  Any help appreciated.
>Please email or post to this board.  Thanks.  Mike

>Program valid;
>procedure getID;
>var
>   ID:integer;
>   begin
>       writeln('Enter your ID number ');
>       readln(ID);
>   end;
>procedure validation(ID:integer);
>   begin
>       if (ID>99) AND (ID <800) then
>          writeln('valid') else
>              writeln('invalid');
>   end;
>begin
>   getID;
>   validation;
>   readln;
>end.

Mike,

The general structure of your code is OK but it
needs some work on the parameters.

Pascal knows two types of parameters:
- Value parameters
- Variable parameters

Example with a value parameter:
var UserName: string;
Procedure Display(name: string);
begin
    writeln ('Your name is', name);
    name := '';
end;
Begin
    UserName := 'Mike';
    Display(UserName);
end.
Note these points:
- When calling the Display procedure, the list
  of parameters must agree in type and number
  with the list of parameters in the Procedure
  declaration. Your sample program fails to do this.
- Any change made to a value parameter in the
  Display procedure is NOT reflected back to the
  calling routine. The line "name := ''" has therefore
  no effect on the variable UserName.

Example with a variable parameter:
var UserName: string;
Procedure Display(var name: string);
begin
    writeln ('Your name is', name);
    name := '';
end;
Begin
    UserName := 'Mike';
    Display(UserName);
end.
Note these points:
- Again the list of parameters in the procedure
  call must agree in type and number with the
  list of parameters in the Procedure declaration.
- Any change made to a value parameter in a
  procedure IS reflected back to the calling
  routine. The line "name := ''" will cause the
  variable UserName in the main program to be
  reset to an empty string.

I trust this helps.

KlausL



Wed, 18 Jun 1902 08:00:00 GMT  
 passing values betw procedures help

Quote:

>Can someone please look at this program I did and tell me if this is the
>way to pass a local variable between procedures.  Any help appreciated.
>Please email or post to this board.  Thanks.  Mike

>Program valid;
>procedure getID;
>var
>   ID:integer;
>   begin
>       writeln('Enter your ID number ');
>       readln(ID);
>   end;
>procedure validation(ID:integer);
>   begin
>       if (ID>99) AND (ID <800) then
>          writeln('valid') else
>              writeln('invalid');
>   end;
>begin
>   getID;
>   validation;
>   readln;
>end.

There is no rule that says you cannot use blank lines.

Osmo



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Passing values to Assembly procedures in Turbo Pascal

2. Pass by reference. Pass by value

3. passing procedures as arguments for another procedure in Turbo pascal

4. passing procedures as arguments for another procedure in Turbo pascal

5. passing procedures as arguments for another procedure in Tu

6. Passing a pointer to a procedure/function as a parameter in a procedure

7. problem passing array values.

8. Passing values to other units

9. Passing values between forms

10. Re : passing values between two forms

11. How to pass parameter to TQuery and retrieve value from TQuery

12. units & passing pointer values

 

 
Powered by phpBB® Forum Software