7: lets learn return


#1



I am making a simple add method with return function but seems like its not working
right

help anyone please

Replace this line with your code.


#2

The problem is the way you've written your code makes the ruby interpreter think you just want to merge the two numbers inputted.

Your return should look like return a + b

And your last line should have a space after the string to make it look better like "Your answer is "


#3

I'm sure you've figured this out by now but I am going to post anyway for future users. To get this code to add the numbers and not just put them together, the output needs to look something like this:

output = (number1.to_i + number2.to_i)

The ".to_i" tells Ruby to add the integers together.


#4

I have the same problem. How do I make sure that method is expecting two arguments? If i write
def add (n,y)
return n+y
end
should it work? I tried to call it by putting add(5,6), expecting it to return me 11, but it didn't work.


#5

7. Let's Learn Return

Stuck? Get a hint!

def add(a,b)
return a+b
end
add(1,2)


#6

Sorry, new coder here. I have a quick question, the directions ask for numbers, but why are we putting a+b instead of like 1+2? Thank you for the responses.


#7

That's what is throwing me off to anyone have a answer for that. Good question


#8

after much struggle:

def add(n,y)
return n + y
end

print "2 numbers"
n = gets.to_i
y = gets.to_i

output = add(n,y)
puts "those added =" "#{output}"

the issue is the last line " + output "
my solution here uses "#{output}" to insert the answer to the above equation


#9

I'm not sure if you ever figured this out, but I'll answer it anyway for future reference.

a and b are variables that represent numbers. You can think of variables as a sort of "package" that deliver whatever values (in this case, numbers) you like to the method.

Let's look at this method written a couple of different ways.

def add(a, b)
  return 1 + 2
end

add(5, 6) #returns 3
add(1, 2) #returns 3
add(12, 4) #returns 3
add(457, 92) #returns 3

In this version of the method, we did not use variables in the body of the method (which is the code between the def line and end). We instead used the numbers 1 and 2, so the method will do what it is told -- it will add 1 and 2 and return 3 every time.

Let's look at another version.

def add(a, b)
  return a + b
end

add(5, 6) #returns 11
add(1, 2) #returns 3
add(12, 4) #returns 16
add(457, 92) #returns 549

The variables, a and b, enable the add method to use any numbers when we call it. Here's more of a breakdown of what the computer does as it reads the code, using the example add(5, 6).

  1. Call the method: add(5, 6)
  2. The program looks for a method called add and finds it at def add(a, b).
  3. The program then looks for a and b in the method call, and sees that 5 is in the spot where a is and 6 is in the spot where b is.
  4. It goes through the body of the method and looks for a and b and replaces them with 5 and 6, respectively.
  5. It returns the result based on what you asked it to do with a and b (5 and 6).

This may seem like a lot of steps for something as simple as adding 5 and 6, but it makes sense when you have bigger methods.

I hope that helps! If there are any mistakes, feel free to correct me. :slight_smile:


#10

you made understanding that so much better! I appreciate the time you put out for breaking that down


#11

this is what i've used for this exercise:

def add(x, y)
return x + y
end

puts "Write 2 integer numbers: "
nr1 = gets.chomp.to_i
nr2 = gets.chomp.to_i
puts "the sum of your numbers is: " + add(nr1, nr2).to_s


#12

Ruby methods automatically return last computed value, so Rubyist approach here would be:

def add(a, b)
  a + b
end

#13

Super answer. Thank you so straight forward...


#14

simpler than that !

def add(x, y)
return x + y
end

I thought we had to pass numbers ??? not only theory ?? and I thought that the element after def method name was called “parameter” and the name after calling the def method was called “argument” ??? In explanation exercice one is taken for the other , isn’t it ???
https://www.codecademy.com/courses/learn-ruby/lessons/methods-blocks-sorting/exercises/lets-learn-return?action=lesson_resume