1. Homepage
  2. Homework
  3. CMPSC461 Fall-2024 Programming Language Concepts - Assignment-4 (Function and Parameter Passing)
This question has been solved

CMPSC461 Fall-2024 Programming Language Concepts - Assignment-4 (Function and Parameter Passing)

Engage in a Conversation
PSUCMPSC461Programming Language ConceptsFunction and Parameter PassingPythonJava

CMPSC461 Fall-2024 Programming Language Concepts Instructor: Dr. Suman Saha CourseNana.COM

Assignment-4 (Function and Parameter Passing) CourseNana.COM

General Instructions:
You need to submit your homework to Gradescope. Do every problem on a separate page and mark them before submitting. If the questions are not marked or are sub- mitted with incorrect page-to-question mapping, the question will be deducted partial points. Make sure your name and PSU ID are legible on the first page of your assignment. CourseNana.COM

You are required to submit your assignments in typed format, please follow the latex/- doc template (which can be found on Canvas) for the homework submission. Furthermore, please note that no handwritten submissions (of any form on paper or digital) will be ac- cepted. CourseNana.COM

**(Kindly refer to the syllabus for late submission and academic integration policies.) CourseNana.COM

Assignment Specific Instructions: CourseNana.COM

  1. The sample examples provided in the questions below are just for your reference and do not cover every possible scenario your solution should cover. Therefore, it is advised you think through all the corner cases before finalizing your answer.
  2. Students are expected to answer the questions in a way that shows their understanding of the concepts rather than just mentioning the answers. The rubric does contain partial points to encourage brief conceptual explanations.

Problem 1: Parameter Passing Exercise CourseNana.COM

Consider the following pseudocode: CourseNana.COM

x=1, y=3, z=5 function foo(a, b): CourseNana.COM

x = x + b;
z = x + y;
a = a - z;
y = a - x + b;
CourseNana.COM

[20 pts] CourseNana.COM

  1. 1  [16pts] For each of the cases below, write down the values of x, y, and z after the following calls to foo(). If necessary, assume that output arguments are copied back to parameters in the left-to-right order.

(a) foo(x,y) where all parameters are passed by value. (b) foo(x,z) where all parameters are passed by reference. CourseNana.COM

(c) foo(y,z) where all parameters are passed by value-result. (d) foo(z,z) where all parameters are passed by reference. CourseNana.COM

  1. 2  [4pts] Passed-by-result parameter passing scheme is not applicable for the above pro- gram – justify this statement with proper example.

Problem 2: Parameter Passing II [4 + 4 = 8 pts] CourseNana.COM

Like Call by Name parameter-passing mechanism, Call by Macro Expansion technique also uses a lazy evaluation technique. It is widely used in C, C++, etc. Macro expansion works in the following way: CourseNana.COM

No evaluation: The literal text of each macro argument is substituted for the corre- sponding parameter in the macro’s body. CourseNana.COM

No evaluation: The resulting macro body is then textually inserted into the program where the macro was called. CourseNana.COM

Evaluation: The expanded macro code is executed in the caller’s environment, mean- ing the variables and expressions are evaluated as if they were part of the original calling code. CourseNana.COM

Below are two functions that implement the Call by Macro scheme. Each function accepts a single parameter x, which can either be a numerical value (-1, 2, 0, etc.) or an arithmetic expression consisting of numbers, and the arithmetic operators from set {+, , , /}. CourseNana.COM

\\ Function 1: Print the Square of a Number function square(x) { CourseNana.COM

1
2
3
4
}
5 \\ Function 2: Print the Value of x 6 function print(x) { CourseNana.COM

printf(x * x); CourseNana.COM

7 printf(x); 8} CourseNana.COM

For each function, indicate whether there will be any issues when using the Call by Macro parameter-passing scheme. If issues exist, explain why they occur and suggest how to fix them. Otherwise, explain why there is no issue. CourseNana.COM

