CSCI 320, Homework 8

Show what will be printed by each program (below) if each of the following parameter passingmechanisms is used. Assume that if a parameter is not initialized by its method, the value in the parameteris garbage.a. VAL pass by valueb. RES pass by resultc. VALRES pass by value-resultd. REF pass by referencee. NAME pass by namea. Consider the following program in the (made-up) programming language Leibniz.main ( ) { // execution starts hereint i, j, k;i := 12; j := 3; k := 0;Subtract(i, j, k);writeln("Difference = ", k);}method Subtract (VAL int minuend; // always a VAL parameterVAL int subtrahend; // always a VAL parameterdifference: integer); // type changes based on problem{difference := minuend - subtrahend;}b. Consider the following program in the (made-up) programming language a[2]; // declarations here are visible by all methods and mainmain ( ) {int i;i := 1;a[1] := 1;a[2] := 1;P(a[i]);writeln(a[1], a[2]);}method P (int x integer){x := x + 2;a[i] := 10;i := 2;x := x + 2;}// Careful: the method has side effects.Notes:• This is a paper and pencil homework – write clearly labeled answers and submit on paper.• This assignment requires that you work alone – you may, of course, read Chapter 9 or lookup definitions of parameter passing mechanisms on the internet – but you may not discussthese particular problems with others.• Submit, on paper, by Friday 10am. Slip under my office door by 10am on Friday (freeing upyour weekend to do the Prolog stuff that Professor Boetje will give you on Wednesday).• If you have questions about the assignment, contact me by email.
Powered by