Control - Procedures and Environments
Control Procedure definition and activation: A procedure is a mechanism in a programming language for abstracting a group of actions or computations. The group of actions is called the body of the procedure. A procedure is represented by a specification including A name, The types and names of parameters. The type of the return value. We shall not make a significant distinction between a function and a procedure, although differences exist. Dr. Sherif G. Aly 2
Control Procedure definition and activation: Formal Example: Parameters //C++ code void intSwap (int &x, int &y) //Specification { int temp = x; //Body x = y; //Body y = temp; //Body } Dr. Sherif G. Aly 3
Control Procedure definition and activation: A procedure is called or activated by stating its name, together with arguments to the call corresponding to the parameters. Example: Actual Parameters intSwap(a, b); Dr. Sherif G. Aly 4
Control Procedure definition and activation: A call to the procedure transfers control to the beginning of the body of the called procedure (The Callee). In some languages, control can be returned to the caller even before reaching the end of the callee ’ s body by using a return statement. Dr. Sherif G. Aly 5
Control Procedure definition and activation: Example: //C++ code void intSwap (int &x, int &y) { If x is equal to y, the function will exit here if (x == y) return; int temp = x; x = y; y = temp; } Dr. Sherif G. Aly 6
Control Procedure definition and activation: In some languages such as FORTRAN, to call a procedure, one must also include the keyword CALL. CALL INTSWAP (A, B) In FORTRAN, procedures are called subroutines. Dr. Sherif G. Aly 7
Control Procedure definition and activation: In some languages, procedure and function declarations are written in a form similar to constant declarations: Example: ML (* ML code *) fun swap (x, y) = let val t = !x in x:= !y y:= t end; Dr. Sherif G. Aly 8
Control Procedure definition and activation: In such ML case, we can say that a procedure declaration creates a constant procedure value. It associates a symbolic name (the name of the procedure) with the value. A procedure communicates with the rest of the program through its: Parameters Non-local references (references to variables outside the procedure body) Dr. Sherif G. Aly 9
Control Procedure Semantics: When a block is encountered during execution, it causes the allocation of local variables and other objects corresponding to the declarations of the block. The memory allocated for the local objects of the block is called the activation record (stack frame). Dr. Sherif G. Aly 10
Control Procedure Semantics: When a block is encountered during execution, it causes the allocation of local variables and other objects corresponding to the declarations of the block. The memory allocated for the local objects of the block is called the activation record (stack frame). Dr. Sherif G. Aly 11
Control Global Environment x x Procedure Semantics: Activation Record of A y int x; void B(void){ int i; i Activation Record of B Using Lexical i = x/2; Scoping, which } x is this? void A(void){ int x, y; B(); } main(){ A(); return 0; } Dr. Sherif G. Aly 12
Control Global Environment x x Procedure Semantics: Activation Record of A y int x; void B(void){ int i; i Activation Record of B Using Lexical i = x/2; Scoping, which } x is this? void A(void){ int x, y; B(); The global environment is called the defining } environment of B The activation record of A is called the calling main(){ environment of B A(); return 0; } Dr. Sherif G. Aly 13
Control Global Environment x x Procedure Semantics: Activation Record of A y int x; void B(void){ int i; i Activation Record of B Using Lexical i = x/2; Scoping, which } x is this? void A(void){ int x, y; B(); The global environment is called the defining } environment of B The activation record of A is called the calling main(){ environment of B A(); return 0; } The x is that of the defining environment : The global x Dr. Sherif G. Aly 14
Control Procedure Semantics: A procedure may have multiple calling environments (could be called from more than one place). However, a procedure will have one defining environment. Dr. Sherif G. Aly 15
Control Parameter Passing Mechanisms: Pass by value. Pass by reference. Pass by value result. Pass by name and delayed evaluation. Dr. Sherif G. Aly 16
Control Parameter Passing Mechanisms: Pass by value: The value of the parameter is evaluated. The parameter is passed as a constant. It ’ s value cannot be modified, or if modified does not affect the actual parameter. This is by far the most common mechanism for parameter passing. In Java, all primitive data types are passed by value. Dr. Sherif G. Aly 17
Control Parameter Passing Mechanisms: Pass by value (C): void increment (int x){ A copy of y is made and x++; passed to x. } Changing x DOES NOT increment (y); change y. Dr. Sherif G. Aly 18
Control Parameter Passing Mechanisms: Pass by reference: The location of the parameter is passed. The formal parameter becomes an alias to the actual parameter. Any change in the formal parameter affects the actual parameter. In FORTRAN, passing by reference is the only allowed parameter passing mechanism. In C++ and Pascal, passing by reference is specified using an extra syntax. Dr. Sherif G. Aly 19
Control Parameter Passing Mechanisms: Pass by reference (C): void increment (int &x){ x becomes an alias of y. x++; Changing x affects y. } The & before x specifies passing by reference. increment (y); Dr. Sherif G. Aly 20
Control Parameter Passing Mechanisms: Pass by reference: If FORTRAN only allows passing by reference. How is a call such as inc(2) achieved? A temporary variable is located, initialized with 2, and passed. This mimics passing by value in a passing by reference mechanism. Dr. Sherif G. Aly 21
Control Parameter Passing Mechanisms: Pass by Value-Result: This mechanism achieves a similar result to passing by reference. No actual alias is established. A copy of the actual parameter is made, used in the procedure, and then copied back to the actual parameter. Also known as copy-in, copy-out. Also known as copy-restore. Dr. Sherif G. Aly 22
Control Parameter Passing Mechanisms: Pass by Name and Delayed Evaluation: The actual parameter passed to the procedure is NOT evaluated until it is actually used in the called procedure. Dr. Sherif G. Aly 23
Control Parameter Passing Mechanisms: Pass by Name and Delayed Evaluation: Example: Void inc(int x) { … x++; a[i] only evaluated here! } If i somehow changed in procedure inc before this point, BIG problem!! We will not be incrementing inc(a[i]); the actually intended a[i] Dr. Sherif G. Aly 24
Control Parameter Passing Specification: Parameter passing specification is different than parameter passing mechanisms. Parameter passing mechanisms are tied closely to the internal mechanics of the code used to implement them. Dr. Sherif G. Aly 25
Control Parameter Passing Specification: In Ada for example, we can specify parameters as In: Cannot be legally assigned a new value inside the procedure, or otherwise have its value changed! More like a constant. Out: Can only be assigned to. Its value can never be used. In Out : Both The meaning of such words is exactly what we can expect. Any programs violating the rules above is considered erroneous! Dr. Sherif G. Aly 26
Control Type Checking of Parameters: In strongly typed languages: Procedure calls must be checked so that the actual parameters agree in type and number with the formal parameters. Procedures may not have a variable number of parameters. Rules must be stated for type compatibility. Dr. Sherif G. Aly 27
Control Procedure Environments, Activations, and Allocation: In block structured language with recursion, such as C and Algol like languages, we need a stack to store various scope information. In stack based runtime environments: An environment pointer is needed to point to the current activation. A control link is needed to point to the previous activation record of the block from which control passed to the current block and to which control will return. Dr. Sherif G. Aly 28
Control Dynamic Memory Management: In an imperative language such as C, the automatic allocation and deallocation of storage occurs only for activation records on the stack. Space is allocated for an activation record on the stack when a procedure is called, and deallocated when the procedure exits. Explicit dynamic allocation and use of pointers is also available under manual programmer control using a heap of memory separate from the stack. Dr. Sherif G. Aly 29
Recommend
More recommend