pass by value and pass by reference for x and y - pass-by-reference

Given the pseudo code below, what will be the output of the program, when the two parameters x and y are passed by value, and when they are passed by reference?
int x = 1;
int y = 1;
int count = 0;
while count < 3{
addnumbers(x, y);
println (x);
count++;
}
addnumbers(int x, int y){
x = x + y;
println (x);
}

Pass by Value: In pass by value, when you pass a variable into a function, the function will use a copy of the variable, so the original is not affected.
2
1
2
1
2
1
Pass by Reference: In pass by reference, you pass the reference of the variable, so since both int x and int x inside the function reference the same thing, if you change one, the other one changes too.
2
2
3
3
4
4

Related

Returning an object of the same class from an overloaded method operation "plus" rather than struct?

I have a MATLAB class that has a simple overloaded plus function, and I can't get it to return an object. I want the function to add each field value together and output an object of the same class, with the field values being sums of the two inputs. When I add the two objects together, I get a struct, not an object. I am new to classes and am likely doing this wrong; any help would be great.
The code is as follows:
classdef Molar
properties
A = 0;
B = 0;
C = 0;
end
methods
function M = Molar(val)
M.A = val;
M.B = val+1;
M.C = val+2;
end
function M = plus(M1,M2)
M.A = M1.A + M2.A;
M.B = M1.B + M2.B;
M.C = M1.C + M2.C;
end
end
end
When it runs and I do:
>> x = Molar(2)
x =
Molar with properties:
A: 2
B: 3
C: 4
>> y = Molar(3)
y =
Molar with properties:
A: 3
B: 4
C: 5
Then I get a struct when I do the + operation. How can I get this to return another Molar object?
>> x+y
ans =
struct with fields:
A: 5
B: 7
C: 9
I wonder if it has to do with needing to use the constructor method differently?
Any help in this regard would be appreciated.
The first parameter should be the returned value:
function M = plus(M,M2)
M.A = M.A + M2.A;
M.B = M.B + M2.B;
M.C = M.C + M2.C;
end

Matlab recursion

Need a little help understanding what is happening in this function particularly line 7 [Fnm1,Fnm2] = fibrecurmemo(N-1); I don't understand how a new variable can be declared here with in the array. an example of what is happening would be appreciated.
function [Fn,Fnm1] = fibrecurmemo(N)
% Computes the Fibonacci number, F(N), using a memoized recursion
if N <= 2
Fn = 1;
Fnm1 = 1;
else
[Fnm1,Fnm2] = fibrecurmemo(N-1);
Fn = Fnm1 + Fnm2;
end
end
Say we start with:
fibrecurmemo(3) %// N is 3
The else statements run (since N > 2):
[Fnm1,Fnm2] = fibrecurmemo(2); %//statement 1
Fn = Fnm1 + Fnm2; %//statement 2
Before statement 2 can run, fibrecurmemo(2) must first run.
The if statements in fibrecurmemo(2) run (since N <= 2):
Fn = 1;
Fnm1 = 1;
As a result, fibrecurmemo(2) returns 1, 1.
Contininuing from statement 1 above,
[1,1] = fibrecurmemo(2); %//statement 1
Fn = 1 + 1; %//statement 2
Finally,
[2, 1] = fibrecurmemo(3);
The function returns two values.
function [xFive,yFive] = addFive(x,y)
xFive = x + 5;
yFive = y + 5;
end
xx = (addFive(3,4))
xx will be equal to 8 in this example
the syntax for assignment for multiple return values is
[a,b,c,...] = someFunc();
where someFunc() has output of [a,b,c,...]
[aa,bb] = addFive(3,4);
cc = addFive(3,4);
if you do it this way you would get
aa == 8
bb == 9
cc == 8
in the case of cc instead of [aa,bb] Then you will just get the first return value.
i.e. you could do
x = fibrecurmemo(5)
[y,z] = fibrecurmemo(5)
in this case x == y

How to generate two variables at the same time?

I have a struct which has two fields X and Y, I have several keeps on them, and i would like to generate them several times at the same time inside the struct itself.
My need is more complicated but this is the main issue i'm facing.
How can we generate two fields at the same time?
Example:
struct example {
X : int;
Y : int;
keep X < Y;
keep Y < 10;
keep X+Y > 5;
do_something(cnt : int) is {
for i from 1 to cnt {
gen X and Y;
print X, Y;
};
};
};
Since gen me doesn't work, you can just declare a variable of type example in do_something(...) and generate that:
do_something(cnt : int) is {
for i from 1 to cnt {
var obj : example;
gen obj;
X = obj.X;
Y = obj.Y;
print X, Y;
};
};

Counting Number of Specific Outputs of a Function

If I have a matrix and I want to apply a function to each row of the matrix. This function has three possible outputs, either x = 0, x = 1, or x > 0. There's a couple things I'm running into trouble with...
1) The cases that output x = 1 or x > 0 are different and I'm not sure how to differentiate between the two when writing my script.
2) My function isn't counting correctly? I think this might be a problem with how I have my loop set up?
This is what I've come up with. Logically, I feel like this should work (except for the hiccup w/ the first problem I've stated)
[m n] = size(matrix);
a = 0; b = 0; c = 0;
for i = 1 : m
x(i) = function(matrix(m,:));
if x > 0
a = a + 1;
end
if x == 0
b = b + 1;
end
if x == 1
c = c + 1;
end
end
First you probably have an error in line 4. It probably should be i instead of m.
x(i) = function(matrix(i,:));
You can calculate a, b and c out of the loop:
a = sum(x>0);
b = sum(x==0);
c = sum(x==1);
If you want to distinguish x==1 and x>0 then may be with sum(xor(x==1,x>0)).
Also you may have problem with precision error when comparing double values with 0 and 1.

Matlab: link to variable, not variable value

It has been very difficult to use google, MATLAB documentation, I've spent a few hours, and I cannot learn how to
x = 1
y = x
x = 10
y
ans = 10
what happens instead is:
x = 1
y = x
x = 10
y
ans = 1
The value of x is stored into y. But I want to dynamically update the value of y to equal x.
What operation do I use to do this?
Thanks.M
Matlab is 99% a pass-by-value environment, which is what you have just demonstrated. The 1% which is pass-by-reference is handles, either handle graphics (not relevant here) or handle classes, which are pretty close to what you want.
To use a handle class to do what you describe, put the following into a file call RefValue.
classdef RefValue < handle
properties
data = [];
end
end
This creates a "handle" class, with a single property called "data".
Now you can try:
x = RefValue;
x.data = 1;
y = x;
x.data = 10;
disp(y.data) %Displays 10.
you can try something of the following;
x=10;
y='x'
y
y =
x
eval(y)
x =
10
You can also define an implicit handle on x by defining a function on y and referring to it:
x = 1;
y = #(x) x;
y(x) % displays 1
x = 10;
y(x) % displays 10
In MATLAB, this is not possible. However, there are many ways to get similar behavior. For example, you could have an array a = [1, 5, 3, 1] and then index it by x and y. For x = 2, you could assign a(x) = 7, y = x, and once you change a(x) = 4, a(y) == 4.
So indexing may be the fastest way to emulate references, but if you want some elegant solution, you could go through symbolic variables as #natan points out. What's important to take from this is that there are no pointers in MATLAB.