CSCI 3336 Homework #1 (chapters 1-3



CSC 407/507 Homework #3 answer key

1. The idea of nesting subroutines comes closer to the ideology of structured programming – creating your program through top-down design. A process that contains subprocesses can be coded structurally to fit that design more closely. So overall, nested subroutines promotes structured programming and procedural abstraction far better than the C/C++ stand-alone approach. Thus, the ability to nest subroutines will aid in readability. Although it may look more complex and hard to read, the fact that you know that a given subroutine will only be invoked from certain other subroutines gives you additional information that you do not see in a C/C++ program. This in turn should aid the language’s reliability. However, if one attempts to access non-local variables, it damages both readability and reliability. Since the idea of non-local variables is something made more encouraging by nested subroutines (it offers a level between global and local variables), the Pascal-like languages can also be less readable and reliable. Because subroutines cannot be stand-alone entities (as in C/C++), this harms the language’s writability because you can no longer create subroutines that are to be used libraries of functions.

2.

a. C – yes, you specify the optional parameters using … after all of the required parameters. For instance, void foo(int a, …) You then access the optional parameters in the function using built-in functions from the stdarg library. The printf and scanf functions use this approach to allow you to submit as many variables as you want after the specification string, e.g., printf(“%d %d %d %d %d\n”, a, b, c, d, e); vs printf(“hello\n”); or printf(“%d\n”, a);

b. Ada – in a way. You can provide formal parameters with default values. These parameters then do not have to be passed. For instance, you might have a header procedure example(a : in integer; b : in integer := 0). This would set b to automatically be 0 if you did not specify a second parameter. What you cannot do, unlike the other languages listed here, is pass any number of additional parameters (from 0 to millions). Ada only allows you to pass optional parameters up to those listed in the parameter list with their own default values.

c. Java – yes, in one of two ways. First, earlier versions of Java permitted passing any number of parameters in an array of Objects (or Strings). This can be used to simulate optional parameters. You see this in the main method which receives (String[ ] args). With the newest version of Java, you can also pass in optional parameters using the vararg approach. In this case, the notation is (Object… arguments). In fact, this does the same thing in that the optional params are packaged into an array, but it is done for you.

d. Python – yes, it offers both the Ada default parameters as well as the Java passing of arrays (in this case, they are stored in a tuple).

e. Common Lisp – yes, you specify the optional parameters similar to C but instead of …, you use &optional var1 or &rest var1. For &optional, you can list any number of optional parameters. If you use &rest, then there is one parameter and all optional params are packaged up in a list (similar to the Java array or Python tuple). For instance, if you have (defun fun (a b &rest c) …) then c is a list that contains all optional parameters. Like Java’s vararg, when passing to the function, you do not put the values into a list. For instance, fun call be called as (fun 1 2) (a = 1, b = 2, c is nil), (fun 1 2 3) (a = 1, b = 2, c is the list (3)), or (fun 1 2 3 4 5) (a = 1, b = 2, c = (3 4 5).

3.

a. value = 2, list = {1, 3, 5, 7, 9}

value = 2, list = {1, 3, 5, 7, 9}

value = 2, list = {1, 3, 5, 7, 9}

(value and list remain unchanged because only copies were passed)

b. value = 1, list = {2, 3, 5, 7, 9}

value = 1, list = {3, 2, 5, 7, 9}

value = 2, list = {3, 1, 5, 7, 9}

(the function works as we would expect, swapping the two parameter values since we passed pointers)

c. same as b.

d. value = 1, list = {2, 3, 5, 7, 9}

value = 1, list = {3, 2, 5, 7, 9}

value = 2, list = {3, 2, 1, 7, 9}

(notice that the first two calls are exactly the same as from b and c since we passed “constant” names, that is, names that would not change, but in the last call, we passed “list[value]” and so when value changed, it changed which element of list we were going to affect which was list[2] rather than list[0] or list[1]).

4. 5.

My figures are not showing the main methods (my mistake!)

Notice to the right that the static link for A still points to Bigsub, but this ARI for A is currently inaccessible because of the more recent version of A (I indicate this with the dotted line)

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download