|
| 1 | +--- |
| 2 | +title: More about arguments |
| 3 | +--- |
| 4 | + |
| 5 | +So far we've only looked at functions with a single argument. What if we want to have no parameters, multiple parameters or a variable number? |
| 6 | + |
| 7 | +### Functions with no parameters |
| 8 | + |
| 9 | +It is sometimes useful to have a function with no parameters, often for a task that you need to perform repetitively: |
| 10 | + |
| 11 | +{% highlight ruby %} |
| 12 | +require 'csv' |
| 13 | + |
| 14 | +def load_data |
| 15 | + # read data in from the csv file |
| 16 | + data = CSV.read('tmp/students/data.csv') |
| 17 | + |
| 18 | + # remove the header row |
| 19 | + data[1..-1] |
| 20 | +end |
| 21 | +{% endhighlight %} |
| 22 | + |
| 23 | +When you call a function with no parameters, you don't need to use brackets: |
| 24 | + |
| 25 | +{% highlight ruby %} |
| 26 | + |
| 27 | +load_data #=> [[..], [..], [..], ...] |
| 28 | + |
| 29 | +{% endhighlight %} |
| 30 | + |
| 31 | +<p class='small'>[In fact you don't need brackets when you're calling a function with arguments either. For the time being we'll use them though, to keep things simple.]</p> |
| 32 | + |
| 33 | +### Functions with multiple parameters |
| 34 | + |
| 35 | +Defining and calling a function with multiple parameters is just like defining and calling one with a single parameter: |
| 36 | + |
| 37 | +{% highlight ruby %} |
| 38 | +def insult(word, strength) |
| 39 | + "#{word.capitalize} off" + '!' * strength |
| 40 | +end |
| 41 | + |
| 42 | +insult('goose', 4) #=> "Goose off!!!!" |
| 43 | +{% endhighlight %} |
| 44 | + |
| 45 | +When you call a function with multiple parameters, you need to make sure you provide the right number, otherwise you'll get an error. |
| 46 | + |
| 47 | +{% highlight ruby %} |
| 48 | +insult('badger', 4, 5) |
| 49 | +#=> ArgumentError: wrong number of arguments (3 for 2) |
| 50 | +#=> from (pry):2:in `insult' |
| 51 | +{% endhighlight %} |
| 52 | + |
| 53 | +### Functions with optional parameters |
| 54 | + |
| 55 | +Sometimes it's useful to be able to supply a parameter if you want, but use a sensible default otherwise: |
| 56 | + |
| 57 | +{% highlight ruby %} |
| 58 | +def i_love(thing = 'ruby') |
| 59 | + "I love #{thing}!" |
| 60 | +end |
| 61 | + |
| 62 | +i_love('coding') #=> "I love coding!" |
| 63 | +i_love #=> "I love ruby!" |
| 64 | +{% endhighlight %} |
| 65 | + |
| 66 | +{% exercise %} |
| 67 | +1. Open `more_functions.rb` and begin to work your way throught the functions in the file. |
| 68 | +2. For each function you write, reload the file into irb to try it out: `load 'more_functions.rb'` (run this **inside** `irb`). |
| 69 | +3. For each function you write, run the appropriate test e.g.: `rspec spec/multiply_and_sum_spec.rb` (run this **outside** `irb`, on the command line). |
| 70 | +4. If you get onto the `describe_result` function, you will need to start by writing your own test in `spec/describe_result_spec.rb`. (Do this by copying and pasting from another test, and make the appropriate changes.) |
| 71 | +{% endexercise %} |
| 72 | +{% endexercise %} |
0 commit comments