You are on page 1of 45

Procedures /

Subprograms
Procedure/Subprogram meaning

A set of instructions that performs a specific task; a


subroutine or function.

Interaction among procedures and how procedures


manage to pass data among themselves in a
structured and efficient manner.
Kinds of Procedure

Simple call-return
Recursive procedures
Coroutines
Exception handlers
Scheduled Subprograms
Tasks or Concurrent Procedures
Simple Call Return

Simple call-
return Two types of method:
Recursive
procedures
Coroutines Copy rule method
Exception
handlers
Scheduled Memory allocation
Subprograms
Tasks or
Concurrent
Procedures
Simple Call Return
(Copy Rule Method)

Simple call-
return Is replaced by a copy of the body of the procedure
Recursive before execution. Of course, with suitable
procedures
substitutions for parameters and conflicting
Coroutines
indentifiers.
Exception
handlers
Scheduled Example:
Subprograms
FORTRAN
Tasks or
Concurrent COBOL
Procedures
Simple Call Return
(Copy Rule Method)

Simple call-
return
Recursive
procedures
Coroutines
Exception
handlers
Scheduled
Subprograms
Tasks or
Concurrent
Procedures
Simple Call Return
(Copy Rule Method)

Simple call-
return Procedure cannot be recursive
Recursive It will cause the unending copying of program code.
procedures
Coroutines
Explicit call statements are required
Exception
handlers As there are procedures that are executed without an
Scheduled explicit call. Ex. Exception handlers.
Subprograms
Tasks or Procedures must execute completely at each call
Concurrent
Procedures It must be executed completely every time its called.
Simple Call Return
(Copy Rule Method)

Simple call-
return Immediate transfer of control at point of call
Recursive Others types of procedures where execution is
procedures delayed to a later time. Ex. Simula, SIMSCRIPT and
Coroutines GPSS
Exception
handlers
Single execution sequence
Scheduled Code has to be completely executed first before the
Subprograms body corresponding to the next call to a procedure will
Tasks or be executed.
Concurrent
Procedures
Simple Call Return
(Memory Allocation)

Simple call-
return Procedure definition and activation:
Recursive The definition is translated into a template, used to
procedures create an activation each time a subprogram is called.
Coroutines
Exception
Procedure activation: consist of
handlers code segment (the invariant part) - executable code
Scheduled and constants
Subprograms activation record (the dynamic part) - local data,
Tasks or parameters created anew each time the subprogram is
Concurrent called, destroyed when the subprogram returns.
Procedures
Simple Call Return
(Memory Allocation)

Simple call-
return Execution is implemented by the use of two
Recursive system-defined pointers:
procedures
Coroutines
Exception Current-instruction pointer (CIP) address of the next
handlers statement to be executed
Scheduled
Subprograms
Current-environment pointer (CEP) pointer to the
Tasks or
activation record.
Concurrent
Procedures
Simple Call Return
(Memory Allocation)

Simple call-
return On call instruction:
Recursive a) An activation record is created.
procedures
b) Current CIP and CEP are saved in the created
Coroutines
activation record as return point
Exception
handlers
c) CEP is assigned the address of the activation record.
Scheduled d) CIP gets the address of the first instruction in the
Subprograms code segment
Tasks or e) The execution continues from the address in CIP
Concurrent
Procedures
Simple Call Return
(Memory Allocation)

Simple call-
return On return
Recursive a) The old values of CIP and CEP are retrieved .
procedures
b) The execution continues from the address in CIP
Coroutines
Exception
handlers
Scheduled
Subprograms
Tasks or
Concurrent
Procedures
Simple Call Return
(Memory Allocation)

Simple call-
return Procedure call in FORTRAN:
Recursive
procedures
Coroutines
Exception
handlers
Scheduled
Subprograms
Tasks or
Concurrent
Procedures
Simple Call Return
(Memory Allocation)

Simple call-
return Activation records in FORTRAN
Recursive
procedures
Coroutines
Exception
handlers
Scheduled
Subprograms
Tasks or
Concurrent
Procedures
Simple Call Return
(Memory Allocation)

