# Defining and calling functions in MATLAB

27 views (last 30 days)
Spaceman on 27 Feb 2024
Commented: Spaceman on 2 Mar 2024
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
Invalid expression. Check for missing or extra characters.
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)

Voss on 27 Feb 2024
Refer to the following documentation page for a description and examples of how to define and call a function:
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
X = 3.1416
callFunctionThatUsesXInternally(42)
X % This didn't change even when the function assigned 42 to X inside its workspace
X = 3.1416
function callFunctionThatUsesXInternally(value)
X = value; % This is independent of the variable on the 1st line of code segment
end
Spaceman on 29 Feb 2024
Edited: Spaceman on 2 Mar 2024
Thank you, my lord. This clears things up. I think the main takeaway here is that a function call's workspace is completely independent of what's outside the workspace.

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)
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
Hilarious as you are wise XD

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)
tF = 212
function tFahr = tempC2F(tCelcius)
tFahr=(1.8*tCelcius)+32;
end
##### 3 CommentsShow 1 older commentHide 1 older comment
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
This. I actually finally learned about functions and this explains it perfectly. Thank you. I assume when it gets more complicated with loops and such I will be right back here asking questions, lol.

### Categories

Find more on Entering Commands in Help Center and File Exchange

R2023b

### Community Treasure Hunt

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

Start Hunting!