MELJUN CORTES Programming Languages Subprograms

download MELJUN CORTES Programming Languages Subprograms

of 10

Transcript of MELJUN CORTES Programming Languages Subprograms

  • 8/8/2019 MELJUN CORTES Programming Languages Subprograms

    1/23

    Subprograms

    Programming Languages

    * Property of STI 

    Page 1 of 23

    Subprograms

     These are the fundamental building blocksof programs and are therefore among the

    most important concepts in programminglanguage design.

     Two forms:

    Procedure

    Function

    Components:

    Subprogram header 

    Subprogram implementation

    Entry point 

    Exit point 

  • 8/8/2019 MELJUN CORTES Programming Languages Subprograms

    2/23

    Subprograms

    Programming Languages

    * Property of STI 

    Page 2 of 23

    Subprograms

    Classifications:

    Internal subprogram

    External subprogram

     Advantages:

    Extensibility 

    Modularity 

    Reusability and maintainability 

     Abstraction

  • 8/8/2019 MELJUN CORTES Programming Languages Subprograms

    3/23

    Subprograms

    Programming Languages

    * Property of STI 

    Page 3 of 23

    CALL-RETURN

    Mechanisms

    Necessary mechanism in connection withthe subprogram call:

     A mechanism for saving the status ofexecution of the program unit which called it(or the caller)

     A mechanism for passing parameters

     A mechanism for allocating and bindingstorage to local variables declared in thesubprogram

     A mechanism for bringing the control to thecalled subprogram (or the callee)

    Necessary mechanism in connection to asubprogram return:

     A mechanism for moving the values of the

    formal parameters outmode  A mechanism for deallocating the storage

    used by the local variables

     A mechanism for restoring the status ofexecution of the caller 

     A mechanism for transferring control backto the caller which invoked the call

  • 8/8/2019 MELJUN CORTES Programming Languages Subprograms

    4/23

    Subprograms

    Programming Languages

    * Property of STI 

    Page 4 of 23

    Recursive Subprogram

     A recursive subprogram is one that callsitself, either directly or by invoking some

    other subprogram that calls it.

    Example:

    class Factorial {

    int fact(int n) {

    int result;if ( n ==1) return 1;

    result = fact (n-1) * n;

    return result;

    }

    }

    class Recursion { public static void main (String args[]) {

    Factorial f =new Factorial();

    System.out.println(“Factorial of 3 is ”

    + f.fact(3));

    System.out.println(“Factorial of 3 is ”+ f.fact(4));

    System.out.println(“Factorial of 3 is ”

    + f.fact(5));}

    }

  • 8/8/2019 MELJUN CORTES Programming Languages Subprograms

    5/23

    Subprograms

    Programming Languages

    * Property of STI 

    Page 5 of 23

    Categories of

    Subprograms

      rocedures perform a specific action orproduces results.

     These provide user-defined parameterizedcomputation statements.

    •  These computations are enacted by single callstatements, in effect the procedures definenew statements

    Produces results via two methods:• If there are variable that are not formal

    parameters but are still visible in both theprocedure and the calling program, theprocedure can change them

    • If the subprogram has formal parameter hatallow the transfer of data to the caller, thoseparameters can be changed.

    Composed of two parts:• Procedure specification 

    • Procedure body 

  • 8/8/2019 MELJUN CORTES Programming Languages Subprograms

    6/23

    Subprograms

    Programming Languages

    * Property of STI 

    Page 6 of 23

    Categories of

    Subprograms

     You write procedure using the syntax PROCEDURE name [(parameter[, parameter,

    ...])] IS[local declarations]

    BEGIN

    executable statements

    [EXCEPTION

    exception handlers]

    END [name]; where parameter stands for the followingsyntax:

     parameter_name [IN | OUT | IN OUT]

    datatype [{:= | DEFAULT} expr]

     You cannot impose the NOT NULL

    constraint on a parameter.  Also, you cannot specify a constraint on

    the datatype. For example, the followingdeclaration of emp_id is illegal:

    PROCEDURE ... (emp_id NUMBER(4)) IS --illegal; should be NUMBER

    BEGIN ... END;

  • 8/8/2019 MELJUN CORTES Programming Languages Subprograms

    7/23

    Subprograms

    Programming Languages

    * Property of STI 

    Page 7 of 23

    Categories of

    Subprograms

      unctions

    compute a value.

     These provide user-defined operators that

    are semantically modeled on mathematicalfunctions.

    • If a function is a faithful model, it produces noside effects.

    • It modifies neither its parameters nor any variables defined outside the function.

    Composed of two parts:

    • Function specification • Function body 

     –  Declarative part 

     –  Executable part 

     –  Exception-handling part 

  • 8/8/2019 MELJUN CORTES Programming Languages Subprograms

    8/23

    Subprograms

    Programming Languages

    * Property of STI 

    Page 8 of 23

    Categories of

    Subprograms

     You write functions using the syntax 

    FUNCTION name [(parameter[, parameter,

    ...])] RETURN datatype IS[local declarations]

    BEGIN

    executable statements

    [EXCEPTION

    exception handlers]

    END [name];

     where parameter stands for the followingsyntax:

     parameter_name [IN | OUT | IN OUT]datatype [{:= | DEFAULT} expr]

    Remember, you cannot impose the NOTNULL constraint on a parameter, and youcannot specify a constraint on thedatatype.

  • 8/8/2019 MELJUN CORTES Programming Languages Subprograms

    9/23

    Subprograms

    Programming Languages

    * Property of STI 

    Page 9 of 23

    Parameters

    Define information that is passed to afunction

     Types:

    Input parameters

    Output parameters

    Input/output parameters

    Categories:

    Formal parameters –  variables are declaredin the procedure or function definition

     Actual parameters – appear in the procedurecall statement or the function reference

    Positional parameters – or plain arguments,are parameters without keyword

    Keyword parameters –  which either actual orformal, are expressions or variable namespreceded by a keyword and an equal sign “=”that identifies which parameter is beingpassed

  • 8/8/2019 MELJUN CORTES Programming Languages Subprograms

    10/23

    Subprograms

    Programming Languages

    * Property of STI 

    Page 10 of 23

    Formal Parameters

     These are dummy variables found in thesubprogram header and used in the

    subprogram.

    Consider the following example:

     public int mult(int x, int y)

    {return x * y;

    }

     The formal parameters for subprogram mult are the integers x and y.

  • 8/8/2019 MELJUN CORTES Programming Languages Subprograms

    11/23

    Subprograms

    Programming Languages

    * Property of STI 

    Page 11 of 23

     Actual Parameters

     These represent a value or address used inthe subprogram call statement.

    Consider the following example:

     public int mult(int x, int y) {

    return x * y;

    }

    // Where the method mult is used 

    int length = 10;

    int width = 5;

    int area = mult(length, width);

     The actual parameters are the integerslength and width.

  • 8/8/2019 MELJUN CORTES Programming Languages Subprograms

    12/23

    Subprograms

    Programming Languages

    * Property of STI 

    Page 12 of 23

    Positional Parameters

     The binding of actual parameters to formalparameters is by position: the first actual

    parameter is bound to the first formalparameter and so forth.

     These parameters are safe and effective.

  • 8/8/2019 MELJUN CORTES Programming Languages Subprograms

    13/23

    Subprograms

    Programming Languages

    * Property of STI 

    Page 13 of 23

    Keyword Parameters

     The name of the formal parameter to whichan actual parameter is to be bound is

    specified with the actual parameter.

     Advantage:

     They can appear in any order in the actualparameter list.

    Disadvantage:

     The user of the subprogram must know thenames of formal parameters.

     The only restriction with this is that after a

    keyword parameter appears in the list, allremaining parameters must be keyworded.

  • 8/8/2019 MELJUN CORTES Programming Languages Subprograms

    14/23

    Subprograms

    Programming Languages

    * Property of STI 

    Page 14 of 23

    Parameter-Passing

    Methods

     Ways in which parameters are transmittedto and/or from called subprograms

    Pass-by-value Pass-by-result 

    Pass-by-value-result 

    Pass-by-reference

    Pass-by-name

    Semantics Models for Formal Parameters

    In Mode

    Out mode

    Inout Mode

  • 8/8/2019 MELJUN CORTES Programming Languages Subprograms

    15/23

    Subprograms

    Programming Languages

    * Property of STI 

    Page 15 of 23

    Pass-by-Value Method

     The value of the actual parameter is usedto initialize the corresponding formal

    parameter.

    It uses In mode semantic.

    It can be implemented using actual data

    transfer or through the use of an accesspath.

    Example in Java:

    int num = 29;

    newNum(num);

    System.out.println(num);

     public void newNum(int num) {

    num = 16;

    }

  • 8/8/2019 MELJUN CORTES Programming Languages Subprograms

    16/23

    Subprograms

    Programming Languages

    * Property of STI 

    Page 16 of 23

    Pass-by-Result Method

    In this method, no value is transferred toor given to the formal parameters of the

    subprogram.

     Thus, the formal parameters become thelocal variables.

    It uses Out mode semantics.

  • 8/8/2019 MELJUN CORTES Programming Languages Subprograms

    17/23

    Subprograms

    Programming Languages

    * Property of STI 

    Page 17 of 23

    Pass-by-Value-Result

    Method

    It is a combination of pass-by value andpass-by-result.

    It uses Inout mode semantics.

    It is sometimes called pass by copy because the actual parameter is copied to

    the formal parameter at subprogram entryand then copied back at subprogramtermination.

  • 8/8/2019 MELJUN CORTES Programming Languages Subprograms

    18/23

    Subprograms

    Programming Languages

    * Property of STI 

    Page 18 of 23

    Pass-by-Reference

    Method

    In this method, an access path or anaddress to the called subprogram is

    transmitted instead of copying data values before and after the called subprogram’sexecution.

    It uses Inout mode semantics.

     Advantage:

     The passing process is efficient in terms oftime and space.

    Disadvantages:

     Access to the formal parameters will beslower than pass-by-value, because ofadditional level of indirect addressing that isrequired.

    Inadvertent and erroneous changes may bemade to the actual parameter.

  • 8/8/2019 MELJUN CORTES Programming Languages Subprograms

    19/23

    Subprograms

    Programming Languages

    * Property of STI 

    Page 19 of 23

    Pass-by-Name Method

     When parameters are passed by name, theactual parameter is, in effect, textually

    substituted for the corresponding formalparameter in all its occurrences in thesubprogram.

     A formal parameter is bound to an accessmethod at the time of the subprogram call,

     but the actual binding to a value or anaddress is delayed until the formalparameter is assigned or referenced.

    It uses Inout mode semantics.

  • 8/8/2019 MELJUN CORTES Programming Languages Subprograms

    20/23

    Subprograms

    Programming Languages

    * Property of STI 

    Page 20 of 23

     Type-Checking

    Parameters

     The type-checking feature of programminglanguages specifically checks whether the

    types of actual parameters are consistentor compatible with the types of thecorresponding formal parameters.

    Consider the following function call:

    ans – EX_FUNC(9.26);

     The actual parameter, or argument, is 9.26 which is floating point number. What if thereceiving formal parameter of EX_FUNC isof integer type, obviously without a featurelike parameter type-checking, no error isdetected by the compiler, an incorrectresult is given by the function andassigned to the variable ans.

  • 8/8/2019 MELJUN CORTES Programming Languages Subprograms

    21/23

    Subprograms

    Programming Languages

    * Property of STI 

    Page 21 of 23

    Overloaded

    Subprograms

     An overloaded subprogram works with adifferent set and/or numbers of

    parameters.

     An overloaded operator is one that hasmultiple meanings and/or operates ondifferent data types.

     The meaning of a particular instance of anoverloaded operator is determined by thetype of its operand.

     An overloaded subprogram is a

    subprogram that has a same name asanother subprogram in the samereferencing environment.

  • 8/8/2019 MELJUN CORTES Programming Languages Subprograms

    22/23

    Subprograms

    Programming Languages

    * Property of STI 

    Page 22 of 23

    Generic Subprograms

    Generic Units

     Ada generics are used to provide thefunctionality of parameters that are

    subprograms; generic part is a subprogram.

    C++ Template Functions

     The terms generic formal object (or simplyformal object), generic formal type (or simplyformal type), and generic formal subprogram

    (or simply subprogram) are used to refer tocorresponding generic formal parameters.

     The only form of subtype indication allowed within a generic formal part is a type mark(that is, the subtype indication must notinclude an explicit constraint).

     The designator of a generic subprogram

    must be an identifier.

    Default expressions for generic formalobjects and default names for formalsubprograms are only evaluated for genericinstantiations that use such defaults.

    Default expressions for parameters of formal

    subprograms are only evaluated for calls ofthe formal subprograms that use suchdefaults.

  • 8/8/2019 MELJUN CORTES Programming Languages Subprograms

    23/23

    S b

    Programming Languages

    * P t f STI

     Accessing NONLOCAL

    Environments

    COMMON Block (FORTRAN)

    External Declarations and Modules (Ada and Modula-2)

    Extern Statement (C)