Default argument values
📖 Scenario: Imagine you are creating a simple calculator function in R that adds two numbers. Sometimes, you want to add a number to zero by default if the second number is not given.
🎯 Goal: Build a function called add_numbers that adds two numbers, where the second number has a default value of 0.
📋 What You'll Learn
Create a function named
add_numbers with two parameters: a and bSet the default value of parameter
b to 0Return the sum of
a and bCall the function with one argument and print the result
Call the function with two arguments and print the result
💡 Why This Matters
🌍 Real World
Default argument values are useful in many programs where some inputs are optional or have common defaults, like setting a default tax rate or default color.
💼 Career
Understanding default arguments helps you write flexible and user-friendly functions, a key skill for software development and data analysis.
Progress0 / 4 steps