Function Error: Already declared

I get this error message: Function 'get_portfolio_return' has already been declared within this scope.

This is my function code: function [E_Rp, sigma_Rp] = get_portfolio_return( weight,exp_rets, vols,corr) E_Rp= weight.* exp_rets(1,1)+ (1-weight).*exp_rets(1,2);

sigma_Rp= sqrt((weight.^2.*vols(1).^2) + ((1-weight).^2.*vols(2).^2)+(2.*corr.*(weight)).*(1-weight).*vols(1).*vols(2));

% The stated function defines the necessary variables for subsequent % calculations. Expected Return is calculated as a function of weight and % expected returns. Weight is the weight of Asset1 and exp_rets are the % expected returns of asset 1 and asset 2. What caused this problem/ how do I fix it?
1 Comment
Jeremy on 11 Nov 2019

Direct link to this comment

Cancel Copy to Clipboard

Direct link to this comment

Cancel Copy to Clipboard

Did you save this as a function file separate from your main script file, or did you put it at the bottom of your script as a local function? Did you do both? How are you calling it?

Accepted Answer

Chase Weinberg on 11 Nov 2019

Direct link to this answer

Cancel Copy to Clipboard

Direct link to this answer

Cancel Copy to Clipboard

Okay, so the problem was the function name was different from what I assigned it. I did not realize the name I saved it as has to match the part after function []=

1 Comment
Walter Roberson on 11 Nov 2019

Direct link to this comment

Cancel Copy to Clipboard

Direct link to this comment

Cancel Copy to Clipboard

The rule is that if there is a difference between the file name and the name of the first function in the file, then in order to call the function, you need to use the file name.

More Answers (2)

Muhammad Sinan on 30 Dec 2019

Direct link to this answer

Cancel Copy to Clipboard

Direct link to this answer

Cancel Copy to Clipboard When define user function in MATLAB programming then you will need the following format. function (Dependent Variables1, 2, 3, . ) = FunctionName(Independent Variables1, 2, 3, . ) body of function

so, when you save this file then you have to use this file on the name same as the name of the function. Otherwise, the function will not work.

FunctionName.m
2 Comments
Sian Evans on 5 Jun 2020

Direct link to this comment

Cancel Copy to Clipboard

Direct link to this comment

Cancel Copy to Clipboard

My first function name and the file name are the same yet I still get this error? I don't understand how to bypass it?

Walter Roberson on 5 Jun 2020

Direct link to this comment

Cancel Copy to Clipboard

Direct link to this comment

Cancel Copy to Clipboard What is the first executable word in the file? The very first thing that is not a comment? Sumanth Hegde on 11 Dec 2021

Direct link to this answer

Cancel Copy to Clipboard

Direct link to this answer

Cancel Copy to Clipboard I got this error because I declared the same function name twice.
1 Comment
Yasmeen on 19 Jul 2023

Direct link to this comment

Cancel Copy to Clipboard

Direct link to this comment

Cancel Copy to Clipboard I got it too.

See Also

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.