Lesson 3
Functions Returning Values - Using Return Statements
Diving into Functions Returning Values

Welcome back! In the previous lesson, you learned how to customize your functions using parameters. In this lesson, we are going to venture into an equally important feature of functions: returning values. This lesson will unlock yet another powerful tool in your Ruby programming toolkit. Let's get started!

What You'll Learn

In this lesson, you'll discover:

  • How to write functions that return values in Ruby.
  • The syntax for using return statements.
  • How to utilize returned values in your code.

Here's a sneak peek at an example:

Ruby
1# Function to calculate total adventure cost 2def calculate_adventure_cost(days, daily_rate) 3 return days * daily_rate 4end 5 6# Using the function to compute a cost 7cost1 = calculate_adventure_cost(10, 100) 8cost2 = calculate_adventure_cost(7, 50) 9puts "The adventures will cost $#{cost1} and $#{cost2}." # Output: The adventures will cost $1000 and $350.

In this example, the function calculate_adventure_cost calculates the total cost of an adventure based on the number of days and the daily rate. The function returns the calculated cost, which is then stored in variables cost1 and cost2 and printed to the console.

This is the explicit way of returning values from functions in Ruby using the return statement. Now let's understand the implicit way of returning values in Ruby functions.

In Ruby, the last expression evaluated in a function is automatically returned. This means that you don't need to use the return keyword explicitly if you want to return the last expression's value. Here's an example to illustrate this:

Ruby
1# Function to add two numbers 2def add(a, b) 3 a + b 4end 5 6add_result = add(5, 3) 7puts "The sum is #{add_result}." # Output: The sum is 8.

Here, the function add takes two arguments a and b and returns their sum. Since the last expression in the function is a + b, it is automatically returned when the function is called. The result is stored in the variable add_result and printed to the console.

Why It Matters

Understanding how to return values from functions is essential for several reasons:

  1. Enhanced Reusability: Functions that return values can be reused in various parts of your code, making your programs more modular and readable.
  2. Cleaner Code: Returning values helps avoid repetitive calculations or operations, thereby making your code cleaner and more efficient.
  3. Better Data Management: Functions that return values can manage and manipulate data more effectively, enabling you to create more complex and powerful programs.

For example, consider the function calculate_adventure_cost from before. Instead of printing the result directly within the function, it returns the calculated value. This allows you to use the result in different ways — store it in a variable, print it, or even pass it to another function.

Exciting stuff, isn't it? With these skills, you'll be well on your way to writing more dynamic and flexible Ruby programs. Let's move on to the practice section and put these concepts into action!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.