加载中...

习题 19: 函式和变数


函式这个概念也许承载了太多的资讯量。不过别担心,只要坚持做这些练习题,对照上个练习中的检查清单检查这次练习的关联,你最终会明白这些内容的。

有一个你可能没有注意到的细节,我们现在强调一下,函式里面的变量和脚本里面的变量之间是没有连接的。下面的这个练习可以让你对这一点有更多的思考:

  1. def cheese_and_crackers(cheese_count, boxes_of_crackers)
  2. puts "You have #{cheese_count} cheeses!"
  3. puts "You have #{boxes_of_crackers} boxes of crackers!"
  4. puts "Man that's enough for a party!"
  5. puts "Get a blanket."
  6. puts # a blank line
  7. end
  8. puts "We can just give the function numbers directly:"
  9. cheese_and_crackers(20, 30)
  10. puts "OR, we can use variables from our script:"
  11. amount_of_cheese = 10
  12. amount_of_crackers = 50
  13. cheese_and_crackers(amount_of_cheese, amount_of_crackers)
  14. puts "We can even do math inside too:"
  15. cheese_and_crackers(10 + 20, 5 + 6)
  16. puts "And we can combine the two, variables and math:"
  17. cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)

通过这个练习,你看到我们给我们的函式 cheese_and_crackers 很多的参数,然后在函式里把他们印出来。我们可以塞数字、塞变量进去函式,我们甚至可以将变量和数学运算结合在一起。

从一方面来说,函式的参数和我们生成变量时用的 = 赋值符号类似。事实上,如果一个物件你可以用 = 将其命名,你通常也可以将其作为参数传给一个函式。

你应该看到的结果

你应该研究一下脚本的输出,和你想像的结果对比一下看有什么不同。

  1. $ ruby ex19.rb
  2. We can just give the function numbers directly:
  3. You have 20 cheeses!
  4. You have 30 boxes of crackers!
  5. Man that's enough for a party!
  6. Get a blanket.
  7. OR, we can use variables from our script:
  8. You have 10 cheeses!
  9. You have 50 boxes of crackers!
  10. Man that's enough for a party!
  11. Get a blanket.
  12. We can even do math inside too:
  13. You have 30 cheeses!
  14. You have 11 boxes of crackers!
  15. Man that's enough for a party!
  16. Get a blanket.
  17. And we can combine the two, variables and math:
  18. You have 110 cheeses!
  19. You have 1050 boxes of crackers!
  20. Man that's enough for a party!
  21. Get a blanket.
  22. $

加分习题

  1. 倒著将脚本读完,在每一行上面添加一行注解,说明这行程式的作用。
  2. 从最后一行开始,倒著阅读每一行,读出所有重要的符号来。
  3. 自己边写出至少一个函式出来,然后用十种方法运行这个函式。

还没有评论.