Unit 4


Published on

Published in: Education
1 Comment
  • thank for your great ppt u save me in my mid term exam
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Unit 4

  1. 1. By-Garima Jain
  2. 2.  Scope - Static and Dynamic Block Structures Local Data and Shared Data Parameter and Parameter Transmission Local and Common Environments Tasks and Shared Data
  3. 3. The dynamic scope of an association for an identifier : the set of subprogram activations in which the associationis visible during execution. tied to the dynamic chain of subprogram activations.The static scope of a declaration : the part of the program text where the declared identifieris used.
  4. 4. Dynamic scope rules :relate references with associations for names duringprogram executionStatic scope rules :relate references with declarations of names in theprogram text.
  5. 5. Block-structured languages : • Each program or subprogram is organized as a set of nested blocks. • Each block introduces a new local referencing environment.
  6. 6. Subprogram ADeclaration of XDeclaration of Y Subprogram B Static scope Declaration of Y rules for Declaration of Z block- Use of Y structured programs Use of X Use of Z Hidden to A
  7. 7. Local environment of a subprogram:various identifiers declared in the subprogram : variables, parameters, subprogram names.Static scope rules: implemented by means of a table of thelocal declarationsDynamic scope rules: Retention - Associations and the bound values areretained after execution Deletion - Associations are deleted
  8. 8. By means of a local environment table to associatenames, types and values.Retention: the table is kept as part of the code segmentDeletion: the table is kept as part of the activationrecord, destroyed after each execution.
  9. 9. Mechanisms to exchange dataArguments - data objects sent to a subprogram to beprocessed. Obtained through • parameters • non-local referencesResults - data object or values delivered by thesubprogram. Returned through • parameters, • assignments to non-local variables, or • explicit function values
  10. 10. Parameter: A variable in a procedure that represents some other datafrom the procedure that invoked the given procedure.Parameter transmission: How that information is passed to theprocedure.The parameter is also called the formal argument. The data from theinvoking procedure is called the actual argument or sometimes justthe argument.Usual syntax:Actual arguments: call P(A, B+2, 27+3)Parameters: Procedure P(X, Y, Z)What is connection between the parameters and the arguments?Call by nameCall by referenceCall by valueCall by result (or value-result)
  11. 11. A formal parameter is a particular kind of local data objectwithin a subprogram.It has a name, the declaration specifies its attributes.
  12. 12. An actual parameter is a data object that is shared withthe caller subprogram. a local data object belonging to the caller, a formal parameter of the caller, a non-local data object visible to the caller, a result returned by a function invoked by the callerand immediately transmitted to the called subprogram.
  13. 13. Positional correspondence – pairing actual and formalparameters based on their respective positions in theactual- and formal- parameter listsCorrespondence by explicit name – The name ispaired explicitly by the caller.
  14. 14. Call by name - the actual parameter is substituted in thesubprogramCall by reference – a pointer to the location of the dataobject is made available to the subprogram.The data object does not change position in memory.Call by value – the value of the actual parameter is copiedin the location of the formal parameter.
  15. 15. Call by value-result – same as call by values, however atthe end of execution the result is copied into the actualparameter.Call by result – A parameter transmitted by result is usedonly to transmit a result back from a subprogram. Theinitial value of the actual-parameter data object makes nodifference and cannot be used by the subprogram.
  16. 16. Call by constant value – no change in the valueof the formal parameter is allowed during programexecution.The formal parameter acts as a local constantduring the execution of the subprogram.
  17. 17. Pass by name in Algol Pass by reference in FORTRANprocedure S (el, k); SUNROUTINE S(EL, K) integer el, k; K = 2 begin EL = 0 k:=2; RETURN el := 0 END end;A[1] := A[2] := 1; A(1) = A(2) = 1i := 1; I = 1S(A[i],i); CALL S (A(I), I)
  18. 18. After calling S(A[i], i), the effect is as if theprocedure were i := 2; A[i] := 0;As a result A[2] becomes 0.On exit we havei = 2, A[1] = 1, A[2] = 0.
  19. 19. Since at the time of call i is 1, the formalparameter el is linked to the address of A(1).Thus it is A(1) that becomes 0.On exit we have: i = 2, A(1) = 0, A(2) = 1
  20. 20. Types of parameters: input information output information (the result) both input and outputThe three types can be accomplished by copying or usingpass-by-referenceReturn results: Using parameters Using functions with a return value
  21. 21. Implementing formal parameters: Storage - in the activation record Type: Local data object of type T in case of pass byvalue, pass by value-result, pass by result Local data object of type pointer to T in case ofpass by referenceCall by name implementation: the formal parameters aresubprograms that evaluate the actual parameters.
  22. 22. Associated with the point of call of thesubprogram each actual parameter is evaluated in the referencing environment of the calling program, and a list of pointers is set up.
  23. 23. Associated with the entry and exit in the subprogramon entry: copying the entire contents of the actual parameter in the formal parameter, or copying the pointer to the actual parameteron exit: copying result values into actual parameters or copying function values into registers
  24. 24. Specification: A common environment not local to anysingle subprogram. It may contain: definitions of variables, constants, types. It cannot contain: subprograms, formal parameters.Implementation: as a separate block of memorystorage.
  25. 25. 1. Generate prologue and epilogue code to perform theactions on entry and exit .This code is stored in the segment code part of theactivation record of the subprogram2. Perform the necessary static type checking