Simple call-
return Stack based implementation
Recursive
procedures
Coroutines Call statement: push CIP and CEP
Exception
handlers
Return statements: pop CIP and CEP off the stack
Scheduled
Subprograms
Tasks or
Concurrent
Procedures
Recursive Procedures

Simple call-
return Types of Recursive:
Recursive
procedures
Coroutines Directly recursive if it can contain a call the itself.
Exception
handlers Indirectly recursive if it calls another procedure that
Scheduled calls the original procedure or that initiates a chain of
Subprograms call that eventually call the original procedure.
Tasks or
Concurrent
Procedures
Recursive Procedures

Simple call-
return Difference between recursive call and ordinary
Recursive call
procedures
Coroutines
Exception Recursive call creates a second activation of the
handlers procedure during the lifetime of the first activation.
Scheduled
Subprograms
Tasks or Ex. LIST, Pascal, ADA, Modula and C
Concurrent
Procedures
Recursive Procedures

Simple call-
return Code segment for a recursive procedure
Recursive
procedures
Coroutines
Exception
handlers
Scheduled
Subprograms
Tasks or
Concurrent
Procedures
Recursive Procedures

Simple call-
return Activation record for a recursive procedure
Recursive
procedures
Coroutines
Exception
handlers
Scheduled
Subprograms
Tasks or
Concurrent
Procedures
Coroutines

Simple call-
return A procedure that may transfer control to another
Recursive procedure even though the whole procedure is
procedures
not completely executed yet.
Coroutines
Exception
handlers The next time control is returned back to the
Scheduled procedure, the execution continues from the first
Subprograms
Tasks or
unexecuted instruction.
Concurrent
Procedures
Coroutines

Simple call-
return Coroutines diagram
Recursive
procedures
Coroutines
Exception
handlers
Scheduled
Subprograms
Tasks or
Concurrent
Procedures
Coroutines

Simple call-
return MODULA-2 example
Recursive New process is created by a call to the procedure:
procedures
Coroutines PROCEDURE NEWPROCESS(P; PROC; A: ADDRESS;
Exception n: CARDINAL; VAR p1: ADDRESS)
handlers
Scheduled
Subprograms Transfer of control between 2 processes:
Tasks or PROCEDURE TRANSFER(VAR p1, p2: ADDRESS)
Concurrent
Procedures
Coroutines

Simple call-
return Activation records for coroutines
Recursive
procedures
Coroutines
Exception
handlers
Scheduled
Subprograms
Tasks or
Concurrent
Procedures
Exception handlers

Simple call-
return Any unusual event, erroneous or not, that is
Recursive detectable either hardware or software and that
procedures
may require special processing.
Coroutines
Exception
handlers
Scheduled
Subprograms
Tasks or
Concurrent
Procedures
Exception handlers

Simple call-
return Terms:
Recursive Exception handling the special processing
procedures
Coroutines
required after detecting an exception
Exception
handlers
Exception handler the processing is usually done
Scheduled
Subprograms using a procedure
Tasks or
Concurrent
Procedures
Exception handlers

Simple call-
return Uses:
Recursive Exception handler are procedures called when
procedures
Coroutines
some events or conditions considered exceptional
Exception becomes true.
handlers Ex.
Scheduled Error condition
Subprograms
Tracing and monitoring during program testing
Tasks or
Concurrent
Procedures
Exception handlers

Simple call-
return Uses:
Recursive Exception handler are not explicitly called, they
procedures
Coroutines
simply take control of execution once the
Exception conditions for their executions become true.
handlers Ex.
Scheduled When the subscript of an array exceeds that is given in
Subprograms
the definition.
Tasks or
Concurrent
When a division by zero occurs.
Procedures
Exception handlers

Simple call-
return Example of Exception handling:
Recursive Try {
procedures
out.write (b);
Coroutines
Exception } catch (IOException e) {
handlers System.out.println(Output Error);
Scheduled } finally {
Subprograms
Tasks or out.close();
Concurrent }
Procedures
Scheduled Subprograms

