O3-Procedures and Functions

Embed Size (px)

Citation preview

  • 8/6/2019 O3-Procedures and Functions

    1/13

    PROCEDURES AND FUNCTIONS

    Procedures are sub-programs that can be called from the main part of the program. Procedures aredeclared outside of the main program body using the procedure k eyword. Procedures must also begiven a unique name. Procedures have their own begin and end . Here is an example of how to ma k e aprocedure called Hello that prints "Hello" on the screen.

  • 8/6/2019 O3-Procedures and Functions

    2/13

    To use a procedure we must call it by using its name in themain body.

    Procedures must always be above where they are called from. program Proc1;

    procedure Hai;begin

    Writeln('Hello');end;

    beginHai;

    end.

  • 8/6/2019 O3-Procedures and Functions

    3/13

    program Proc1;

    procedure Hai;begin

    Writeln('Hello');

    end;

    procedure HaiBro;begin

    Hai;end;

    beginHaiBro;

    end.

  • 8/6/2019 O3-Procedures and Functions

    4/13

    Procedures can have parameters just like the other commands we have beenusing. Each parameter is given a name and type and is then used just like any other variable. If you want to use more than one parameter then they must be separated

    with semi-colons. program Proc2;

    procedure Ceta k (s: String; i: Integer);begin

    Writeln(s);Writeln(i);

    end;

    beginCeta k ('Hello',3);end.

  • 8/6/2019 O3-Procedures and Functions

    5/13

    GL OBA L AND L OCA L VARIAB L ES

    The variables we have been using so far havebeen global because they can be used at anytime during the program. Local variables canonly be used inside procedures but the memorythey use is released when the procedure is notbeing used. Local variables are declared just

    underneath the procedure name declaration.

  • 8/6/2019 O3-Procedures and Functions

    6/13

    program Proc2;

    procedure Ceta k (s: String);var

    i: Integer;beginfor i := 1 to 3 do

    Writeln(s);end;

    beginCeta k ('Hello');

    end.

  • 8/6/2019 O3-Procedures and Functions

    7/13

    FUNCTIONS

    F unctions are li k e procedures except theyreturn a value. The f unction k eyword is usedinstead of procedure when declaring afunction. To say what data type the return valuemust be you must use a colon and the name of the type after the function's name.

  • 8/6/2019 O3-Procedures and Functions

    8/13

    program F ung1;

    function Tambah(i, j:Integer): Integer;beginend;

    beginend.

  • 8/6/2019 O3-Procedures and Functions

    9/13

    Assigning the value of a function to a variablema k e the variable equal to the return value. If you use a function in something li k e W riteln itwill print the return value. To set the returnvalue just ma k e the name of the function equalto the value you want to return.

  • 8/6/2019 O3-Procedures and Functions

    10/13

    program F ung1;

    varAnswer: Integer;

    function Tambah(i, j:Integer): Integer;begin

    Tambah := i + j;end;

    beginAnswer := Tambah(1,2);Writeln( Tambah(1,2));

    end.

  • 8/6/2019 O3-Procedures and Functions

    11/13

    You can exit a procedure or function at any time byusing the Exit command.

    program Proc3;

    procedure GNama;var

    Name: String;

    beginWriteln('What is your name?');Readln(Name);if Name = '' then

    Exit;Writeln('Your name is ',Name);

    end;begin

    GNama;end.

  • 8/6/2019 O3-Procedures and Functions

    12/13

  • 8/6/2019 O3-Procedures and Functions

    13/13