Return values
📖 Scenario: Imagine you are creating a simple calculator function in R that adds two numbers and returns the result. This is like using a vending machine: you put in money (input), press a button (function), and get a snack (output). Here, the snack is the sum of two numbers.
🎯 Goal: You will build a function called add_numbers that takes two numbers as input and returns their sum. Then, you will call this function and print the result.
📋 What You'll Learn
Create a function called
add_numbers with two parameters: a and bInside the function, return the sum of
a and bCall the function with the numbers 5 and 7 and store the result in a variable called
resultPrint the value of
result💡 Why This Matters
🌍 Real World
Functions that return values are used everywhere in programming to perform calculations, process data, and give back results. For example, a shopping app might use functions to calculate total prices.
💼 Career
Understanding how to write functions that return values is a key skill for any programming job, enabling you to write reusable and organized code.
Progress0 / 4 steps