Variable scope (lexical scoping) in R
📖 Scenario: You are helping a friend understand how variables work inside and outside functions in R. This is important because sometimes variables inside a function can be different from those outside.
🎯 Goal: Build a small R script that shows how variable values change inside a function and how the outside variable stays the same. You will create a variable, write a function that uses a variable with the same name, and then print both to see the difference.
📋 What You'll Learn
Create a variable called
number with the value 10Write a function called
change_number that creates a local variable number with value 5 and returns itCall the function
change_number and save its result in a variable called new_numberPrint both
number and new_number to show their values💡 Why This Matters
🌍 Real World
Understanding variable scope helps avoid bugs when writing functions that use variables with the same names as outside variables.
💼 Career
Many programming jobs require writing clean functions that do not accidentally change important data outside their scope.
Progress0 / 4 steps