Break::nofunc Continue::nofunc Return::nofunc
Details
-
- This message is generated when the optional second argument in Break, Continue, or Return does not match an enclosing function call.
- The second argument in Break, Continue, or Return is a label that specifies the enclosing function or loop that will be affected. If this label is omitted, the affected function or loop is determined using built-in heuristics.
- Off[message] switches off the message; On[message] switches it on. For example: Off[Break::nofunc].
Examples
Basic Examples (2)
Return is used with two arguments and there are no enclosing functions:
Return[1, 2]This shows a typical use of Return with two arguments. The second argument is used here to specify that the return should be from the enclosing Module:
f[n_] := Module[{k},
k = 1;
Do[
Print["k = ", k];
If[++k > n, Return[k, Module]],
{1000}
];
1000
]f[3]Without the second argument in Return, the default is to return from the enclosing Do loop, so the return value of the function will be different:
f[n_] := Module[{k},
k = 1;
Do[
Print["k = ", k];
If[++k > n, Return[k]],
{1000}
];
1000
]f[3]This shows an incorrect use of the second argument in Return. The function f does not enclose the Return expression and so cannot be used as a return destination.
f[n_] := Module[{k},
k = 1;
Do[
Print["k = ", k];
If[++k > n, Return[k, f]],
{1000}
];
1000
]f[3]Clear[f]