Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

units

MaStMuNdAMaStMuNdA Posts: 17Member
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

Comments

  • sweeneysweeney Posts: 117Member
    : 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!
  • Phat NatPhat Nat Posts: 757Member
    : 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
  • MaStMuNdAMaStMuNdA Posts: 17Member
    : : 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?
  • Phat NatPhat Nat Posts: 757Member
    : : : 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.

    Phat Nat
Sign In or Register to comment.