Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.ruby Subject: Re: Using functions to return something Date: Tue, 19 Apr 2016 19:19:19 +0200 Lines: 45 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net pzZK0PYg4p/jiwj5ZHijywQBPvD7W5R3Ahem7+iAahqO+N9Zg= Cancel-Lock: sha1:1BwCNMrxFrFOHgaC9i51mHPiYkg= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 In-Reply-To: Xref: csiph.com comp.lang.ruby:7234 On 19.04.2016 16:06, Cai Gengyang wrote: > --- So I managed to write a simple function and use return and puts to display it. > > def add(a, b) > puts "ADDING #{a} + #{b}" > return a + b > end > > age = add(30, 5) > > puts "Age: #{age}" > > Display : > > CaiGengYangs-MacBook-Pro:GoatBoy CaiGengYang$ ruby ex80.rb > ADDING 30 + 5 > Age: 35 > > --- But when I tried to extend it to (A+B)/C, it display error message : > > def add(a, b) > puts "(ADDING #{a} + #{b}) / #{c}" > return (a + b) / c > end > > age = add(30, 10) / 5 > > puts "Age: #{age}" > > Display : > > CaiGengYangs-MacBook-Pro:GoatBoy CaiGengYang$ ruby ex90.rb > ex90.rb:2:in `add': undefined local variable or method `c' for main:Object (NameError) > from ex90.rb:6:in `
'' > > Any smart person over here ? Help lah ... The error message really says it all. robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/