[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
functions within functions?
From: |
John W. Eaton |
Subject: |
functions within functions? |
Date: |
Fri, 2 Oct 1998 14:21:01 -0500 (CDT) |
On 1-Oct-1998, Ferdinand Schinagl <address@hidden> wrote:
| obviously it is not possible to define
| functions within functions directly, like:
|
| function a(x)
| function y = b(x)
| y = sin(x)
| end
| end
|
| However, as I found out, it can be done by
| evaluating strings (at runtime then, of course):
|
| function a(x)
| str = sprintf("function y = b(x); y = %e*sin(x); end;",x)
| eval(str);
| end
|
| or by means of writing into an external file
| which then contains all the necessary information:
|
| function a(x)
| handle = fopen("b.m","w");
| fprintf(handle,"function y = b(x)\n y = %e*sin(x);\nend",x);
| fclose(handle);
| end
Defining functions within other functions is not intended to work.
The fact that you can (more or less) avoid this restriction by using
eval() or some other means is a misfeature. I certainly wouldn't
depend on it.
Can you explain why you need to do something like this? As far as I
can tell, your example does not show something that requires the
ability to define a function inside another function.
Thanks,
jwe