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

10 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2021-7-1
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"

请先登录,再进行评论。

采纳的回答

Jan
Jan 2021-7-1
编辑:Jan 2021-7-2
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by