1. Homepage
  2. Coding
  3. CMPSC461 Fall-2024 Programming Language Concepts - Assignment-4 (Function and Parameter Passing)

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

Get in Touch with Our Experts
PSUCMPSC461Programming Language ConceptsFunction and Parameter PassingPythonJava

CMPSC461 Fall-2024 Programming Language Concepts Assignment Writing Service


Assignment Writing Service

Assignment-4 (Function and Parameter Passing) Assignment Writing Service

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. Assignment Writing Service

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. Assignment Writing Service

**(Kindly refer to the syllabus for late submission and academic integration policies.) Assignment Writing Service

Assignment Specific Instructions: Assignment Writing Service

  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 Assignment Writing Service

Consider the following pseudocode: Assignment Writing Service

x=1, y=3, z=5 function foo(a, b): Assignment Writing Service

x = x + b;
z = x + y;
a = a - z;
y = a - x + b;
Assignment Writing Service

[20 pts] Assignment Writing Service

  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. Assignment Writing Service

(c) foo(y,z) where all parameters are passed by value-result. (d) foo(z,z) where all parameters are passed by reference. Assignment Writing Service

  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] Assignment Writing Service

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. Unlike function calls, macros do not have runtime overhead because they are expanded at compile-time (before actual code execution). Macro expansion works in the following way: Assignment Writing Service

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

No evaluation: The resulting macro body is then textually inserted into the program where the macro was called. Assignment Writing Service

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. Assignment Writing Service

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 {+, , , /}. Assignment Writing Service

\\ Function 1: Print the Square of a Number function square(x) { Assignment Writing Service

1
2
3
4
}
5 \\ Function 2: Print the Value of x 6 function print(x) { Assignment Writing Service

printf(x * x); Assignment Writing Service

7 printf(x); 8} Assignment Writing Service

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. Assignment Writing Service

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). Assignment Writing Service

CMPSC461 Fall-2024 | Assignment-4 (Function and Parameter Passing) 2/5 Assignment Writing Service

Problem 3: Recursion Assignment Writing Service

Consider the following implementation of function sum: Assignment Writing Service

def sum(n):
if n == 0: Assignment Writing Service

return 0 else: Assignment Writing Service

return n + sum(n - 1) Assignment Writing Service

1. Briefly explain why tail-recursive functions are useful. Assignment Writing Service

[3 + 6 = 9 pts] Assignment Writing Service

2. Give a tail-recursive implementation for the function sum. You might need to define a helper function. Assignment Writing Service

CMPSC461 Fall-2024 | Assignment-4 (Function and Parameter Passing) 3/5 Assignment Writing Service

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 { Assignment Writing Service

static class CustomException extends Exception { public CustomException(String message) { Assignment Writing Service

super(message); Assignment Writing Service

public static void main(String[] args) { try { Assignment Writing Service

methodA (); Assignment Writing Service

System.out.println("D"); } catch (CustomException e) { System.out.println("Outer Assignment Writing Service

} Assignment Writing Service

try {
methodB ();
Assignment Writing Service

System.out.println("E"); } catch (CustomException e) { System.out.println("Catch Assignment Writing Service

} } Assignment Writing Service

catch: " + e.getMessage()); Assignment Writing Service

in main for methodB: " + e.getMessage()); Assignment Writing Service


5 6
} 7} Assignment Writing Service

public static void methodA() throws CustomException { try { Assignment Writing Service

methodB (); Assignment Writing Service

System.out.println("C"); } catch (CustomException e) { Assignment Writing Service

} } Assignment Writing Service

public static throw new Assignment Writing Service

} Assignment Writing Service

public static throw new Assignment Writing Service

} } Assignment Writing Service

System.out.println("Catch in methodA for methodB: " + e.getMessage()); Assignment Writing Service

throw Assignment Writing Service

new CustomException("Exception propagated from methodA"); Assignment Writing Service

void methodB() throws CustomException { CustomException("Exception from methodB"); Assignment Writing Service

void methodC() throws CustomException { CustomException("Exception from methodC"); Assignment Writing Service

1. Write down what will be the output by the given program and briefly justify your answer. Assignment Writing Service

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

(a) Catch in methodA for methodB: Exception from methodC via B Assignment Writing Service

(b) Catch in main: Exception propagated from methodA Assignment Writing Service

CMPSC461 Fall-2024 | Assignment-4 (Function and Parameter Passing) 5/5 Assignment Writing Service

  Assignment Writing Service

联系辅导老师!
私密保护
WeChat 微信
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代考,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编程代写,PSU作业答案,CMPSC461作业答案,Programming Language Concepts作业答案,Function and Parameter Passing作业答案,Python作业答案,Java作业答案,