Syntax problem with first unit

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Syntax problem with first unit

Francisco Reyes-2
Trying my first unit and getting an error I can't figure out.

--- unit
unit debugprint;
Interface
function DebugPrint(ProgLevel, CallLevel:integer; MessageIn: string):string;

Implementation

function DebugPrint(ProgLevel, CallLevel:integer; MessageIn: string):string;
begin
        if ProgLevel>= CallLevel  then
                DebugPrint := MessageIn ;
end;

end.

--- Program to test unit
program tdp;
uses debugprint;

var
Msg: string;

begin
writeln ('hello world');
Msg := DebugPrint(0,1,'Debug message');
writeln(Msg);
end.

------

Getting an error
tdp.pas(9,18) Fatal: Syntax error, "." expected but "(" found
Line 9 is the call to the function in the unit.

If I take the function and move it from the unit to the main
program it works fine.

Any pointers greatly appreciated.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Syntax problem with first unit

Mehmet Erol Sanliturk-2


Francisco Reyes wrote:

> Trying my first unit and getting an error I can't figure out.
>
> --- unit
> unit debugprint;
> Interface
> function DebugPrint(ProgLevel, CallLevel:integer; MessageIn:
> string):string;
>
> Implementation
>
> function DebugPrint(ProgLevel, CallLevel:integer; MessageIn:
> string):string;
> begin
>     if ProgLevel>= CallLevel  then
>         DebugPrint := MessageIn ;
> end;
>
> end.
>
> --- Program to test unit
> program tdp;
> uses debugprint;
>
> var
> Msg: string;
>
> begin
> writeln ('hello world');
> Msg := DebugPrint(0,1,'Debug message');
> writeln(Msg);
> end.
>
> ------
>
> Getting an error
> tdp.pas(9,18) Fatal: Syntax error, "." expected but "(" found
> Line 9 is the call to the function in the unit.
>
> If I take the function and move it from the unit to the main program
> it works fine.
>
> Any pointers greatly appreciated.
Unit and function names DebugPrint are the same .
Pascal is case insensitive . Making their cases different does NOT
make them different .


Please make them different and retry .

Thank you very much .

Mehmet Erol Sanliturk






_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Syntax problem with first unit

Francisco Reyes-2
Mehmet Erol Sanliturk writes:

> Unit and function names DebugPrint are the same .
> Pascal is case insensitive . Making their cases different does NOT
> make them different .
>
> Please make them different and retry .

Thanks. That worked.

It may help newcomers like myself if this was mentioned on the ref document.
Just double checked and it does not mention this.

Likely an error that people will only commit once, but still would be nice
to have int he ref doc.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Syntax problem with first unit

Michael Van Canneyt


On Sun, 5 Apr 2009, Francisco Reyes wrote:

> Mehmet Erol Sanliturk writes:
>
> > Unit and function names DebugPrint are the same .
> > Pascal is case insensitive . Making their cases different does NOT
> > make them different .
> >
> > Please make them different and retry .
>
> Thanks. That worked.
>
> It may help newcomers like myself if this was mentioned on the ref document.
> Just double checked and it does not mention this.

It most certainly does: see the section on reserved words, first paragraph.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Syntax problem with first unit

Tomas Hajny
In reply to this post by Francisco Reyes-2
On Sun, April 5, 2009 16:03, Francisco Reyes wrote:

> Mehmet Erol Sanliturk writes:
>
>> Unit and function names DebugPrint are the same .
>> Pascal is case insensitive . Making their cases different does NOT
>> make them different .
>>
>> Please make them different and retry .
>
> Thanks. That worked.
>
> It may help newcomers like myself if this was mentioned on the ref
> document.
> Just double checked and it does not mention this.
>
> Likely an error that people will only commit once, but still would be nice
> to have int he ref doc.

Note that although having the unit named equally to a function, procedure
or a variable defined within the unit (or elsewhere) is not really
advisable, it is possible, but you need to take scoping rules into account
when trying to access the function, procedure or variable. In your
particular case, you would need to use DebugPrint.DebugPrint (your
parameters) in order to tell the compiler that you refer to the function
DebugPrint stored within unit DebugPrint referenced in the "uses" section.
That's also why the compiler suggested that you should use '.' after
DebugPrint.

Tomas


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Syntax problem with first unit

David W Noon
In reply to this post by Francisco Reyes-2
On Sun, 2009-04-05 at 10:03 -0400, Francisco Reyes wrote:

