Quick Pascal question pls

Discussion in 'NZ Computing' started by Warwick, Dec 22, 2003.

  1. Warwick

    Warwick Guest

    I am playing with the scripting tool in Dialog.
    So far I am crash coursing the necessary pascal ok but I have one question
    that I can't seem to readily find the answer to online.

    Functions always return values in Pascal (if there is a void return type
    its called a procedure I think lol).

    What is getting to me is what I know in C as the return statement.

    Declaration for a function that takes an int by value and returns its
    square might look like this.
    function sqaure (x :int):int;
    begin
    foo := x * x;
    end; //return statement is assignment into function name.

    however in the script examples the functions are of this form...

    function sqaure (x :int):int;
    begin
    result := x * x;
    end; //return statement is assignment into result.
    //result is not declared anywhere, seems to be a typeless global?!

    in the online manuals only the first form in mentioned.
    If I try replacing the second form with the first in a dialog script it
    throws an error.

    Can anyone explain to me whats going on here?

    TIA
    Warwick
     
    Warwick, Dec 22, 2003
    #1
    1. Advertising

  2. Warwick

    Rudy Seoa Guest

    > Declaration for a function that takes an int by value and returns its
    > square might look like this.
    > function sqaure (x :int):int;
    > begin
    > foo := x * x;
    > end; //return statement is assignment into function name.


    Pascal's integer type is 'integer' not 'int'.

    This will work:
    function square (x :integer):integer;
    begin
    square := x * x;
    end;

    > however in the script examples the functions are of this form...
    >
    > function sqaure (x :int):int;
    > begin
    > result := x * x;
    > end; //return statement is assignment into result.
    > //result is not declared anywhere, seems to be a typeless global?!


    This should work as well, if you correct the integer declarations.

    The scripting is standard Object Pascal so any Delphi programming
    resource should help you, and there's a bunch of example scripts in the
    Dialog homepage's Wiki.

    Dialog looks promising, but it's still very beta'ish (I could make it
    crash with a AV in the initial configuration wizard)
     
    Rudy Seoa, Dec 22, 2003
    #2
    1. Advertising

  3. Warwick

    Warwick Guest

    On Mon, 22 Dec 2003 22:06:01 +1300, Rudy Seoa wrote:

    >> Declaration for a function that takes an int by value and returns its
    >> square might look like this.
    >> function sqaure (x :int):int;
    >> begin
    >> foo := x * x;
    >> end; //return statement is assignment into function name.

    >
    > Pascal's integer type is 'integer' not 'int'.
    >
    > This will work:
    > function square (x :integer):integer;
    > begin
    > square := x * x;
    > end;
    >
    >> however in the script examples the functions are of this form...
    >>
    >> function sqaure (x :int):int;
    >> begin
    >> result := x * x;
    >> end; //return statement is assignment into result.
    >> //result is not declared anywhere, seems to be a typeless global?!

    >
    > This should work as well, if you correct the integer declarations.
    >
    > The scripting is standard Object Pascal so any Delphi programming
    > resource should help you, and there's a bunch of example scripts in the
    > Dialog homepage's Wiki.
    >
    > Dialog looks promising, but it's still very beta'ish (I could make it
    > crash with a AV in the initial configuration wizard)


    Hey tyvm!

    Sorry bout the int declarations, I was in C++ Mode.
    I like to think I am familiar with CBuilder so the VCL types in Dialog are
    all familiar.

    The first form (which sits more comfortably with me, the second suggests a
    loosely typed global called result - which is an odd thing to have in a
    strictly typed langauge) will not work in Dialog.

    specific example:

    FUNCTION GetClipboardData(var memo:TMemo):boolean; //Copy Clipboard data
    var b:boolean; //redundant ?
    begin // return true if data copied.
    memo.Clear();
    memo.PasteFromClipboard();
    GetClipboardData := b; //memo.Lines.Count > 0; //this is line 87
    end;

    Failed when compiling
    [Error] (87:26): open round ('(')expected
    When I saw that I worried that the lvalue in the return statement may not
    accept an expression on the lhs, which is why I declared a local variable
    first.

    again with out local boolean variable

    FUNCTION GetClipboardData(var memo:TMemo):boolean; //Copy Clipboard data
    begin // return true if data copied.
    memo.Clear();
    memo.PasteFromClipboard();
    GetClipboardData := memo.Lines.Count > 0; //this is line 87
    end;

    Failed when compiling
    [Error] (87:26): open round ('(')expected

    finally ...
    FUNCTION GetClipboardData(var memo:TMemo):boolean; //Copy Clipboard data
    begin // return true if data copied.
    memo.Clear();
    memo.PasteFromClipboard();
    result := memo.Lines.Count > 0;
    end;
    Compiles ok.

    So I am going to stick with the result syntax.



    Ive spent some time at WIKI and run some event scripts (ur right, a little
    buggy, well perhaps not buggy so much as 'interesting' :) ) and I am
    bastardizing Alains script which iterates over a group.

    Trouble with his example is he has output to a form, set options via ini
    file and with an option form confirm and some code for restoring the views.

    I can live without all that and its a powerful complication around the
    iterator.

    ty again
    Warwick
     
    Warwick, Dec 22, 2003
    #3
  4. Suddenly, Warwick sprang forth and uttered these pithy words:
    > What is getting to me is what I know in C as the return statement.


    No such thing.

    result is an implicit variable (type of the function's return type) that
    will be returned.
    if you want to return early then use exit.
    so for example

    function sqaure (x :int):int;
    begin
    if x = 7 then
    begin
    result := 0;
    exit;
    end;
    result := x * x;
    end; //return statement is assignment into result.


    Personally I find having a result variable effectively declared for you
    often handy, as in many cases one ends up doing the same thing in C,
    manually...



    --
    aaronl at consultant dot com
    For every expert, there is an equal and
    opposite expert. - Arthur C. Clarke
     
    Aaron Lawrence, Dec 22, 2003
    #4
  5. Warwick wrote:
    > function sqaure (x :int):int;
    > begin
    > result := x * x;
    > end; //return statement is assignment into result.
    > //result is not declared anywhere, seems to be a typeless
    > global?!


    Result is the return value. It's the same as doing square:=x*x;

    Cheers,
    Nicholas Sherlock
     
    Nicholas Sherlock, Dec 22, 2003
    #5
  6. Warwick

    Mainlander Guest

    In article <18k1frt6ikm7o$.15b992uk76yrp$>,
    says...
    > I am playing with the scripting tool in Dialog.
    > So far I am crash coursing the necessary pascal ok but I have one question
    > that I can't seem to readily find the answer to online.
    >
    > Functions always return values in Pascal (if there is a void return type
    > its called a procedure I think lol).
    >
    > What is getting to me is what I know in C as the return statement.
    >
    > Declaration for a function that takes an int by value and returns its
    > square might look like this.
    > function sqaure (x :int):int;
    > begin
    > foo := x * x;
    > end; //return statement is assignment into function name.


    No,
    sqaure := x * x;

    >
    > however in the script examples the functions are of this form...
    >
    > function sqaure (x :int):int;
    > begin
    > result := x * x;
    > end; //return statement is assignment into result.
    > //result is not declared anywhere, seems to be a typeless global?!
    >
    > in the online manuals only the first form in mentioned.
    > If I try replacing the second form with the first in a dialog script it
    > throws an error.
    >
    > Can anyone explain to me whats going on here?


    The first format is correct. The second is part of the Delphi Object
    Pascal extensions and not part of standard Pascal.

    --
    Full featured open source Win32 newsreader - Gravity 2.70
    http://sourceforge.net/projects/mpgravity/
     
    Mainlander, Dec 22, 2003
    #6
  7. Warwick

    Mainlander Guest

    In article <okxfb2hgujj3.p7kd9j6suuo8$>,
    says...
    > On Mon, 22 Dec 2003 22:06:01 +1300, Rudy Seoa wrote:
    >
    > >> Declaration for a function that takes an int by value and returns its
    > >> square might look like this.
    > >> function sqaure (x :int):int;
    > >> begin
    > >> foo := x * x;
    > >> end; //return statement is assignment into function name.

    > >
    > > Pascal's integer type is 'integer' not 'int'.
    > >
    > > This will work:
    > > function square (x :integer):integer;
    > > begin
    > > square := x * x;
    > > end;
    > >
    > >> however in the script examples the functions are of this form...
    > >>
    > >> function sqaure (x :int):int;
    > >> begin
    > >> result := x * x;
    > >> end; //return statement is assignment into result.
    > >> //result is not declared anywhere, seems to be a typeless global?!

    > >
    > > This should work as well, if you correct the integer declarations.
    > >
    > > The scripting is standard Object Pascal so any Delphi programming
    > > resource should help you, and there's a bunch of example scripts in the
    > > Dialog homepage's Wiki.
    > >
    > > Dialog looks promising, but it's still very beta'ish (I could make it
    > > crash with a AV in the initial configuration wizard)

    >
    > Hey tyvm!
    >
    > Sorry bout the int declarations, I was in C++ Mode.
    > I like to think I am familiar with CBuilder so the VCL types in Dialog are
    > all familiar.
    >
    > The first form (which sits more comfortably with me, the second suggests a
    > loosely typed global called result - which is an odd thing to have in a
    > strictly typed langauge) will not work in Dialog.


    Object Pascal (and C) are not strictly typed languages.

    "Every function implicitly has a local variable Result of the same type
    as the function's return value. Assigning to Result has the same effect
    as assigning to the name of the function. In addition, however, you can
    refer to Result in an expression, which refers to the current return
    value rather than generating a recursive function call."

    from Delphi Help

    --
    Full featured open source Win32 newsreader - Gravity 2.70
    http://sourceforge.net/projects/mpgravity/
     
    Mainlander, Dec 22, 2003
    #7
  8. In article <>,
    Aaron Lawrence <> wrote:

    >Personally I find having a result variable effectively declared for you
    >often handy, as in many cases one ends up doing the same thing in C,
    >manually...


    The Pascal construct also allows you to do:

    function outer
    (
    x : integer
    ) : integer;

    procedure inner;

    begin
    ...
    outer := x * x
    end {inner};

    begin {outer}
    ...
    inner;
    ...
    end {outer};

    By the way, interesting that when Niklaus Wirth designed Modula-2, the
    successor to Pascal, he dropped the above assign-to-function-name
    approach and adopted C-style RETURN statements.

    When I did a programming-language design for my Master's thesis, I
    decided to explicitly name the variable for holding the return result,
    allowing for things like:

    function outer
    (
    x : integer
    ) returning
    outer_result : integer;

    function inner
    (
    y :integer
    ) returning
    inner_result : integer;

    begin
    ...
    inner_result := outer_result + y
    end inner;

    begin {outer}
    outer_result := 0;
    ...
    outer_result := outer_result + inner_result(2 * x);
    ...
    end outer;

    though not of course with this syntax.
     
    Lawrence D¹Oliveiro, Dec 22, 2003
    #8
  9. Warwick

    Warwick Guest

    On Tue, 23 Dec 2003 02:01:57 +1200, Aaron Lawrence wrote:

    > Suddenly, Warwick sprang forth and uttered these pithy words:
    >> What is getting to me is what I know in C as the return statement.

    >
    > No such thing.
    >
    > result is an implicit variable (type of the function's return type) that
    > will be returned.
    > if you want to return early then use exit.
    > so for example
    >
    > function sqaure (x :int):int;
    > begin
    > if x = 7 then
    > begin
    > result := 0;
    > exit;
    > end;
    > result := x * x;
    > end; //return statement is assignment into result.
    >
    >
    > Personally I find having a result variable effectively declared for you
    > often handy, as in many cases one ends up doing the same thing in C,
    > manually...


    Jeez thanks for that. I would have assumed the result assignment exited the
    function! Bound to have caused a headache or two down the line !
     
    Warwick, Dec 22, 2003
    #9
  10. Warwick

    Warwick Guest

    On Mon, 22 Dec 2003 21:05:29 +1300, Warwick wrote:

    > I am playing with the scripting tool in Dialog.
    > So far I am crash coursing the necessary pascal ok but I have one question
    > that I can't seem to readily find the answer to online.
    >
    > Functions always return values in Pascal (if there is a void return type
    > its called a procedure I think lol).
    >
    > What is getting to me is what I know in C as the return statement.
    >
    > Declaration for a function that takes an int by value and returns its
    > square might look like this.
    > function sqaure (x :int):int;
    > begin
    > foo := x * x;
    > end; //return statement is assignment into function name.
    >
    > however in the script examples the functions are of this form...
    >
    > function sqaure (x :int):int;
    > begin
    > result := x * x;
    > end; //return statement is assignment into result.
    > //result is not declared anywhere, seems to be a typeless global?!
    >
    > in the online manuals only the first form in mentioned.
    > If I try replacing the second form with the first in a dialog script it
    > throws an error.
    >
    > Can anyone explain to me whats going on here?
    >
    > TIA
    > Warwick
    >
    >
    >
    >


    An especially Merry Christmas, Rudy, Mainlander, Aaron and Nicholas :)
    Precisely the answer I required :)

    cheers
     
    Warwick, Dec 22, 2003
    #10
  11. Warwick

    Mainlander Guest

    In article <ebpjr9lp9r0n.1sq7e8o47o0e3$>,
    says...
    > On Tue, 23 Dec 2003 02:01:57 +1200, Aaron Lawrence wrote:
    >
    > > Suddenly, Warwick sprang forth and uttered these pithy words:
    > >> What is getting to me is what I know in C as the return statement.

    > >
    > > No such thing.
    > >
    > > result is an implicit variable (type of the function's return type) that
    > > will be returned.
    > > if you want to return early then use exit.
    > > so for example
    > >
    > > function sqaure (x :int):int;
    > > begin
    > > if x = 7 then
    > > begin
    > > result := 0;
    > > exit;
    > > end;
    > > result := x * x;
    > > end; //return statement is assignment into result.
    > >
    > >
    > > Personally I find having a result variable effectively declared for you
    > > often handy, as in many cases one ends up doing the same thing in C,
    > > manually...

    >
    > Jeez thanks for that. I would have assumed the result assignment exited the
    > function! Bound to have caused a headache or two down the line !


    You can use it like any other variable name i.e. in expressions

    x := result * 2;

    --
    Full featured open source Win32 newsreader - Gravity 2.70
    http://sourceforge.net/projects/mpgravity/
     
    Mainlander, Dec 23, 2003
    #11
  12. Warwick

    Bruce Hoult Guest

    In article <>,
    Lawrence D¹Oliveiro <_zealand> wrote:

    > When I did a programming-language design for my Master's thesis, I
    > decided to explicitly name the variable for holding the return result,
    > allowing for things like:
    >
    > function outer
    > (
    > x : integer
    > ) returning
    > outer_result : integer;
    >
    > function inner
    > (
    > y :integer
    > ) returning
    > inner_result : integer;
    >
    > begin
    > ...
    > inner_result := outer_result + y
    > end inner;
    >
    > begin {outer}
    > outer_result := 0;
    > ...
    > outer_result := outer_result + inner_result(2 * x);
    > ...
    > end outer;
    >
    > though not of course with this syntax.


    And Peano was also not ever implemented, as I recall?

    -- Bruce
     
    Bruce Hoult, Dec 23, 2003
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. olabanji  timothy

    pls, help.. i need a number..pls

    olabanji timothy, Sep 9, 2003, in forum: MCSE
    Replies:
    7
    Views:
    834
  2. _303

    My 1st pascal Need some help here ...:O)

    _303, Jul 31, 2003, in forum: Computer Support
    Replies:
    6
    Views:
    475
    Boomer
    Aug 3, 2003
  3. fgphotog

    Pascal compiler ?

    fgphotog, Oct 16, 2003, in forum: Computer Support
    Replies:
    3
    Views:
    497
  4. Churchill, Dave

    Pascal Language

    Churchill, Dave, Jul 21, 2005, in forum: Computer Support
    Replies:
    2
    Views:
    573
  5. Replies:
    3
    Views:
    406
    measekite
    Feb 28, 2005
Loading...

Share This Page