bc ne limite-t-il pas la portée d'une variable ?
Définissez la fonction dans la calculatrice de base bc comme
define void f () { test=42; print "all done\n"; }
J'aurais pensé que la valeur de test
serait limitée à la portée de la fonction f
, mais non, test
équivaut à 42 globalement. N'y a-t-il aucun moyen de limiter la portée des variables dans les fonctions bc? C'est-à-dire qu'il existe un moyen de définir des variables locales dans bc?
Solution du problème
Vous devez spécifier n'importe quel AUTO_LIST dans la définition de votre fonction. Du manuel de la Colombie-Britannique,
`define' NAME `(' PARAMETERS `)' `{' NEWLINE
AUTO_LIST STATEMENT_LIST `}'
[...]
The AUTO_LIST is an optional list of variables that are for "local"
use. The syntax of the auto list (if present) is "`auto' NAME,...;".
(The semicolon is optional.) Each NAME is the name of an auto
variable. Arrays may be specified by using the same notation as used
in parameters. These variables have their values pushed onto a stack
at the start of the function. The variables are then initialized to
zero and used throughout the execution of the function. At function
exit, these variables are popped so that the original value (at the
time of the function call) of these variables are restored. The
parameters are really auto variables that are initialized to a value
provided in the function call. Auto variables are different than
traditional local variables because if function A calls function B, B
may access function A's auto variables by just using the same name,
unless function B has called them auto variables. Due to the fact that
auto variables and parameters are pushed onto a stack, `bc' supports
recursive functions.
Donc, pour garder la test
variable locale dans votre fonction, vous utiliseriez
define void f () { local test; test=42; print "all done\n"; }
Commentaires
Enregistrer un commentaire