Friday, June 28, 2013

Chapter 9 - Subprograms

Review Questions 1. What are the three general characteristic of subprograms? 2. What does it mean for a subprogram to be active? 5. What languages allow a variable number of parameters ? 7. What is a parameter profile? What is a subprogram protocol? 8. What are formal parameters? What are actual parameters? 9. What are the advantages and disadvantages of the keyword parameter? 10 . What are the differences between a function and a procedure ? 22. What are the design issues for functions? 23. In what ways are coroutines different from conventional subprogram? 24. What is an overloaded subprogram? 26. What is multicast delegate? 32. What exactly is a delegate? 34. What is a closure? Answers: 1. Each subprogram has a single entry point, the calling program unit is suspended during the execution of the called subprogram, which implies that there is only one subprogram in execution at any given time, and control always returns to the caller when the subprogram execution terminates. 2. A subprogram is said to be active if, after having been called, it has begun execution but has not yet completed that execution. 5. C,C++,Perl JavaScript, and Lua. 7. Parameter profile is the number, order, and types of its formal parameters. Subprogram protocol is its parameter profile plus, if it is a function, its return type. In languages in which subprograms have types, those types are defined by the subprogram’s protocol. 8. The parameters in the subprogram header are called formal parameters. Subprogram call statements must include the name of the subprogram and a list of parameters to be bound to the formal parameters of the subprogram. These parameters are called actual parameters. 9. The advantage of keyword parameters is that they can appear in any order in the actual parameter list. The disadvantage to keyword parameters is that the user of the subprogram must know the names of formal parameters. 10. Functions return values while procedure does not, and procedure defines new statements, while function define new user-defined operators. 22. Are side effects allowed and what types of values can be returned. 23. Conventional subprograms are subordinate to their callers. When a routine calls a subprogram, execution suspends at that point in the program and resumes after the subprogram has run to completion. As a result, conventional subprogram invocation is atomic, much like a built-in statement in the programming language. 24. Overloaded subprogram is a subprogram that has the same name as another subprogram in the same referencing environment. 26. Multicast delegate is all of the methods stored in a delegate instance are called in the order in which they were placed in the instance. 32. A delegate is the power and flexibility of method pointers in C# increased by making them objects. 34. Closure is a nested subprogram ant its referencing environment, which together allow the subprogram to be called from anywhere in a program. Problem Set 3. Argue in support of the template functions of C++. How is it different from the template functions in other languages? 6 . Compare and contrast PHP’s parameter passing with that of C#. 8 . Argue against the Java design of not providing operator overloading 12 . Research Jensen’s Device, which was a widely known use of pass-by-name parameters, and write a short description of what it is and how it can be used. 15. How is the problem of passing multidimensional arrays handles by Ada? Answers: 3. C++ templated classes are instantiated to become typed classes at compile time. For example, an instance of the templated Stack class, as well as an instance of the typed class, can be created with the following declaration: Stack myIntStack; However, if an instance of the templated Stack class has already been created for the int type, the typed class need not be created. 6. PHP’s parameter passing is similar to that of C#, except that either the actual parameter or the formal parameter can specify pass-by-reference. Passby- reference is specified by preceding one or both of the parameters with an ampersand. 8. Arithmetic operators are often used for more than one purpose. For example, + usually is used to specify integer addition and floating-point addition. Some languages—Java, for example—also use it for string catenation. This multiple use of an operator is called operator overloading and is generally thought to be acceptable, as long as neither readability nor reliability suffers. 12. Implementing a pass-by-name parameter requires a subprogram to be passed to the called subprogram to evaluate the address or value of the formal parameter. The referencing environment of the passed subprogram must also be passed. This subprogram/referencing environment is a closure. Pass-by-name parameters are both complex to implement and inefficient. They also add significant complexity to the program, thereby lowering its readability and reliability. Because pass-by-name is not part of any widely used language, it is not discussed further here. However, it is used at compile time by the macros in assembly languages and for the generic parameters of the generic subprograms in C++, Java 5.0, and C# 2005. 15. Ada compilers are able to determine the defined size of the dimensions of all arrays that are used as parameters at the time subprograms are compiled. In Ada, unconstrained array types can be formal parameters. An unconstrained array type is one in which the index ranges are not given in the array type definition. Definitions of variables of unconstrained array types must include index ranges. The code in a subprogram that is passed an unconstrained array can obtain the index range information of the actual parameter associated with such parameters.

No comments:

Post a Comment