: 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!
: 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. [code] 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;
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. [/code]
This is an example of the program that would use it.
[code] 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. [/code]
If you still have questions, just ask. If you still have problems with your unit, post the code and the errors that it gives.
: : 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. : [code] : 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. : [/code] : : This is an example of the program that would use it. : : [code] : 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. : [/code] : : 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 :
: : : 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. : : [code] : : 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. : : [/code] : : : : This is an example of the program that would use it. : : : : [code] : : 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. : : [/code] : : : : 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:
[code] 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. [/code]
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: [code]
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. [/code]
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.
Comments
:
could you posibly post ur code or some of it so we know what u mean lol!
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.
[code]
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.
[/code]
This is an example of the program that would use it.
[code]
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.
[/code]
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
:
: 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.
: [code]
: 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.
: [/code]
:
: This is an example of the program that would use it.
:
: [code]
: 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.
: [/code]
:
: 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?
: :
: : 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.
: : [code]
: : 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.
: : [/code]
: :
: : This is an example of the program that would use it.
: :
: : [code]
: : 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.
: : [/code]
: :
: : 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:
[code]
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.
[/code]
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:
[code]
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.
[/code]
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