【问题标题】:GNU BC: functions, if-clauses and returnsGNU BC:函数、if 子句和返回
【发布时间】:2009-08-28 09:37:10
【问题描述】:

问题在代码的cmets中:

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?

【问题讨论】:

    标签: bc


    【解决方案1】:

    您的函数仅打印他们计算的内容。他们不返回结果。

    因此,当您调用 f(2) 时,f 将打印 4,当您调用 g(1) 时,g 将打印 3。

    试试这个方法:

    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"
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      • 2016-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多