MATLAB Function Syntax

Now I'm trying to learn MATLAB ; I tried a simple step, factorial function.

factorial.m :

 function result = factorial (m) if m == 1 result = m; else result = m .* factorial(m.-1); end 

and then call it like this:

 x = 2; f = factorial (x) 

but all i get is error:

Missing variable or function.

+5
source share
4 answers
  • You have a syntax error, should not be in the second line . after the second m
  • if should be on a separate line from the function declaration.
+3
source

You need one more "end": the first ends the if statement.

0
source

You are an end or if-else statement, but not an end function. Add an extra end to the end.

0
source

if m == 1;

you forgot ";"

0
source

Source: https://habr.com/ru/post/1346675/


All Articles