Note 1: You should not consider changing the parameter-passing scheme as a potential fix. Note 2: Check related exercises from this link to learn more about macro expansion (col- lected from the optional reading material section of Module Week 6). CourseNana.COM

CMPSC461 Fall-2024 | Assignment-4 (Function and Parameter Passing) 2/5 CourseNana.COM

Problem 3: Recursion CourseNana.COM

Consider the following implementation of function sum: CourseNana.COM

def sum(n):
if n == 0: CourseNana.COM

return 0 else: CourseNana.COM

return n + sum(n - 1) CourseNana.COM

1. Briefly explain why tail-recursive functions are useful. CourseNana.COM

[3 + 6 = 9 pts] CourseNana.COM

2. Give a tail-recursive implementation for the function sum. You might need to define a helper function. CourseNana.COM

CMPSC461 Fall-2024 | Assignment-4 (Function and Parameter Passing) 3/5 CourseNana.COM

Problem 4: Exception Handling [3 + 10 = 13 pts] Consider the following code snippet with exceptions. Note that the program execution starts with main:
public class TryCatch { CourseNana.COM

static class CustomException extends Exception { public CustomException(String message) { CourseNana.COM

super(message); CourseNana.COM

public static void main(String[] args) { try { CourseNana.COM

methodA (); CourseNana.COM

System.out.println("D"); } catch (CustomException e) { System.out.println("Outer CourseNana.COM

} CourseNana.COM

5 6} 7} CourseNana.COM

public static void methodA() throws CustomException { try { CourseNana.COM

methodB (); CourseNana.COM

System.out.println("C"); } catch (CustomException e) { CourseNana.COM

} } CourseNana.COM

public static throw new CourseNana.COM

} CourseNana.COM

public static throw new CourseNana.COM

} } CourseNana.COM

System.out.println("Catch in methodA for methodB: " + e.getMessage()); CourseNana.COM

throw CourseNana.COM

new CustomException("Exception propagated from methodA"); CourseNana.COM

void methodB() throws CustomException { CustomException("Exception from methodB"); CourseNana.COM

void methodC() throws CustomException { CustomException("Exception from methodC"); CourseNana.COM

1. Write down what will be the output by the given program and briefly justify your answer. CourseNana.COM

2. Modify your code in a way that it prints the following messags in order (propagate the exception raised by methodC through methodA): CourseNana.COM

(a) Catch in methodA for methodB: Exception from methodC via B CourseNana.COM

(b) Catch in main: Exception propagated from methodA CourseNana.COM

CMPSC461 Fall-2024 | Assignment-4 (Function and Parameter Passing) 5/5 CourseNana.COM

  CourseNana.COM

Get in Touch with Our Experts

WeChat (微信) WeChat (微信)
Whatsapp WhatsApp
PSU代写,CMPSC461代写,Programming Language Concepts代写,Function and Parameter Passing代写,Python代写,Java代写,PSU代编,CMPSC461代编,Programming Language Concepts代编,Function and Parameter Passing代编,Python代编,Java代编,PSU代考,CMPSC461代考,Programming Language Concepts代考,Function and Parameter Passing代考,Python代考,Java代考,PSUhelp,CMPSC461help,Programming Language Conceptshelp,Function and Parameter Passinghelp,Pythonhelp,Javahelp,PSU作业代写,CMPSC461作业代写,Programming Language Concepts作业代写,Function and Parameter Passing作业代写,Python作业代写,Java作业代写,PSU编程代写,CMPSC461编程代写,Programming Language Concepts编程代写,Function and Parameter Passing编程代写,Python编程代写,Java编程代写,PSUprogramming help,CMPSC461programming help,Programming Language Conceptsprogramming help,Function and Parameter Passingprogramming help,Pythonprogramming help,Javaprogramming help,PSUassignment help,CMPSC461assignment help,Programming Language Conceptsassignment help,Function and Parameter Passingassignment help,Pythonassignment help,Javaassignment help,PSUsolution,CMPSC461solution,Programming Language Conceptssolution,Function and Parameter Passingsolution,Pythonsolution,Javasolution,