Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-04

Syntax problem with first unit

10 messages · 5 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Francisco Reyes

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.

Mehmet Erol Sanliturk
to Francisco Reyes
Francisco Reyes wrote 42 lines
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

Francisco Reyes
to Mehmet Erol Sanliturk
Mehmet Erol Sanliturk wrote 5 lines
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.

Michael Van Canneyt
to Francisco Reyes
Francisco Reyes wrote 12 lines
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.

Tomas Hajny
to Francisco Reyes: Thanks. That worked. It may help newcomers like myself if this was mentioned on the ref…
Francisco Reyes wrote 16 lines
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

David W Noon
to Francisco Reyes: Thanks. That worked. It may help newcomers like myself if this was mentioned on the ref…
Francisco Reyes wrote 15 lines
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] ======================================================================= (David W Noon) =======================================================================
Francisco Reyes
to Michael Van Canneyt: It most certainly does: see the section on reserved words, first paragraph. Michael.
Michael Van Canneyt wrote
> 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.

Francisco Reyes
to Tomas Hajny: Note that although having the unit named equally to a function, procedure or a variable…
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

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.

Mehmet Erol Sanliturk
to Tomas Hajny: Note that although having the unit named equally to a function, procedure or a variable…
Tomas Hajny wrote 15 lines
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

Tomas Hajny
to Francisco Reyes: In the ref document? Page10? Section 1.3? Just looked there and didn't see it it. There…
Francisco Reyes wrote 5 lines
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