Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

expressing functions as constants

Status
Not open for further replies.

johndelphi

IS-IT--Management
Jul 1, 2003
10
0
0
ZA
const
sSource =
'unit %s;' + #13#10 +
'// Username: ' + GetWindowsUserName + #13#10 +
'// Time: ' + DateToStr(Date) + #13#10 +
'' + #13#10 +
'interface' + #13#10 +
'' + #13#10 +
'uses' + #13#10 +
' SysUtils;' + #13#10 +
'' + #13#10 +
'type' + #13#10 +
' { Enter your type variables }' + #13#10 +
' private' + #13#10 +
' { Private declarations }' + #13#10 +
' public' + #13#10 +
' { Public declarations }' + #13#10 +
' end;' + #13#10 +
'' + #13#10 +
'var' + #13#10 +
' %s: T%s;' + #13#10 +
'' + #13#10 +
'implementation' + #13#10 +
'' + #13#10 +
'{$R *.DFM}' + #13#10 +
'' + #13#10 +
'end.';


getWindowsUserName and DateToStr need to be constants too because the sSource is a const input to another procedure.
how do i make the 2 functions const?
 
A procedure that is defined something like
Code:
procedure Test ( const s: string );
begin
  ...
end;
is not actually expecting a constant! What it is saying is that you will not modify the parameter s within the procedure Test. The compiler will flag up any attempt to modify s within the procedure Test.

The reason for a const in the procedure is one of efficiency. The compiler instead of passing s by value which is the default situation with Pascal passes it by reference. This is much faster.

The same effect could be achieved by passing s as a var parameter but you would not have the insurance of the compiler detecting if you tried to modify s.

So, you do not need to make the 2 functions const. You should create a variable and pass it to the procedure.

I hope this answers your question and makes sense.

Andrew


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top