Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Computing > NZ Computing > Quick Pascal question pls

Reply
Thread Tools

Quick Pascal question pls

 
 
Warwick
Guest
Posts: n/a
 
      12-22-2003
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





 
Reply With Quote
 
 
 
 
Rudy Seoa
Guest
Posts: n/a
 
      12-22-2003
> 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)
 
Reply With Quote
 
 
 
 
Warwick
Guest
Posts: n/a
 
      12-22-2003
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
 
Reply With Quote
 
Aaron Lawrence
Guest
Posts: n/a
 
      12-22-2003
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
 
Reply With Quote
 
Nicholas Sherlock
Guest
Posts: n/a
 
      12-22-2003
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


 
Reply With Quote
 
Mainlander
Guest
Posts: n/a
 
      12-22-2003
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/
 
Reply With Quote
 
Mainlander
Guest
Posts: n/a
 
      12-22-2003
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/
 
Reply With Quote
 
Lawrence DčOliveiro
Guest
Posts: n/a
 
      12-22-2003
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.
 
Reply With Quote
 
Warwick
Guest
Posts: n/a
 
      12-22-2003
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 !
 
Reply With Quote
 
Warwick
Guest
Posts: n/a
 
      12-22-2003
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
URGENT - Pls help...pls recommend - laptop purchase irfansmith@gmail.com Computer Information 2 08-15-2008 11:34 PM
Quick question, hopefully quick answer. ~misfit~ NZ Computing 114 01-06-2005 01:36 PM
Quick question, hopefully quick answer. ~misfit~ NZ Computing 0 12-28-2004 11:55 PM
Quick Question Quick Answer JKop C++ 11 05-24-2004 09:46 PM
pls, help.. i need a number..pls olabanji timothy MCSE 7 09-10-2003 04:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57