Problem with procedural var - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Problem with procedural var

king0deuking0deu Posts: 29Member
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:

[code]
type proc=procedure;

procedure main;
var p:proc;

procedure one;
begin
{something}
end;

begin
p:=one;
end;

begin
p;
end.
[/code]

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

Comments

  • zibadianzibadian Posts: 6,349Member
    : 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:
    :
    : [code]
    : type proc=procedure;
    :
    : procedure main;
    : var p:proc;
    :
    : procedure one;
    : begin
    : {something}
    : end;
    :
    : begin
    : p:=one;
    : end;
    :
    : begin
    : p;
    : end.
    : [/code]
    :
    : 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:
    [code]
    var
    p: procedure;

    procedure one;
    begin
    {something}
    end;

    begin
    P := one;
    P;
    end.
    [/code]
    If the assignment doesn't work, then use this instead:
    [code]
    P := @one;
    [/code]
  • AlcatizAlcatiz Posts: 51Member
    : 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:
    :
    : [code]
    : type proc=procedure;
    :
    : procedure main;
    : var p:proc;
    :
    : procedure one;
    : begin
    : {something}
    : end;
    :
    : begin
    : p:=one;
    : end;
    :
    : begin
    : p;
    : end.
    : [/code]
    :
    : 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 :
    [code]@p:=@one;[/code]
  • king0deuking0deu Posts: 29Member
    : : 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:
    : :
    : : [code]
    : : type proc=procedure;
    : :
    : : procedure main;
    : : var p:proc;
    : :
    : : procedure one;
    : : begin
    : : {something}
    : : end;
    : :
    : : begin
    : : p:=one;
    : : end;
    : :
    : : begin
    : : p;
    : : end.
    : : [/code]
    : :
    : : 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 :
    : [code]@p:=@one;[/code]
    :

    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:

    [code]
    : : var y:integer;

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

    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?
  • zibadianzibadian Posts: 6,349Member
    : : : 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:
    : : :
    : : : [code]
    : : : type proc=procedure;
    : : :
    : : : procedure main;
    : : : var p:proc;
    : : :
    : : : procedure one;
    : : : begin
    : : : {something}
    : : : end;
    : : :
    : : : begin
    : : : p:=one;
    : : : end;
    : : :
    : : : begin
    : : : p;
    : : : end.
    : : : [/code]
    : : :
    : : : 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 :
    : : [code]@p:=@one;[/code]
    : :
    :
    : 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:
    :
    : [code]
    : : : var y:integer;
    :
    : : procedure main([b]var x:integer[/b]);
    : : : var p:procedure;
    : : :
    : : : procedure one;
    : : : begin
    : : : {something}
    : : : [b]x:=123; {change the value of x which is a parameter}[/b]
    : : : end;
    : : :
    : : : begin
    : : : x:=0;
    : : : @p:=@one; {assign to variable}
    : : : [b]p; {call the procedure one, to change x to 123}[/b]
    : : : end;
    : : :
    : : : begin
    : : : y:=1;
    : : : main(y);
    : : : end.
    : [/code]
    :
    : 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:
    [code]
    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.
    [/code]
    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.
  • king0deuking0deu Posts: 29Member
    [b][red]This message was edited by king0deu at 2007-1-1 9:47:11[/red][/b][hr]
    : : : : 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:
    : : : :
    : : : : [code]
    : : : : type proc=procedure;
    : : : :
    : : : : procedure main;
    : : : : var p:proc;
    : : : :
    : : : : procedure one;
    : : : : begin
    : : : : {something}
    : : : : end;
    : : : :
    : : : : begin
    : : : : p:=one;
    : : : : end;
    : : : :
    : : : : begin
    : : : : p;
    : : : : end.
    : : : : [/code]
    : : : :
    : : : : 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 :
    : : : [code]@p:=@one;[/code]
    : : :
    : :
    : : 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:
    : :
    : : [code]
    : : : : var y:integer;
    : :
    : : : procedure main([b]var x:integer[/b]);
    : : : : var p:procedure;
    : : : :
    : : : : procedure one;
    : : : : begin
    : : : : {something}
    : : : : [b]x:=123; {change the value of x which is a parameter}[/b]
    : : : : end;
    : : : :
    : : : : begin
    : : : : x:=0;
    : : : : @p:=@one; {assign to variable}
    : : : : [b]p; {call the procedure one, to change x to 123}[/b]
    : : : : end;
    : : : :
    : : : : begin
    : : : : y:=1;
    : : : : main(y);
    : : : : end.
    : : [/code]
    : :
    : : 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:
    : [code]
    : 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.
    : [/code]
    : 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


  • zibadianzibadian Posts: 6,349Member
    : [b][red]This message was edited by king0deu at 2007-1-1 9:47:11[/red][/b][hr]
    : : : : : 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:
    : : : : :
    : : : : : [code]
    : : : : : type proc=procedure;
    : : : : :
    : : : : : procedure main;
    : : : : : var p:proc;
    : : : : :
    : : : : : procedure one;
    : : : : : begin
    : : : : : {something}
    : : : : : end;
    : : : : :
    : : : : : begin
    : : : : : p:=one;
    : : : : : end;
    : : : : :
    : : : : : begin
    : : : : : p;
    : : : : : end.
    : : : : : [/code]
    : : : : :
    : : : : : 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 :
    : : : : [code]@p:=@one;[/code]
    : : : :
    : : :
    : : : 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:
    : : :
    : : : [code]
    : : : : : var y:integer;
    : : :
    : : : : procedure main([b]var x:integer[/b]);
    : : : : : var p:procedure;
    : : : : :
    : : : : : procedure one;
    : : : : : begin
    : : : : : {something}
    : : : : : [b]x:=123; {change the value of x which is a parameter}[/b]
    : : : : : end;
    : : : : :
    : : : : : begin
    : : : : : x:=0;
    : : : : : @p:=@one; {assign to variable}
    : : : : : [b]p; {call the procedure one, to change x to 123}[/b]
    : : : : : end;
    : : : : :
    : : : : : begin
    : : : : : y:=1;
    : : : : : main(y);
    : : : : : end.
    : : : [/code]
    : : :
    : : : 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:
    : : [code]
    : : 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.
    : : [/code]
    : : 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:
    [code]
    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.
    [/code]
    [code]
    program PVProg;

    uses
    ProcVar;

    var
    y: integer;
    begin
    y:=0;
    main(y);
    writeln(y);
    readln;
    end.
    [/code]
  • king0deuking0deu Posts: 29Member
    : : [b][red]This message was edited by king0deu at 2007-1-1 9:47:11[/red][/b][hr]
    : : : : : : 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:
    : : : : : :
    : : : : : : [code]
    : : : : : : type proc=procedure;
    : : : : : :
    : : : : : : procedure main;
    : : : : : : var p:proc;
    : : : : : :
    : : : : : : procedure one;
    : : : : : : begin
    : : : : : : {something}
    : : : : : : end;
    : : : : : :
    : : : : : : begin
    : : : : : : p:=one;
    : : : : : : end;
    : : : : : :
    : : : : : : begin
    : : : : : : p;
    : : : : : : end.
    : : : : : : [/code]
    : : : : : :
    : : : : : : 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 :
    : : : : : [code]@p:=@one;[/code]
    : : : : :
    : : : :
    : : : : 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:
    : : : :
    : : : : [code]
    : : : : : : var y:integer;
    : : : :
    : : : : : procedure main([b]var x:integer[/b]);
    : : : : : : var p:procedure;
    : : : : : :
    : : : : : : procedure one;
    : : : : : : begin
    : : : : : : {something}
    : : : : : : [b]x:=123; {change the value of x which is a parameter}[/b]
    : : : : : : end;
    : : : : : :
    : : : : : : begin
    : : : : : : x:=0;
    : : : : : : @p:=@one; {assign to variable}
    : : : : : : [b]p; {call the procedure one, to change x to 123}[/b]
    : : : : : : end;
    : : : : : :
    : : : : : : begin
    : : : : : : y:=1;
    : : : : : : main(y);
    : : : : : : end.
    : : : : [/code]
    : : : :
    : : : : 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:
    : : : [code]
    : : : 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.
    : : : [/code]
    : : : 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:
    : [code]
    : 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.
    : [/code]
    : [code]
    : program PVProg;
    :
    : uses
    : ProcVar;
    :
    : var
    : y: integer;
    : begin
    : y:=0;
    : main(y);
    : writeln(y);
    : readln;
    : end.
    : [/code]
    :

    I see.. thanks alot

Sign In or Register to comment.