![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/176285/image.png)
Hi..i want to write an m-file that takes an integer, computes the factorial and returns the answer..is this correct?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Function f=fact(num) If num<0 f='no factor'; else if num==0 f=1; Else f=1; While num>=1 f=f*num; num=num-1; End End End
0 Commenti
Risposte (1)
Image Analyst
il 27 Dic 2016
No, there are numerous syntax errors there, as the lint in the code editor will point out to you with red lines or red error messages.
For some of them: function, if, while, and else do not have a capital letter at the start of them. And "Else if" is one lower case word "elseif".
The indenting is also messed up. In the MATLAB code editor you can type control a (to select All text) and then control i (to properly indent all the code). To put it here, paste the code here, highlight it, and then (not before) click the {} Code button:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/176285/image.png)
2 Commenti
Image Analyst
il 27 Dic 2016
That's why I told you how to fix it, and the last link I gave basically tells you the same thing, but also has other good info. Give it a try - it's a good thing to learn.
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!