> Mehmet Erol Sanliturk writes:
>
> > Unit and function names DebugPrint are the same .
> > Pascal is case insensitive . Making their cases different does NOT
> > make them different .
> >
> > Please make them different and retry .
>
> Thanks. That worked.
>
> It may help newcomers like myself if this was mentioned on the ref document.
> Just double checked and it does not mention this.
>
> Likely an error that people will only commit once, but still would be nice
> to have int he ref doc.

But the Pascal language has *always* been case insensitive. [As are
FORTRAN, PL/I, ALGOL 60/68, COBOL and a host of others.]

I wrote my first Pascal programs on punched cards around 1975, hosted on
a Control Data Corp. Cyber 72 mainframe. The key-punch machines we used
to punch the cards did not support lower case letters, so we could not
readily prepare case sensitive input. This is why almost all the older
generation of programming languages are case insensitive.

Once we started using timesharing on the mainframe, we were
automatically accustomed to lower case letters being treated the same as
capital letters, as even the system commands were punched in capital
letters on cards, but typed as lower case from a terminal.

--
Regards,

Dave  [RLU #314465]
=======================================================================
[hidden email] (David W Noon)
=======================================================================
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Syntax problem with first unit

Francisco Reyes-2
In reply to this post by Michael Van Canneyt
Michael Van Canneyt writes:

>> It may help newcomers like myself if this was mentioned on the ref document.
>> Just double checked and it does not mention this.
>
> It most certainly does: see the section on reserved words, first paragraph.

In the ref document? Page10? Section 1.3?
Just looked there and didn't see it it.

There is a reference to case insensitivity, but that is not what I was
suggesting. I was tying to suggest to mention that a function can not have
the same name as a unit name. I am sure this is obvious to experienced
users, but may not be for a newcomer.

In particular it would be helpful if that note (ie name of fuction and unit
name can't be same) was in the unit section.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Syntax problem with first unit

Francisco Reyes-2
In reply to this post by Tomas Hajny
Tomas Hajny writes:

> Note that although having the unit named equally to a function, procedure
> or a variable defined within the unit (or elsewhere) is not really
> advisable, it is possible, but you need to take scoping rules into account

Thanks for the note. I wasn't purposedly trying to do it though. Just didn't
know it would be a problem.

> That's also why the compiler suggested that you should use '.' after
> DebugPrint.

Aha!
The error didn't make sense, but with your explanation now it does.

I just changed the unit name.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Syntax problem with first unit

Mehmet Erol Sanliturk-2
In reply to this post by Tomas Hajny


Tomas Hajny wrote:

>
> Note that although having the unit named equally to a function, procedure
> or a variable defined within the unit (or elsewhere) is not really
> advisable, it is possible, but you need to take scoping rules into account
> when trying to access the function, procedure or variable. In your
> particular case, you would need to use DebugPrint.DebugPrint (your
> parameters) in order to tell the compiler that you refer to the function
> DebugPrint stored within unit DebugPrint referenced in the "uses" section.
> That's also why the compiler suggested that you should use '.' after
> DebugPrint.
>
> Tomas
>
>
>  
The following addition may be made to the above explanation for a little
more clarification :

When compiler scanned the assignment statement := , it found the name
DebugPrint .
Within the scope of the program DebugPrint is a unit name whereas there
should be
a variable name . For that reason , the compiler assumed that there
should be a qualification of a variable name by the unit name such as

 ... := DebugPrint . x ...

Instead of finding a period and variable name after the unit name
DebugPrint , it found a left parenthesis .

Due to this , it gave the message  ... Expected . ( period ) but found (
left parenthesis ) .

Thank you very much .

Mehmet Erol Sanliturk








_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Syntax problem with first unit

Tomas Hajny
In reply to this post by Francisco Reyes-2
On Sun, April 5, 2009 17:23, Francisco Reyes wrote:
> Michael Van Canneyt writes:
>
>>> It may help newcomers like myself if this was mentioned on the ref
>>> document.
>>> Just double checked and it does not mention this.
 .
 .
> In particular it would be helpful if that note (ie name of fuction and
> unit
> name can't be same) was in the unit section.

Well, this is something more general, not specific to units - using the
same identifier for different things (although possibly having different
scope) may lead to conflicts, of course. As an example, the same is true
for having the same name for a local variable (function, procedure) and a
global one. In this context, placing such a note in the unit section isn't
very logical. I haven't checked whether some hint in this sense appears in
the general section about identifiers, but that is the right place in my
opinion (if we want to mention something like that explicitly - note that
it's quite logical and everyone understands that if two people have the
same name, one needs to find some other means of differentiating between
them.

Tomas


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal