3

Any idea why the following produces an error?

printf("this is my text\n");

MyFunc;

function MyFunc
  printf("printing from inside function\n");
endfunction

This is the error I get (from the Command Window)

error: 'MyFunc' undefined near line 3, column 3 error: called from function_example at line 3 column 1

Sketch
  • 81
  • 1
  • 6

1 Answers1

3

Okay, I see the issue now. The function definition needs to be placed before the function call is made - as shown below.

printf("this is my text\n");

function MyFunc
  printf("printing from inside function\n");
endfunction

MyFunc;
Sketch
  • 81
  • 1
  • 6
  • 2
    Correct. In particular, note that this is the opposite of matlab. Matlab expects 'inline' definitions at the end of a script. Therefore if you care at all about matlab compatibility you should probably avoid inline function definitions. – Tasos Papastylianou Feb 09 '21 at 12:49