DOC PREVIEW
UMD CMSC 330 - Homework #3

This preview shows page 1 out of 2 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMSC 330 Homework #3– parameters Fall 20051. For each of the three parameter transition techniques call–by–value, call–by–reference, and call–by–name, give the output of the following program, written using C syntax.#include <stdio.h>int a, b, i;int c[4]= {100, 150, 175, 190};void f(int d, int e, int f) {d= 5;b= a + 3;e= 3;i= 0;f= 20;c[i]= 200;}int main() {a= 10;b= 5;i= 1;f(a, i, c[i]);printf("%d %d %d %d %d %d %d\n", a, b, i, c[0], c[1], c[2], c[3]);return 0;}2. Consider the following code written in OCaml syntax:let value = ref 1;;let f n = value := !value + n ; !value;;let g v w =let x = (f 2) inlet y = v + w inlet z = v * w inx + z - y;;g (f 1) (if !value > 4 then 6 else 5)(a) What output would the program pro duce if parameters were passed by value (as they actually arein OCaml)?(b) What output would the program produce if parameters were passed by need, as they are in Haskell?Recall that in call–by–need an actual parameter is evaluated only once, at the point where it isfirst used in the called procedure.(c) What output would the program produce if parameters were passed by name?13. What results would the following program, written in C syntax, produce if parameters were passed byneed? If C adopted call–by–need as its default parameter passing mechanism, what would the program’sresults be?#include <stdio.h>int func(int a, int b) {if (b == 0)return 0;else return func(a, b);}int main() {printf("%d\n", func(func(1, 1), func(0, 0)));return 0;}4. The following procedure will exchange the values of two integer variables if they are passed by reference.If the parameter transmission mechanism were changed to call–by–name, is there a set of input variableswhose values cannot be exchanged?void swap(x, y: integer);int temp;temp= x;x= y;y= temp}5. Give the least restrictive limits that can be placed on actual parameters so that call–by–name andcall–by–reference always give the same results in all


View Full Document

UMD CMSC 330 - Homework #3

Documents in this Course
Exam #1

Exam #1

6 pages

Quiz #1

Quiz #1

2 pages

Midterm 2

Midterm 2

12 pages

Exam #2

Exam #2

7 pages

Ocaml

Ocaml

7 pages

Parsing

Parsing

38 pages

Threads

Threads

12 pages

Ruby

Ruby

7 pages

Quiz #3

Quiz #3

2 pages

Threads

Threads

7 pages

Quiz #4

Quiz #4

2 pages

Exam #2

Exam #2

6 pages

Exam #1

Exam #1

6 pages

Threads

Threads

34 pages

Quiz #4

Quiz #4

2 pages

Threads

Threads

26 pages

Exam #2

Exam #2

9 pages

Exam #2

Exam #2

6 pages

Load more
Download Homework #3
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Homework #3 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Homework #3 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?