Simple call-
return These procedures are usually supported in
Recursive simulation languages like SIMSCRIPT, SIMULA and
procedures
GPSS
Coroutines
Exception
handlers Simulation Language is used to describe the
Scheduled operation of a simulation on a computer.
Subprograms
Tasks or
Concurrent
Procedures
Scheduled Subprograms

Simple call-
return Examples:
Recursive 1. To be executed before or after another procedure:
procedures CALL B AFTER A or CALL A BEFORE B
Coroutines 2. To be executed when certain conditions are true:
Exception CALL A WHEN X > Y
handlers
3. On the basis of a simulated time scale:
Scheduled
Subprograms CALL A AT TIME = 10
Tasks or 4. According to priority designation
Concurrent CALL A WITH PRIORITY 1
Procedures
Scheduled Subprograms

Simple call-
return SIMULA Examples:
Recursive
procedures
Coroutines activate P reactivate P
Exception activate P after Q reactivate P after Q
handlers
Scheduled activate P before Q reactivate P before Q
Subprograms activate P delay 10.0 reactivate P delay 10.0
Tasks or
Concurrent activate P at 10.0 reactivate P at 10.0
Procedures
Task or Concurrent Procedures

Simple call-
return Is a procedure that can be executed concurrently
Recursive with another procedure.
procedures
Coroutines
Exception
handlers
Scheduled
Subprograms
Tasks or
Concurrent
Procedures
Task or Concurrent Procedures

Simple call-
Example in Pascal:
return program p; Activation records for
Recursive procedure q; concurrent process:
begin
procedures end;
Coroutines procedure r1;
begin
Exception end;
handlers procedure r2;
begin
Scheduled
end;
Subprograms Begin
Tasks or cobegin
r1; r2;
Concurrent coend
Procedures end
PARAMETERS
PARAMETERS

One way of making available data from one procedure to another is


through parameter transmission.

Formal parameter :
Parameter specified in the procedure declaration
Part where the type and other attributes of the parameter are specified.

Actual parameter:
Is a data object that is shared with the called procedure by the caller.
PARAMETERS

Types of parameters:
Input parameters
Simply supplies values from the caller to the called procedure
Output parameters
Delivers results from the called procedure to the caller.
Input-output parameters
Supplies and delivers results between the caller and the called
procedures.
PARAMETERS

Ex. In Pascal
Call-By-Value procedure a;
Is the simplest and cleanest var x: integer;
procedure b(y: integer);
parameter passing begin
mechanism, both in terms of y := y + 10;
end
its semantics and begin
implementation x := 5;
b(x)
writeln(x);
end
// Output = 5
PARAMETERS

Call-By-Value stack state


Before After
PARAMETERS

Ex. In Pascal
Call-by-Reference procedure a;
Is also called call-by-address var x: integer;
procedure b(var y: integer);
and is implemented in Pascal begin
as var parameter. y := y + 10;
end
begin
x := 5;
b(x)
writeln(x);
end
// Output = 15
PARAMETERS

Call-By-Reference stack state


Before After
PARAMETERS

Ex. In Pascal
Call-by-Name procedure a;
Were introduced during the var x: integer;
procedure b(name y: integer);
time when call-by-reference begin
were not fully understood y := y + 10;
end
yet. begin
x := 5;
b(x)
writeln(x);
end
// Output = 15
PARAMETERS

Call-by-Return Ex. In Pascal


procedure a;
Is the reverse of call-by-value var x: integer;
When a parameter is passed by procedure b(y: integer);
begin
result, no value is transmitted to the
y := 10;
formal parameter by the actual y := y + 10;
parameter. end
If used, should take extra careful begin
x := 5;
not to use the formal parameter
b(x)
before its initialized. writeln(x);
end
// Output = 20
PARAMETERS

Call-by-Return stack state


Entry to procedure b After return from procedure b
PARAMETERS

Call-by-Value-Return
It is a double ended call-by-value since the value of the actual parameter is
stored in a local storage area for the formal parameter at the beginning of
the invocation.
This method is almost similar to call-by-reference.
If you finally execute the program in a paging or segmentation system this
method will be more efficient that call-by-reference.
End of Report

You might also like