0
0
Rubyprogramming~15 mins

Explicit return statement in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Explicit return statement
📖 Scenario: You are creating a simple calculator function that adds two numbers. You want to make sure the function clearly returns the result using an explicit return statement.
🎯 Goal: Build a Ruby method called add_numbers that takes two numbers as input and returns their sum using an explicit return statement.
📋 What You'll Learn
Create a method named add_numbers with two parameters: a and b
Inside the method, use an explicit return statement to return the sum of a and b
Call the method with the numbers 5 and 7 and store the result in a variable called result
Print the value of result
💡 Why This Matters
🌍 Real World
Explicit return statements make your code clear and easy to understand, especially when working with functions that perform calculations or data processing.
💼 Career
Many programming jobs require writing clear functions that return values explicitly, which helps with debugging and maintaining code.
Progress0 / 4 steps
1
Create the method skeleton
Write a method named add_numbers that takes two parameters called a and b. Leave the method body empty for now.
Ruby
Need a hint?

Use def to start the method and end to finish it.

2
Add explicit return statement
Inside the add_numbers method, write an explicit return statement that returns the sum of a and b.
Ruby
Need a hint?

Use return a + b to return the sum explicitly.

3
Call the method and store result
Call the add_numbers method with arguments 5 and 7, and store the result in a variable called result.
Ruby
Need a hint?

Assign the method call add_numbers(5, 7) to result.

4
Print the result
Print the value of the variable result to display the sum.
Ruby
Need a hint?

Use puts result to print the sum.