Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4095
Number of posts: 14004

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Problem with procedural var Posted by king0deu on 26 Dec 2006 at 11:19 AM
My problem is I can't assign a procedure to a variable within a procedure

I kknow this may be confused so look at this:

type proc=procedure;

procedure main;
var p:proc;

procedure one;
begin
{something}
end;

begin
p:=one;
end;

begin
p;
end.


when you run that you'll encounter the error: invalid proceudre reference..
So is there a solution to use procedural vars within another procedure?
I need this be cause I want to put my whole program in a unit

help me pls

Report
Re: Problem with procedural var Posted by zibadian on 26 Dec 2006 at 2:21 PM
: My problem is I can't assign a procedure to a variable within a procedure
:
: I kknow this may be confused so look at this:
:
:
: type proc=procedure;
: 
: procedure main;
: var p:proc;
: 
: procedure one;
: begin
: {something}
: end;
: 
: begin
: p:=one;
: end;
: 
: begin
: p;
: end.
: 

:
: when you run that you'll encounter the error: invalid proceudre reference..
: So is there a solution to use procedural vars within another procedure?
: I need this be cause I want to put my whole program in a unit
:
: help me pls
:
:
In Pascal procedures to be used in procedural variables must be far. In other words you may not define them as "local"procedures:
var 
  p: procedure;
 
procedure one;
begin
  {something}
end;
 
begin
  P := one;
  P;
end.

If the assignment doesn't work, then use this instead:
  P := @one;

Report
Re: Problem with procedural var Posted by Alcatiz on 26 Dec 2006 at 2:27 PM
: My problem is I can't assign a procedure to a variable within a procedure
:
: I kknow this may be confused so look at this:
:
:
: type proc=procedure;
: 
: procedure main;
: var p:proc;
: 
: procedure one;
: begin
: {something}
: end;
: 
: begin
: p:=one;
: end;
: 
: begin
: p;
: end.
: 

:
: when you run that you'll encounter the error: invalid proceudre reference..
: So is there a solution to use procedural vars within another procedure?
: I need this be cause I want to put my whole program in a unit
:
: help me pls
:
:
Hi !
You must write :
@p:=@one;

Report
Re: Problem with procedural var Posted by king0deu on 1 Jan 2007 at 9:01 AM
: : My problem is I can't assign a procedure to a variable within a procedure
: :
: : I kknow this may be confused so look at this:
: :
: :
: : type proc=procedure;
: : 
: : procedure main;
: : var p:proc;
: : 
: : procedure one;
: : begin
: : {something}
: : end;
: : 
: : begin
: : p:=one;
: : end;
: : 
: : begin
: : p;
: : end.
: : 

: :
: : when you run that you'll encounter the error: invalid proceudre reference..
: : So is there a solution to use procedural vars within another procedure?
: : I need this be cause I want to put my whole program in a unit
: :
: : help me pls
: :
: :
: Hi !
: You must write :
:
@p:=@one;

:

Thanks, man. It seemed to work, but there's another problem:
if I pass a parameter to the main procedure, the procedure one can't access it, like this:

: : var y:integer;

: procedure main(var x:integer);
: : var p:procedure;
: : 
: : procedure one;
: : begin
: : {something}
: : x:=123; {change the value of x which is a parameter}
: : end;
: : 
: : begin
: : x:=0;
: : @p:=@one; {assign to variable}
: : p; {call the procedure one, to change x to 123}
: : end;
: : 
: : begin
: : y:=1;
: : main(y);
: : end.


but the result is that y, after the call main(y), is still 1, it is not changed.
the procedure one is surely called (via the call of p), but it can't access x, I think so

any idea?
Report
Re: Problem with procedural var Posted by zibadian on 1 Jan 2007 at 9:29 AM
: : : My problem is I can't assign a procedure to a variable within a procedure
: : :
: : : I kknow this may be confused so look at this:
: : :
: : :
: : : type proc=procedure;
: : : 
: : : procedure main;
: : : var p:proc;
: : : 
: : : procedure one;
: : : begin
: : : {something}
: : : end;
: : : 
: : : begin
: : : p:=one;
: : : end;
: : : 
: : : begin
: : : p;
: : : end.
: : : 

: : :
: : : when you run that you'll encounter the error: invalid proceudre reference..
: : : So is there a solution to use procedural vars within another procedure?
: : : I need this be cause I want to put my whole program in a unit
: : :
: : : help me pls
: : :
: : :
: : Hi !
: : You must write :
: :
@p:=@one;

