Program vs Function?
Show older comments
Hi, I would just like to ask what is the difference between writing a program vs a function? I'm currently working on a few cdf exercises for my course: for example, prob that returns lies within 1% and 9% given that: no of monte carlo simulations: 1,000,000, mu = 6% and sigma = 25%. Now, I'm able to generate the answer by doing either: prob_bet_1and9=sum((return_norm>.01)&(return_norm<.09))/n or prob_bet_1and9F=normcdf(.09,.06,.25)-normcdf(.01,.06,.25) but I'm told this is not a function. How do I make this a function and what's the difference between that and what I'm doing right now? Why is one better than the other? FYI, I'm still a newbie in Matlab so if you can explain it plainly that would be amazing. Thanks!
1 Comment
"Program vs Function?"
I know what a function is, but what is a program? Can you please show a reference for MATLAB.
"How do I make this a function and what's the difference between that and what I'm doing right now?"
Answers (1)
Walter Roberson
on 10 Jun 2018
0 votes
functions are almost always better.
Not mentioned in the page I linked to is the fact that MATLAB can almost always generate more efficient code for a function than it can for a script.
Categories
Find more on Creating and Concatenating Matrices 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!