How to name variables/constants properly

조회 수: 3 (최근 30일)
Fabian Gock
Fabian Gock 2018년 1월 23일
답변: Fabian Gock 2018년 1월 23일
Is it problematic to name the variables/constants I hand to a function, and the function's inner variables/constants the same?
For instance:
function [f_i_car] = inertial_vehicle(m_car,v_car)
f_i_car = m_car*diff(v_car);
end
The constant for the vehicle mass is saved in the workspace as m_car and the array, containing the speed data is saved with the variable name v_car. I also want the output to be saved in a workspace variable named like the output variable, f_i_car. So now, I call the function as follows:
f_i_car = inertial_vehicle(m_car, v_car)
that means, that there exist global (workspace) and local (within the function) variables with the same name.
Can this cause any problems (e.g. shadowing)?
Thanks in advance for your help! -Fabian

채택된 답변

Fabian Gock
Fabian Gock 2018년 1월 23일
To prevent potential troubles, I now declare the functions and variables as general as possible.
function [f_i] = inertial_force(m,v)
f_i=m*diff(v);
end
And then only give specific names to the workspace variables. I guess, that's the way it should be done.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by