: :
:
: Thanks, man. It seemed to work, but there's another problem:
: if I pass a parameter to the main procedure, the procedure one can't access it, like this:
:
:
: : : var y:integer;
: 
: : procedure main(var x:integer);
: : : var p:procedure;
: : : 
: : : procedure one;
: : : begin
: : : {something}
: : : x:=123; {change the value of x which is a parameter}
: : : end;
: : : 
: : : begin
: : : x:=0;
: : : @p:=@one; {assign to variable}
: : : p; {call the procedure one, to change x to 123}
: : : end;
: : : 
: : : begin
: : : y:=1;
: : : main(y);
: : : end.
: 

:
: but the result is that y, after the call main(y), is still 1, it is not changed.
: the procedure one is surely called (via the call of p), but it can't access x, I think so
:
: any idea?
:
Appearantly the x variable in One() is stored in the stack of One() instead of Main(), thus it remains unchanged. As for the value of y, it has been changed: to 0.
Here is a working example:
type
  TIntChanger = procedure(var Value: integer);

procedure One(var Value: integer); far;
begin
  Value := 1;
end;

procedure Two(var Value: integer); far;
begin
  Value := 2;
end;

procedure main(var x: integer);
var
  p: TIntChanger;
begin
  @p := @One; {assign to variable}
  p(x); {call the procedure one, to change x to 123}
end;

var
  y: integer;
begin
  y:=0;
  main(y);
  writeln(y);
  readln;
end.

As you can see I've taken One() procedure out of the Main() procedure (as previously suggested), and added it's own procedural type definition, since that is necessary in this case to make the change. To make sure the code worked, I've tested it also with the Two() procedure.
Report
Re: Problem with procedural var Posted by king0deu on 1 Jan 2007 at 9:43 AM
This message was edited by king0deu at 2007-1-1 9:47:11

: : : : My problem is I can't assign a procedure to a variable within a procedure
: : : :
: : : : I kknow this may be confused so look at this:
: : : :
: : : :
: : : : type proc=procedure;
: : : : 
: : : : procedure main;
: : : : var p:proc;
: : : : 
: : : : procedure one;
: : : : begin
: : : : {something}
: : : : end;
: : : : 
: : : : begin
: : : : p:=one;
: : : : end;
: : : : 
: : : : begin
: : : : p;
: : : : end.
: : : : 

: : : :
: : : : when you run that you'll encounter the error: invalid proceudre reference..
: : : : So is there a solution to use procedural vars within another procedure?
: : : : I need this be cause I want to put my whole program in a unit
: : : :
: : : : help me pls
: : : :
: : : :
: : : Hi !
: : : You must write :
: : :
@p:=@one;

: : :
: :
: : Thanks, man. It seemed to work, but there's another problem:
: : if I pass a parameter to the main procedure, the procedure one can't access it, like this:
: :
: :
: : : : var y:integer;
: : 
: : : procedure main(var x:integer);
: : : : var p:procedure;
: : : : 
: : : : procedure one;
: : : : begin
: : : : {something}
: : : : x:=123; {change the value of x which is a parameter}
: : : : end;
: : : : 
: : : : begin
: : : : x:=0;
: : : : @p:=@one; {assign to variable}
: : : : p; {call the procedure one, to change x to 123}
: : : : end;
: : : : 
: : : : begin
: : : : y:=1;
: : : : main(y);
: : : : end.
: : 

: :
: : but the result is that y, after the call main(y), is still 1, it is not changed.
: : the procedure one is surely called (via the call of p), but it can't access x, I think so
: :
: : any idea?
: :
: Appearantly the x variable in One() is stored in the stack of One() instead of Main(), thus it remains unchanged. As for the value of y, it has been changed: to 0.
: Here is a working example:
:
: type
:   TIntChanger = procedure(var Value: integer);
: 
: procedure One(var Value: integer); far;
: begin
:   Value := 1;
: end;
: 
: procedure Two(var Value: integer); far;
: begin
:   Value := 2;
: end;
: 
: procedure main(var x: integer);
: var
:   p: TIntChanger;
: begin
:   @p := @One; {assign to variable}
:   p(x); {call the procedure one, to change x to 123}
: end;
: 
: var
:   y: integer;
: begin
:   y:=0;
:   main(y);
:   writeln(y);
:   readln;
: end.
: 

: As you can see I've taken One() procedure out of the Main() procedure (as previously suggested), and added it's own procedural type definition, since that is necessary in this case to make the change. To make sure the code worked, I've tested it also with the Two() procedure.
:

yes, but, to make it handy, I want to put this pile of code in to a unit, that means I have to put the whole code in a procedure, that's my main problem.

