Question

In: Computer Science

Requirements: In this assignment, you are going to create two switch functions. The first function is...

Requirements: In this assignment, you are going to create two switch functions. The first function is going to be called switchVal and the second is going to be called switchRef. The goal of this assignment is to demonstrate the understanding of what pass-by-reference (Links to an external site.) and pass-by-value (Links to an external site.) do when working with functions that you create. The two functions that you will modify need to accept two parameters and inside them they need to switch the values.  

Getting Started: Create a new folder ('assignment6'). Use the following template to create a PHP web page named switchExercise.php in your assignment5 folder. You will need to modify the two function definitions to do the work of switching the x and y values. You also need to add echo statements in the functions to announce which function was called and the values of x and y before the function exits.  

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        // Retrieve the values from the web page for x and y
        $x = $_POST['x'];
        $y = $_POST['y'];

        function switchVal() {
            
        }

        function switchRef() {
            
        }
        ?>
        <form action="switchExercise.php" method="post">
            $x= <input name="x" style="width: 50px;" type="number" > (enter a number value to use for X)<br/>
            $y= <input name="y" style="width: 50px;" type="number" >  (enter a number value to use for Y)<br/>
            <br/>
            <input type="submit" name="switchVal" value="    Switch By Value    "/><br/>
            <input type="submit" name="switchRef" value="Switch By Reference"/><br/>            
        </form>
        <?php
        if (!empty($x) && !empty($y)) {
            echo "Values before function call <br/>";
            echo "X: $x  Y: $y";
            
            // Write a conditional statement to determine which button was selected        
            // and call the appropriate function
            
            echo "Values after function call <br/>";            
            echo "X: $x  Y: $y";            
        }
        ?>
    </body>
</html>

Solutions

Expert Solution

Source Code :

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>


<?php


// Retrieve the values from the web page for x and y
$x = $_POST['x'];
$y = $_POST['y'];
  
//pass by value function
//when this is used
//only the value inside the function is changed
function switchVal($x,$y) {


echo "<br/>switchVal function is called";
//temp variable is used to store
//a value when swapping or switching
//otherwise value in one variable will be lost
//value of x is stored in $temp
$temp = $x;
//value of $y is stored in $x
$x = $y;
//value of $temp (initial value in $x) is stored in $y.
$y = $temp;
//display the value before the function exit
echo "<br/>values before switchVal function exit <br/>";
echo "X: $x Y: $y";


}

//pass by reference function
//here & is used
//when it is used
//the change in value will also reflect in actual value
//because here the reference is passed
function switchRef(&$x,&$y) {


echo "<br/>switchRef function is called";
//temp variable is used to store
//a value when swapping or switching
//otherwise value in one variable will be lost
//value of x is stored in $temp
$temp = $x;
//value of $y is stored in $x
$x = $y;
//value of $temp (initial value in $x) is stored in $y.
$y = $temp;
//display the value before the function exit
echo "<br/>values before switchRef function exit<br/>";
echo "X: $x Y: $y";


}


?>
<form action="switchExercise.php" method="post">


$x= <input name="x" style="width: 50px;" type="number" > (enter a number value to use for X)<br/>
$y= <input name="y" style="width: 50px;" type="number" > (enter a number value to use for Y)<br/>
<br/>
<input type="submit" name="switchVal" value=" Switch By Value "/><br/>
<input type="submit" name="switchRef" value="Switch By Reference"/><br/>


</form>
<?php


if (!empty($x) && !empty($y)) {


//display value befor function call
echo "Values before function call <br/>";
echo "X: $x Y: $y";
  
// Write a conditional statement to determine which button was selected
// and call the appropriate function
//isset() checks whether a variable is set or not
//if set and not null, it will return true
//checks if switchVal button is clicked
if(isset($_POST['switchVal'])){
switchVal($x,$y);
}
//checks if switchRef button is clicked
if(isset($_POST['switchRef'])){
switchRef($x,$y);
}
//display value after function call
echo "<br/>Values after function call <br/>";
echo "X: $x Y: $y";


}


?>


</body>
</html>

Output and screenshots :


