Can you pass same values along a chain of multiple functions?

11 views (last 30 days)
Hi guys, I'm having a hard time understanding this concept in the documentation and I thought I'd ask you guys. If I have one main script that prompts the user to input 3 values (x, y, z), but then I want to pass that information along to function1, then pass the values along to function2, then to function3, and then return the output back to function 2 (function3 does the heavy lifting and computations to give a result).
I know there is the naming-convention of camel case beginning with an uppercase for global and lowercase for local, but why do I sometimes see one function go to the next function and the camel case switches? Should I not be using the same variable name going from main to function3?
Also if in the inputs (x, y, z,) if x is char/str, and y is boolean, and z is integer, is there a certain way I am supposed to be passing? or that shouldnt matter?
Thanks so much!
  1 Comment
Walter Roberson
Walter Roberson on 1 Jul 2021
I know there is the naming-convention of camel case beginning with an uppercase for global and lowercase for local,
Naming conventions are quite local. In larger organizations, it is common for different groups working on the same project to have heated arguments about naming conventions. "Rock Star" programmers have been known to rage-quit projects if the company is not willing to rewrite the entire code base to suit the favoured naming convention of the "Star".
"There are 2 hard problems in computer science: cache invalidation, naming things, and off-by-1 errors. -- Leon Bambrick"

Sign in to comment.

Accepted Answer

Jan
Jan on 1 Jul 2021
Edited: Jan on 2 Jul 2021
The CamelCase or sulkingCamelCase is a convention only, which can help to keep the overview. The Matlab code runs fine without using such conventions.
There is no fixed order to inputs of different type. Just use the order defined in the definitione of the function.
% main script:
x = input('x: ');
y = input('y: ');
z = input('z: ');
[x, y, z] = fcn1(x, y, z);
function [x, y, z] = fcn1(x, y, z)
[x, y, z] = fcn2(x, y, z)
end
function [x, y, z] = fcn2(x, y, z)
x = x * 2;
y = y + 3;
z = z ^ 4;
end
Alternatively:
% main script:
x = input('x: ');
y = input('y: ');
z = input('z: ');
[a, b, c] = fcn1(x, y, z);
function [x, y, z] = fcn1(x1, x2, x3)
[x, y, z] = fcn2(x1, x2, x3)
end
function [u, v, w] = fcn2(Wurstbrot_a, Wurstbrot_b, Wurstbrot_c)
u = Wurstbrot_a * 2;
v = Wurstbrot_b + 3;
w = Wurstbrot_c ^ 4;
end
Both does exactly the same. The names of the variables do not matter.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!