In addition, if I replace the call p() in the code with one(), ie. call the procedure directly, it worked, it can change the value of x parameter (to 123). But I dont know why calling p does not work


Report
Re: Problem with procedural var Posted by zibadian on 1 Jan 2007 at 10:18 AM
: This message was edited by king0deu at 2007-1-1 9:47:11

: : : : : My problem is I can't assign a procedure to a variable within a procedure
: : : : :
: : : : : I kknow this may be confused so look at this:
: : : : :
: : : : :
: : : : : type proc=procedure;
: : : : : 
: : : : : procedure main;
: : : : : var p:proc;
: : : : : 
: : : : : procedure one;
: : : : : begin
: : : : : {something}
: : : : : end;
: : : : : 
: : : : : begin
: : : : : p:=one;
: : : : : end;
: : : : : 
: : : : : begin
: : : : : p;
: : : : : end.
: : : : : 

: : : : :
: : : : : when you run that you'll encounter the error: invalid proceudre reference..
: : : : : So is there a solution to use procedural vars within another procedure?
: : : : : I need this be cause I want to put my whole program in a unit
: : : : :
: : : : : help me pls
: : : : :
: : : : :
: : : : Hi !
: : : : You must write :
: : : :
@p:=@one;

: : : :
: : :
: : : Thanks, man. It seemed to work, but there's another problem:
: : : if I pass a parameter to the main procedure, the procedure one can't access it, like this:
: : :
: : :
: : : : : var y:integer;
: : : 
: : : : procedure main(var x:integer);
: : : : : var p:procedure;
: : : : : 
: : : : : procedure one;
: : : : : begin
: : : : : {something}
: : : : : x:=123; {change the value of x which is a parameter}
: : : : : end;
: : : : : 
: : : : : begin
: : : : : x:=0;
: : : : : @p:=@one; {assign to variable}
: : : : : p; {call the procedure one, to change x to 123}
: : : : : end;
: : : : : 
: : : : : begin
: : : : : y:=1;
: : : : : main(y);
: : : : : end.
: : : 

: : :
: : : but the result is that y, after the call main(y), is still 1, it is not changed.
: : : the procedure one is surely called (via the call of p), but it can't access x, I think so
: : :
: : : any idea?
: : :
: : Appearantly the x variable in One() is stored in the stack of One() instead of Main(), thus it remains unchanged. As for the value of y, it has been changed: to 0.
: : Here is a working example:
: :
: : type
: :   TIntChanger = procedure(var Value: integer);
: : 
: : procedure One(var Value: integer); far;
: : begin
: :   Value := 1;
: : end;
: : 
: : procedure Two(var Value: integer); far;
: : begin
: :   Value := 2;
: : end;
: : 
: : procedure main(var x: integer);
: : var
: :   p: TIntChanger;
: : begin
: :   @p := @One; {assign to variable}
: :   p(x); {call the procedure one, to change x to 123}
: : end;
: : 
: : var
: :   y: integer;
: : begin
: :   y:=0;
: :   main(y);
: :   writeln(y);
: :   readln;
: : end.
: : 

: : As you can see I've taken One() procedure out of the Main() procedure (as previously suggested), and added it's own procedural type definition, since that is necessary in this case to make the change. To make sure the code worked, I've tested it also with the Two() procedure.
: :
:
: yes, but, to make it handy, I want to put this pile of code in to a unit, that means I have to put the whole code in a procedure, that's my main problem.
:
: In addition, if I replace the call p() in the code with one(), ie. call the procedure directly, it worked, it can change the value of x parameter (to 123). But I dont know why calling p does not work
:
:
:
I just explained a probable cause, why calling P() doesn't work. Units can have more than one procedure/function/type definitions/variable declarations/etc.; in fact nearly all of them have. Here's the same program but then with a unit:
unit ProcVar;

interface

procedure main(var x: integer);

implementation

type
  TIntChanger = procedure(var Value: integer);

procedure One(var Value: integer); far;
begin
  Value := 1;
end;

procedure Two(var Value: integer); far;
begin
  Value := 2;
end;

procedure main(var x: integer);
var
  p: TIntChanger;
begin
  @p := @One; {assign to variable}
  p(x); {call the procedure one, to change x to 123}
end;

end.

program PVProg;

uses
  ProcVar;

var
  y: integer;
begin
  y:=0;
  main(y);
  writeln(y);
  readln;
end.

Report
Re: Problem with procedural var Posted by king0deu on 1 Jan 2007 at 9:31 PM
: : This message was edited by king0deu at 2007-1-1 9:47:11

