Help with function pass by reference concept
13 views (last 30 days)
Show older comments
I try to turn my c++ coding to matlab. But I cant seem to incorporate pass by reference concept in here.
clc;
clear;
%variable newz is when z has new value when added 1
[x, y, z] = initialize;
[rate, hours] = getHoursRate; %ask user to enter data for rate and hours of working
amount = payCheck ( rate, hours ); %calculate the amount of salary
printCheck (amount,rate,hours); %display output data after salary is calculated
fprintf('The current value of x is currently %f', x); %display current value of x
funcOne ( x, y );
fprintf('The new value of x is %f', x);
newz = nextChar ( z );
fprintf('\n\n The new character of z is %c', newz);
pause;
end
each function definition,
function [x, y, z] = initialize
x = 0;
y = 0;
z = 32;
end
next,
function [ hours, rate ] = getHoursRate
hours = input ('Please enter number of hours worked: ');
rate = input ('Please enter the rate per hour: ');
end
Another one,
function total = payCheck( hours, rate )
if hours <= 40 %if user enter working hours less than 40
total = rate * hours;
else %if user enter working hours more than 40
rate = rate * 1.5;
total = rate * hours;
end
end
Another one,
function printCheck( amount,hours, rate )
fprintf('The hours worked: %d', hours);
fprintf('The rate per hour: %d',rate);
fprintf(' The salary: RM %.2f ', amount);
end
next,
function x=funcOne( x,y )
number = input('Please enter a number: ');
x = 2 * x + y - number;
end
last one
function newz = nextChar( z ) % to calculate the new value of z
newz = z + 1;
end
0 Comments
Answers (2)
Walter Roberson
on 24 Sep 2016
MATLAB does not support pass-by-reference (except that it supports passing some forms of pointers around when calling external libraries.)
Your functions are set up to not need pass-by-reference. The one mistake I could see was
funcOne ( x, y );
which should be
x = funcOne ( x, y );
as funcOne returns the updated value of x.
By the way, I suggest you have another look at your print function. You have
else %if user enter working hours more than 40
rate = rate * 1.5;
total = rate * hours;
Here you are implying that if someone works more than 40 hours, that all of their hours are to be paid at time and a half. Usually it is only the hours exceeding 40 that are to be paid at time and a half with the first 40 usually paid at regular rate.
0 Comments
Image Analyst
on 24 Sep 2016
You can get the effect by passing the same variable names back out, like, in general for 2 arguments:
function [var1, var2] = YourFunctionName(var1, var2);
By doing that, if you change var1 or var2, the new values will be passed out. Be sure to accept them in your calling routine though! Like in your calling routine:
[v1, v2] = YourFunctionName(v1, v2);
If you just do this:
YourFunctionName(v1, v2);
then v1 and v2 will not be changed in your calling routine.
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!