Defining and calling functions in MATLAB
355 views (last 30 days)
Show older comments
Given: Recall that Fahrenheit and Celsius are related by the following equation:
Find: Create a function called tempC2F that will accept an input argument of a single temperature in degrees Celsius and return the equivalent temperature in degrees Fahrenheit. I should be able to call your function using the following function call: TF=tempC2F(TC)
My Solution: This is what I have, but I am not getting the desired result, I'm not sure how to call the function to run...
function
tempC2F=(1.8*tC)+32
end
% You can test your function using the following function call. Recall that 212F is the boiling point of water at sea level.
% This corresponds to 100C. If you click "Run", the following code will call your function. You should get an output of tF=212.
% Remember that we are "Calling" the function below. As such, the variable names do not have to match the variables used in the function above.
% Those variables are specific to the function itself.
tC = 100;
tF = tempC2F(tC)
0 Comments
Accepted Answer
Voss
on 27 Feb 2024
Refer to the following documentation page for a description and examples of how to define and call a function:
5 Comments
Steven Lord
on 28 Feb 2024
I think this has to do with the fact that I have to name the function itself 'tempC2F'... Even though it states in my practice example that the variable names do not have to match the variables used in the function.
These are two different things.
In general, the name of the main function in a function file should have the same name as the file. If they don't match, you will need to use the name of the file to call the function, not the name of the first function. So if you wrote the code function tF = tempC2F(tC) in a file named myTemperatureConverter.m you'd need to call it as tF = myTemperatureConverter(100) rather than tF = tempC2F(100).
The second sentence in the part of your message that I quoted deals with variables, not functions. In general (leaving out things like nested functions or global variables) a function call's workspace is completely independent of what's outside the workspace. If your function defines a variable named x, for example, that's not going to affect or be affected by a variable named x in the workspace in which you call your function. The main ways that data flows into or out of a function are through the input and output arguments.
X = pi
callFunctionThatUsesXInternally(42)
X % This didn't change even when the function assigned 42 to X inside its workspace
function callFunctionThatUsesXInternally(value)
X = value; % This is independent of the variable on the 1st line of code segment
end
More Answers (2)
Walter Roberson
on 27 Feb 2024
Create a function called tempC2F
function tempC2F
that will accept an input argument of a single temperature in degrees Celsius
function tempC2f(single_temperature_in_degrees_Celcius)
and return the equivalent temperature in degrees Fahrenheit.
function equivelent_temperature_in_degrees_Fahrenheit = tempC2f(single_temperature_in_degrees_Celcius)
6 Comments
Steven Lord
on 29 Feb 2024
I didn't know it was so obvious I am so new
\begin{Yoda mode}
When for more than 20 years you have been answering questions about MATLAB, novice users will you be able to spot :)
\end{Yoda mode}
Aquatris
on 27 Feb 2024
So if you want to define them in a single script, the function needs to be at the bottom.
And as others mentioned you might want to check out the documentation on how to define functions. But here is your code arrange to work.
tC = 100;
tF = tempC2F(tC)
function tFahr = tempC2F(tCelcius)
tFahr=(1.8*tCelcius)+32;
end
3 Comments
Aquatris
on 28 Feb 2024
Just to make sure there are no confusion, here are a couple things that might be helpful to your understanding.
- The function name is 'tempC2F'
- The function output is 'tFahr'
- 'tFahr' variable only lives within the function. Outside of the function, nobody knows what 'tFahr' is (there might be a completely different variable with the same name and that's why you might want to checkout the topic of 'workspaces' here).
- The syntax matlab uses to define a function is literally how you would use that function from another piece of code with a 'function' attached to the front to define it as a function.
% function definition
function functionOutput = myFunctionName(functionInput)
% whatever the function needs to do,
% in this case it just multiplies the input by 5 and returns that value
functionOutput = 5*functionInput;
end
And to call the function from outside as:
x = myFunctionName(10); % this will make x = 50
- To call the function, the script that holds the function needs to be in your matlab path. Because it was not in your matlab path you got the error 'Unrecognized function or variable 'tempC2F' 'in previous comments.
See Also
Categories
Find more on Logical 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!