: : : : : : My problem is I can't assign a procedure to a variable within a procedure
: : : : : :
: : : : : : I kknow this may be confused so look at this:
: : : : : :
: : : : : :
: : : : : : type proc=procedure;
: : : : : : 
: : : : : : procedure main;
: : : : : : var p:proc;
: : : : : : 
: : : : : : procedure one;
: : : : : : begin
: : : : : : {something}
: : : : : : end;
: : : : : : 
: : : : : : begin
: : : : : : p:=one;
: : : : : : end;
: : : : : : 
: : : : : : begin
: : : : : : p;
: : : : : : end.
: : : : : : 

: : : : : :
: : : : : : when you run that you'll encounter the error: invalid proceudre reference..
: : : : : : So is there a solution to use procedural vars within another procedure?
: : : : : : I need this be cause I want to put my whole program in a unit
: : : : : :
: : : : : : help me pls
: : : : : :
: : : : : :
: : : : : Hi !
: : : : : You must write :
: : : : :
@p:=@one;

: : : : :
: : : :
: : : : Thanks, man. It seemed to work, but there's another problem:
: : : : if I pass a parameter to the main procedure, the procedure one can't access it, like this:
: : : :
: : : :
: : : : : : var y:integer;
: : : : 
: : : : : procedure main(var x:integer);
: : : : : : var p:procedure;
: : : : : : 
: : : : : : procedure one;
: : : : : : begin
: : : : : : {something}
: : : : : : x:=123; {change the value of x which is a parameter}
: : : : : : end;
: : : : : : 
: : : : : : begin
: : : : : : x:=0;
: : : : : : @p:=@one; {assign to variable}
: : : : : : p; {call the procedure one, to change x to 123}
: : : : : : end;
: : : : : : 
: : : : : : begin
: : : : : : y:=1;
: : : : : : main(y);
: : : : : : end.
: : : : 

: : : :
: : : : but the result is that y, after the call main(y), is still 1, it is not changed.
: : : : the procedure one is surely called (via the call of p), but it can't access x, I think so
: : : :
: : : : any idea?
: : : :
: : : Appearantly the x variable in One() is stored in the stack of One() instead of Main(), thus it remains unchanged. As for the value of y, it has been changed: to 0.
: : : Here is a working example:
: : :
: : : type
: : :   TIntChanger = procedure(var Value: integer);
: : : 
: : : procedure One(var Value: integer); far;
: : : begin
: : :   Value := 1;
: : : end;
: : : 
: : : procedure Two(var Value: integer); far;
: : : begin
: : :   Value := 2;
: : : end;
: : : 
: : : procedure main(var x: integer);
: : : var
: : :   p: TIntChanger;
: : : begin
: : :   @p := @One; {assign to variable}
: : :   p(x); {call the procedure one, to change x to 123}
: : : end;
: : : 
: : : var
: : :   y: integer;
: : : begin
: : :   y:=0;
: : :   main(y);
: : :   writeln(y);
: : :   readln;
: : : end.
: : : 

: : : As you can see I've taken One() procedure out of the Main() procedure (as previously suggested), and added it's own procedural type definition, since that is necessary in this case to make the change. To make sure the code worked, I've tested it also with the Two() procedure.
: : :
: :
: : yes, but, to make it handy, I want to put this pile of code in to a unit, that means I have to put the whole code in a procedure, that's my main problem.
: :
: : In addition, if I replace the call p() in the code with one(), ie. call the procedure directly, it worked, it can change the value of x parameter (to 123). But I dont know why calling p does not work
: :
: :
: :
: I just explained a probable cause, why calling P() doesn't work. Units can have more than one procedure/function/type definitions/variable declarations/etc.; in fact nearly all of them have. Here's the same program but then with a unit:
:
: unit ProcVar;
: 
: interface
: 
: procedure main(var x: integer);
: 
: implementation
: 
: type
:   TIntChanger = procedure(var Value: integer);
: 
: procedure One(var Value: integer); far;
: begin
:   Value := 1;
: end;
: 
: procedure Two(var Value: integer); far;
: begin
:   Value := 2;
: end;
: 
: procedure main(var x: integer);
: var
:   p: TIntChanger;
: begin
:   @p := @One; {assign to variable}
:   p(x); {call the procedure one, to change x to 123}
: end;
: 
: end.
: 

:
: program PVProg;
: 
: uses
:   ProcVar;
: 
: var
:   y: integer;
: begin
:   y:=0;
:   main(y);
:   writeln(y);
:   readln;
: end.
: 

:

I see.. thanks alot




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.