RSS Forums RSS
Please support our Pascal and Delphi advertiser: Programming Forums

A few questions to finish up my project

Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,884
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 197
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: A few questions to finish up my project

  #12  
Nov 20th, 2007
Ah.

The difference is whether the function is a method of the TMainForm class or not.

A quick tutorial:

Forward declarations vs. automatic declarations
Normally you might have a program that looks like this:
  1. program foo;
  2.  
  3. function triple_it( x: real ): real;
  4. begin
  5. triple_it := x *3
  6. end;
  7.  
  8. var
  9. number: real;
  10.  
  11. begin
  12. write( 'Please enter a number to be tripled> ' );
  13. readln( r );
  14. writeln( r, ' tripled is ', triple_it( r ) )
  15. end.
In this little program, we have both declared and defined a function named "triple_it". By declaring, we mean that we have told the compiler that it exists. By defining, we mean that we have told the compiler all the details about it. Typically, as in this example, things are declared at the same time they are defined. That is, by defining a thing we implicitly declare it as well.

In Pascal, a thing must be at least declared before it is used.

Sometimes, however, we need to tell a program that something exists before we actually define it. Hence, we need to use a forward declaration. Here's a simplistic example lifted off of the Wikipedia (and repaired for correctness):
  1. program oddness;
  2.  
  3. function is_it_even( n: integer ): boolean; forward;
  4.  
  5. function is_it_odd( n: integer ): boolean;
  6. begin
  7. if n = 1
  8. then is_it_odd := TRUE
  9. else is_it_odd := is_it_even( n -1 )
  10. end;
  11.  
  12. function is_it_even;
  13. begin
  14. if n = 1
  15. then is_it_even := FALSE
  16. else is_it_even := is_it_odd( n -1 )
  17. end;
  18.  
  19. var x: integer;
  20.  
  21. begin
  22. write( 'Please enter a whole number greater than zero> ' );
  23. readln( x );
  24. writeln( 'The statement "', x, ' is odd" is ', is_it_odd( x ), '.' );
  25. writeln( 'The statement "', x, ' is even" is ', is_it_even( x ), '.' )
  26. end.
Notice how we used the function "is_it_even" before we defined it? We told the compiler about it beforehand using the forward keyword. That is, we declared it. Thereafter we only needed to define it somewhere in the program.

This is how a unit works. They have an interface section, wherein things are declared, and an implementation section, wherein things are defined.
(If anyone here is using Extended Pascal and wants to know how this looks in a module, post here and I'll post with the details.) Here's a unit that provides the two functions:
  1. unit oddstuff;
  2.  
  3. interface
  4.  
  5. function is_it_even( n: integer ): boolean;
  6. function is_it_odd( n: integer ): boolean;
  7.  
  8. implementation
  9.  
  10. function is_it_odd;
  11. begin
  12. if n = 1
  13. then is_it_odd := TRUE
  14. else is_it_odd := is_it_even( n -1 )
  15. end;
  16.  
  17. function is_it_even;
  18. begin
  19. if n = 1
  20. then is_it_even := FALSE
  21. else is_it_even := is_it_odd( n -1 )
  22. end;
  23.  
  24. end.
And the program that uses it:
  1. program oddness;
  2. uses oddstuff;
  3. var x: integer;
  4. begin
  5. write( 'Please enter a whole number greater than zero> ' );
  6. readln( x );
  7. writeln( 'The statement "', x, ' is odd" is ', is_it_odd( x ), '.' );
  8. writeln( 'The statement "', x, ' is even" is ', is_it_even( x ), '.' )
  9. end.

One thing to note is that in the definition of the procedures/functions, you only needed to name it. You did not need to specify all the parameters and the like. Typically Borland IDEs and variations will duplicate the procedure/function header with the definition. Both have to match exactly. But it is not strictly necessary to duplicate the declaration with the definition.


Classes
A class is always described in two parts: a declaration part and a definition part. (This separation is necessary, but I'll not go into details as to why here.) The declaration looks like you are used to seeing:
  1. type
  2. tMatrix = class
  3. private
  4. f_rows, f_cols: integer;
  5. f_elements: array of array of extended;
  6. public
  7. constructor create( rows, cols: integer );
  8. function get_element( row, col: integer ): extended;
  9. procedure set_element( row, col: integer; value: extended );
  10. end;
In the definition, you must fully qualify the name of the methods of the class, otherwise the compiler thinks you are talking about some other procedure named (e.g.) "set_element" --not the method of the class.

So our definitions may be:
  1. constructor tMatrix.create( rows, cols: integer );
  2. begin
  3. f_rows := rows;
  4. f_cols := cols;
  5. setLength( f_elements, f_rows, f_cols )
  6. end;
  7.  
  8. function tMatrix.get_element( row, col: integer ): extended;
  9. begin
  10. result := f_elements[ row, col ]
  11. end;
  12.  
  13. procedure tMatrix.set_element( row, col: integer; value: extended );
  14. begin
  15. f_elements[ row, col ] := value
  16. end;
In each instance, I had to prefix the name of the class to the name of the method, so that the forward declaration is satisfied. If I had just said:
function get_element( row, col: integer ): extended;
  begin
  result := f_elements[ row, col ]
  end;
The compiler would have complained, saying: I don't have a function named "get_element". (The only thing I've got so far is a class named "tMatrix".)

Since the get_element function is a member of tMatrix, we must use its full name.

Well, I hope this explains things better.
Reply With Quote  
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:49 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC