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
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!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal