Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4095
Number of posts: 14004

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
units Posted by MaStMuNdA on 10 Mar 2003 at 2:11 PM
some one plz help me create units i have trid but i get all sorts of errors. so if some one could give an example in detail plz
Report
Re: units Posted by sweeney on 10 Mar 2003 at 4:40 PM
: some one plz help me create units i have trid but i get all sorts of errors. so if some one could give an example in detail plz
:
could you posibly post ur code or some of it so we know what u mean lol!
Report
Re: units Posted by Phat Nat on 10 Mar 2003 at 5:22 PM
: some one plz help me create units i have trid but i get all sorts of errors. so if some one could give an example in detail plz

I already posted this, but I will add to it now that I have more time. Try this code and you should be able to see how it works.

This is the Unit. It must be saved as UNITINFO.PAS or The First 8 letters of the Unit Name.
UNIT UnitInfo;

INTERFACE          { Anything in here is available to other programs }
USES Crt, Dos;

VAR       { Any program that uses the Unit can access these variables }
   Counter : Byte;

(********************************************************************
 * Start Procedure Declarations. These need to be defined later.    *
 ********************************************************************)
PROCEDURE ShowData;
PROCEDURE SetValue(Num : Byte);
PROCEDURE SetCounter(Num : Byte);
FUNCTION  GetValue : Byte;
FUNCTION  GetCounter : Byte;

IMPLEMENTATION { Anything in here is not available to other programs }

VAR       { Only the Unit can access these variables }
   Value : Byte;


(********************************************************************
 * Start Procedure Definitions                                      *
 ********************************************************************)

PROCEDURE ShowData;
Begin
     Writeln('Current Data = ',Value);
End;

PROCEDURE SetValue(Num : Byte);
Begin
     Value := Num;
End;

PROCEDURE SetCounter(Num : Byte);
Begin
     Counter := Num;
End;

FUNCTION  GetValue : Byte;
Begin
     GetValue := Value;
End;

FUNCTION  GetCounter : Byte;
Begin
     GetCounter := Counter;
End;

(********************************************************************
 * This is run before the program that uses the unit starts.        *
 ********************************************************************)
Begin
     Value := 0;
     Counter := 0;
End.


This is an example of the program that would use it.

PROGRAM TestUnitInfo;
USES UnitInfo;

Begin
     ShowData;                        { Value cannot be used directly }
     WriteLn('Counter = ',Counter);   { Counter can be used directly  }
     SetValue(13);
     ShowData;
     SetCounter(22);
     WriteLn('Counter = ',Counter);
End.


If you still have questions, just ask. If you still have problems with your unit, post the code and the errors that it gives.

Phat Nat
Report
Re: units Posted by MaStMuNdA on 11 Mar 2003 at 2:05 AM
: : some one plz help me create units i have trid but i get all sorts of errors. so if some one could give an example in detail plz
:
: I already posted this, but I will add to it now that I have more time. Try this code and you should be able to see how it works.
:
: This is the Unit. It must be saved as UNITINFO.PAS or The First 8 letters of the Unit Name.
:
: UNIT UnitInfo;
: 
: INTERFACE          { Anything in here is available to other programs }
: USES Crt, Dos;
: 
: VAR       { Any program that uses the Unit can access these variables }
:    Counter : Byte;
: 
: (********************************************************************
:  * Start Procedure Declarations. These need to be defined later.    *
:  ********************************************************************)
: PROCEDURE ShowData;
: PROCEDURE SetValue(Num : Byte);
: PROCEDURE SetCounter(Num : Byte);
: FUNCTION  GetValue : Byte;
: FUNCTION  GetCounter : Byte;
: 
: IMPLEMENTATION { Anything in here is not available to other programs }
: 
: VAR       { Only the Unit can access these variables }
:    Value : Byte;
: 
: 
: (********************************************************************
:  * Start Procedure Definitions                                      *
:  ********************************************************************)
: 
: PROCEDURE ShowData;
: Begin
:      Writeln('Current Data = ',Value);
: End;
: 
: PROCEDURE SetValue(Num : Byte);
: Begin
:      Value := Num;
: End;
: 
: PROCEDURE SetCounter(Num : Byte);
: Begin
:      Counter := Num;
: End;
: 
: FUNCTION  GetValue : Byte;
: Begin
:      GetValue := Value;
: End;
: 
: FUNCTION  GetCounter : Byte;
: Begin
:      GetCounter := Counter;
: End;
: 
: (********************************************************************
:  * This is run before the program that uses the unit starts.        *
:  ********************************************************************)
: Begin
:      Value := 0;
:      Counter := 0;
: End.
: 

:
: This is an example of the program that would use it.
:
:
: PROGRAM TestUnitInfo;
: USES UnitInfo;
: 
: Begin
:      ShowData;                        { Value cannot be used directly }
:      WriteLn('Counter = ',Counter);   { Counter can be used directly  }
:      SetValue(13);
:      ShowData;
:      SetCounter(22);
:      WriteLn('Counter = ',Counter);
: End.
: 

:
: If you still have questions, just ask. If you still have problems with your unit, post the code and the errors that it gives.
:
: Phat Nat
:

what is that stuff about functions?
Report
Re: units Posted by Phat Nat on 11 Mar 2003 at 8:49 AM
: : : some one plz help me create units i have trid but i get all sorts of errors. so if some one could give an example in detail plz
: :
: : I already posted this, but I will add to it now that I have more time. Try this code and you should be able to see how it works.
: :
: : This is the Unit. It must be saved as UNITINFO.PAS or The First 8 letters of the Unit Name.
: :
: : UNIT UnitInfo;
: : 
: : INTERFACE          { Anything in here is available to other programs }
: : USES Crt, Dos;
: : 
: : VAR       { Any program that uses the Unit can access these variables }
: :    Counter : Byte;
: : 
: : (********************************************************************
: :  * Start Procedure Declarations. These need to be defined later.    *
: :  ********************************************************************)
: : PROCEDURE ShowData;
: : PROCEDURE SetValue(Num : Byte);
: : PROCEDURE SetCounter(Num : Byte);
: : FUNCTION  GetValue : Byte;
: : FUNCTION  GetCounter : Byte;
: : 
: : IMPLEMENTATION { Anything in here is not available to other programs }
: : 
: : VAR       { Only the Unit can access these variables }
: :    Value : Byte;
: : 
: : 
: : (********************************************************************
: :  * Start Procedure Definitions                                      *
: :  ********************************************************************)
: : 
: : PROCEDURE ShowData;
: : Begin
: :      Writeln('Current Data = ',Value);
: : End;
: : 
: : PROCEDURE SetValue(Num : Byte);
: : Begin
: :      Value := Num;
: : End;
: : 
: : PROCEDURE SetCounter(Num : Byte);
: : Begin
: :      Counter := Num;
: : End;
: : 
: : FUNCTION  GetValue : Byte;
: : Begin
: :      GetValue := Value;
: : End;
: : 
: : FUNCTION  GetCounter : Byte;
: : Begin
: :      GetCounter := Counter;
: : End;
: : 
: : (********************************************************************
: :  * This is run before the program that uses the unit starts.        *
: :  ********************************************************************)
: : Begin
: :      Value := 0;
: :      Counter := 0;
: : End.
: : 

: :
: : This is an example of the program that would use it.
: :
: :
: : PROGRAM TestUnitInfo;
: : USES UnitInfo;
: : 
: : Begin
: :      ShowData;                        { Value cannot be used directly }
: :      WriteLn('Counter = ',Counter);   { Counter can be used directly  }
: :      SetValue(13);
: :      ShowData;
: :      SetCounter(22);
: :      WriteLn('Counter = ',Counter);
: : End.
: : 

: :
: : If you still have questions, just ask. If you still have problems with your unit, post the code and the errors that it gives.
: :
: : Phat Nat
: :
:
: what is that stuff about functions?
:

FUNCTIONs are just PROCEDUREs, but they will return a value. For example, if you needed your procedure to return a number to your program, you might do something like this:

VAR
   George : Byte;

PROCEDURE GetAge(VAR Age : Byte);
Begin
     Write('What is your age ? ');
     ReadLn(Age);
End;

{ Main Part of Program }
Begin
     GetAge(George);
     WriteLn('You are ',George,' years old!');
End.


This would write the number in GEORGE on the screen. Since it was passed to the procedure with the VAR in front, it will be put into the GEORGE variable. FUNCTIONS do the same sort of thing. You could do the same with a function like this:

FUNCTION GetAge : Byte;
VAR
   Age : Byte;
Begin
     Write('What is your age ? ');
     ReadLn(Age);
     GetAge := Age;
End;

{ Main Part of Program }
Begin
     WriteLn('You are ',GetAge,' years old!');
End.


The main reason that you would use a FUNCTION and not a PROCEDURE with a VAR is so that you can put a procedure right into another statement. Look at the WriteLn() in the main part of the program. Before we had to get the age into a variable and then write it. Here we can just call the FUNCTION in the middle of the WriteLn() statement.

Sorry, this may not be the best explanation, but if this doesn't help just write back.

Phat Nat



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.