Related Solutions

create a C++ program where you have 2 functions with two parameters. Limit the first function...
create a C++ program where you have 2 functions with two parameters. Limit the first function allowed input to values of numbers from 1-10 and from 5 to 20 for the second function. have each function add their two-parameter together then add the functions final values together. Show error message if wrong input is entered   Ask the user if they wish to continue the program (use loop or decision for this question).
C Programming Language: For this lab, you are going to create two programs. The first program...
C Programming Language: For this lab, you are going to create two programs. The first program (named AsciiToBinary) will read data from an ASCII file and save the data to a new file in a binary format. The second program (named BinaryToAscii) will read data from a binary file and save the data to a new file in ASCII format. Specifications: Both programs will obtain the filenames to be read and written from command line parameters. For example: - bash$...
Your assignment this week has two parts: First, you will create a timeline or infographic to...
Your assignment this week has two parts: First, you will create a timeline or infographic to address the following questions: Research and provide examples or comparisons that demonstrate these changes over time from each of the categories: Print, Radio/Music, Cinema, Television, and the Internet. Include images of each artifact. How have these various forms of communication have evolved since they were first introduced? (Give a “before” and “after”) Second, you will write a mini-statement answering the following question. In 300-500...
Assignment Requirements: 1. Find a workout or create a workout and perform it. If you use...
Assignment Requirements: 1. Find a workout or create a workout and perform it. If you use an online workout/video please reference it so I know what you've done. If you create one yourself, make sure to include your written workout in the assignment. 2. Write about each component/part of the workout and how it related to the health-related components of physical fitness. Answer in detail.
For this assignment, you are going to build upon several skills that you've learned: Create an...
For this assignment, you are going to build upon several skills that you've learned: Create an object that contains objects. Using querySelectorAll to read a nodeList from the DOM. Looping through the nodeList then updating the HTML page. Set up Create the assignment in a "week6" folder with the typical files: index.html css/styles.css js/scripts.js This is the standard structure that we'll use in all assignments. Here is the HTML to use for this assignment. Change the meta author tag to...
create overloaded functions called lastValue. The first function should take as a parameter a string and...
create overloaded functions called lastValue. The first function should take as a parameter a string and the second function should take as a parameter an int. each of you functions should return an int value. in the case of the function that takes the string as an argument you will return the ascii value of the last character in the string. in the case of the function that takes an int parameter your function will return the last digit in...
Create two functions that you can use for a dictionary manager: 1. remove_item(dictionary,key): a function that...
Create two functions that you can use for a dictionary manager: 1. remove_item(dictionary,key): a function that removes the item with the supplied key from the dictionary, if it exits. If the input key doesn’t exist in the dictionary, print out a message saying that the new item has not been removed because there is no matching key in the dictionary. Your function should not produce a Python error if the item does not exist; 2. add_new_item(dictionary,key,value): a function that adds...
How to use a Java program that calls for two functions. The first function is asking...
How to use a Java program that calls for two functions. The first function is asking the user for a two integer numbers, then display the sum of the numbers. The second method is asking the user for two floating point numbers, then display the product of the numbers. Call these methods addNumbers and multiplyNumbers respectively. Call the program AddAndMultiply
create a java class with name ModernArt: For the first part of the assignment, you will...
create a java class with name ModernArt: For the first part of the assignment, you will be extending the JApplet class and creating a work of modern using the Graphics class to draw various shapes to the applet window. As part of your masterpiece, you should incorporate the following elements: At least 1 non-solid rectangle At least 1 non-solid oval At least 1 solid oval At least 1 non-solid Polygon At least 1 solid Polygon At least 3 lines At...
Program Requirements: This assignment requires you to create one program in Java to simulate a Point-of-Sale...
Program Requirements: This assignment requires you to create one program in Java to simulate a Point-of-Sale (POS) system. The solution must be in JAVA language. You create the main program called POSmain.java and two classes: ShoppingCart and CashRegister. Your POSmain program should take three file names from command line arguments. The first file contains a list of products and their prices; the second and third files are lists of items in two shopping carts of two customers. The POSmain program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT