필터 지우기
필터 지우기

How to use inputs within a function?

조회 수: 2 (최근 30일)
TheSaint
TheSaint 2024년 2월 9일
편집: Stephen23 2024년 2월 9일
function Discriminant (a)
a = input('Enter the value of a: ');
b = a*2
fprintf('The value of b is: %i', b)
end
This is an example of the code I am going to use. I want there to be a prompt for a user to enter a value of a, and in turn, my function will use that value of a in the formula, and print the value of b.
  댓글 수: 3
TheSaint
TheSaint 2024년 2월 9일
I do not see how to use inputs within a function in aforementioned course. Thanks for the input. I already know most of the basics, I am new to functions.
Stephen23
Stephen23 2024년 2월 9일
편집: Stephen23 2024년 2월 9일
Regardless of the fact that you are using functions, have a look at your code:
function Discriminant (a)
a = input('Enter the value of a: ');
You define a somehow (as a function input... but really it does not matter how, all that matters is that it is defined because this basic principal applies to all variables all of the time). The you redefine a to be something else (using INPUT, but again it does not really matter how you do this).
If you want to use the original a that you defined, then don't redefine a to be something else.
INPUT... needs to be unlearned one day.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2024년 2월 9일
a = input('Enter the value of a: ');
Discriminant(a);
function Discriminant (a)
b = a*2
fprintf('The value of b is: %i', b)
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by