Say, you want to pass some information to a Method.
For example, if you want a Method to add two numbers. You need to pass those two numbers to the Method.
Say, in the below example, you want the add Method to add two numbers for you. And you have passed those numbers in between the brackets () of the add Method(i.e. first_number and second_number).
And those elements inside the brackets () of the add Method is called as Parameters and the actual value passed to them is called as Argument.
def add(first_number, second_number) result = first_number + second_number return result end first_num = 5 second_num = 4 value = add(first_num, second_num) puts "The added result is : #{value}"
So, the variables inside the add Method is called as Parameter.
And the actual values (i.e. 5 and 4) to be sent to the Method are the Arguments.
Throughout the tutorial we will be calling the Parameters as Arguments to avoid unnecessary confusion.
Now, what if, you have defined a Method with two Argument but made the Method call with one argument.
Now, what if, you have defined a Method with two Argument but made the Method call with one argument.
def add(first_number, second_number, third_number) result = first_number + second_number + third_number return result end first_num = 5 second_num = 4 value = add(first_num, second_num) puts "The added result is #{value}"
So, in the above code we have the add Method with three Arguments.
def add(first_number, second_number, third_number)
But when the add Method is called, it is called with just two Arguments.
And there is a mismatch of Arguments. And we end up with the below error.
:in add': wrong number of arguments (given 2, expected 3) (ArgumentError)
from add.rb:8:in <main>'
Let us see in the next example, how to fix it.
To solve the problem of Argument mismatch, we can just add an asterisk * in front of the Argument.
Let us see in the below example.
def add(*number) result = number[0] + number[1] return result end first_num = 5 second_num = 4 value = add(first_num, second_num) puts "The added result is #{value}"
So, to avoid the Argument mismatch problem. We have replaced the arguments first_number and second_number with *number.
'def add(*number)'
And no matter how many Arguments you pass, *number will make an intelligent guess and create a Array out of it.
Now, if you see the complete execution, we have initialised the first and second number in the variables, first_num and second_num.
first_num = 5 second_num = 4
And called the add Method, passing above two values(i.e. first_num and second_num) as Arguments.
value = add(first_num, second_num)
And the execution of add Method begins.
def add(*number) result = number[0] + number[1] return result end
So, the arguments first_num and second_num gets assigned to *number.
And internally an Array is created for *number that has both the values of first_num and second_num.
And the variable number is an Array holding the values 5 and 4. And we know an Array can be accessed using indexes.
result = number[0] + number[1]
So, number[0] would access the first element of the Tuple (i.e. 5) and number[1] would access the second element of the Tuple (i.e. 4).
And the added value is returned to the caller.
return result
Say, you want to pass two Arguments to add two numbers. But what if you don't pass any 'Argument' while calling the Method. You would end up with an error.
Let us see in the below example, how can we avoid that error with Default Argument.
def add(first_number = 0, second_number = 0) result = first_number + second_number return result end first_num = 5 second_num = 4 value = add(first_num, second_num) puts "The added result is #{value}" value = add() puts "The added result is #{value}"
So, in the above code, you have initialised the variables, first_num and second_num with 5 and 4.
And called the add Method passing, first_num and second_num as Arguments.
value = add(first_num, second_num)
And the add Method executes, adding first_number and second_number returning the result.
def add(first_number = 0, second_number = 0) result = first_number + second_number return result end
And the added value is printed using the print statement.
puts "The added result is #{value}"
We can see that the arguments, first_number and second_number is initialised with 0, i.e. first_number = 0, second_number = 0. Which is called Default Arguments. It is ignored if Arguments are passed to it. But comes into effect when no values are passed.
Now, in the next Line, we have called the add Method.
value = add()
Without passing any Arguments.
Now, since the Default Arguments is there in place (i.e. first_number = 0, second_number = 0).
def add(first_number = 0, second_number = 0)
Even though no Arguments are passed, the default values first_number = 0 and second_number = 0 are initialised and returned.
And the added value is 0 now.
result = first_number + second_number
And 0 is returned,
return result
Yes, we can pass an Array as Argument.
Let us see with the below example, passing List as Argument.
def add(number_list) result = number_list[0] + number_list[1] + number_list[2] return result end numbers = [5, 7, 8] value = add(numbers) puts "The added result is #{value}"
So, we have created an Array with three values,
numbers = [5, 7, 8]
And we are calling the add Method passing the Array as argument.
value = add(numbers)
And the add Method executes, adding all the elements of the Array, returning the result.
return result
Once again, the brackets are not mandatory in a method. We can declare the below method without Argument.
Let us rewrite the add method without brackets.
def add first_number, second_number result = first_number + second_number return result end first_num = 5 second_num = 4 value = add first_num, second_num puts "The added result is : #{value}"