Defining and calling functions in MATLAB
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)
Accepted Answer
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 27 Feb 2024
You've combined two unrelated sections of code in the same file. This code that calls your function would be typed in the Command Window or written in some other file, not in the file that contains AreaSphere as its main function. [I'm intentionally omitting discussion of local functions or functions in script for simplicity right now.]
r = randi(10, 3, 1)
Area = AreaSphere(r) % Call the function
The code that defines your function would be in a function file named AreaSphere.m since the main (first) function in the function file should have the same name as the file itself.
function A = AreaSphere(radius) % Define the function
A = 4*pi.*radius.^2;
end
It can be a little difficult to make the distinction here on MATLAB Answers, since usually we type all the code directly in the Answers editor.
Since it seems like you're fairly new to MATLAB, I suggest you start with the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB. The Onramp is more interactive than simply reading through documentation pages, so it might better match your learning style. [Some people learn best by reading, some by hearing, some by watching, and some by doing.]
Stephen23
on 27 Feb 2024
“I am just having a lot of trouble figuring out what is going on and how this works.“
You are overthinking this. Do you know the names of the variables used inside MATLAB function SQRT() ? Answer: no. Does that matter? Answer: no. Can you still call SQRT() with a input of any name? Of course, just like you can with your functions too. Or even no name, simply by writing the input values directly in the function call.
The names of any variables used inside a function definition are totally irrelevant to how you call it. Looking for a relationship that does not exist is just going to confuse you.
Spaceman
on 28 Feb 2024
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}
Spaceman
on 2 Mar 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
Spaceman
on 28 Feb 2024
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.
Spaceman
on 2 Mar 2024
Categories
Find more on Get Started with MATLAB 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!