![](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 view (last 30 days)
Show older comments
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 Comments
Answers (1)
Image Analyst
on 27 Dec 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 Comments
Image Analyst
on 27 Dec 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.
See Also
Categories
Find more on Loops and Conditional Statements 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!