GNU BC: functions, if-clauses and returns
-
20-06-2021 - |
Question
The questions are in the comments of the code:
define f(x) {
print x^2
}
define g(x) {
print x+2
}
if(f(2)>g(1)) {
print "it works"
}
43 # Why 43 instead of the text "it works"?
a=f(2)
b=g(1)
if(a>b) {
print "it works"
}
# Why nothing?
La solution
Your function merely print what they calculate. They don't return the result.
Thus when you call f(2), f will print 4 and when you call g(1), g will print 3.
Try it this way:
define f(x) {
return x^2
}
define g(x) {
return x+2
}
if(f(2)>g(1)) {
print "it works"
}
a=f(2)
b=g(1)
if(a>b) {
print "it works"
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow