Exercise - Understand method scope
for loops, if-else statements, and methods all represent different types of code blocks. Each code block has its own 'scope'. 'Scope' is the region of a program where certain data is accessible. Variables declared inside a method, or any code block, are only accessible within that region. As programs become more complicated, this pattern helps programmers consistently use clearly named variables and maintain easy to read code.
In this exercise, you'll learn more about method scope by working with different types of methods and variables.
Test variable scope
Statements declared outside of any code block are called top-level statements. Variables declared in top-level statements are called 'global variables'. Global variables aren't restricted to any scope, and can be used anywhere throughout the program. Global variables can be useful for different methods that need to access the same data. However, it's important to pay attention to variable names in different scopes.
In the Visual Studio Code Editor, delete any existing code from the previous exercises.
Enter the following code into the Visual Studio Code Editor:
string[] students = {"Jenna", "Ayesha", "Carlos", "Viktor"}; DisplayStudents(students); DisplayStudents(new string[] {"Robert","Vanya"}); void DisplayStudents(string[] students) { foreach (string student in students) { Console.Write($"{student}, "); } Console.WriteLine(); }In this code, you create a global
studentsarray, and a methodDisplayStudentsthat accepts a parameter with the same name.Save and run the code to observe the following output:
Jenna, Ayesha, Carlos, Viktor, Robert, Vanya,Notice that the method parameter
studenttakes precedence over the globalstudentarray. It's important to be deliberate about what global variables you want your methods to use.Delete the previous code.
Enter the following code into the Editor:
PrintCircleArea(12); void PrintCircleArea(int radius) { double pi = 3.14159; double area = pi * (radius * radius); Console.WriteLine($"Area = {area}"); }This code calculates and displays the area of a circle.
Attempt to reference the variables inside of the
PrintCircleAreamethod by updating your code as follows:PrintCircleArea(12); double circumference = 2 * pi * radius;Error messages appear informing you that the names
piandradiusdon't exist in the current scope. Those variables only exist within thePrintCircleAreamethod scope.Delete the incorrect code and add the following code:
void PrintCircleCircumference(int radius) { double pi = 3.14159; double circumference = 2 * pi * radius; Console.WriteLine($"Circumference = {circumference}"); }Since the variable
piis set to the same fixed value and used in both methods, this value is a good candidate for a global variable. In this example,radiusisn't a global variable so that you can call the methods with different values ofradiuswithout updating a variable each time.Update your code to the following:
double pi = 3.14159; void PrintCircleArea(int radius) { double area = pi * (radius * radius); Console.WriteLine($"Area = {area}"); } void PrintCircleCircumference(int radius) { double circumference = 2 * pi * radius; Console.WriteLine($"Circumference = {circumference}"); }Now both methods can reference the same value of
piwithout needing to define it. You might have already guessed that methods can call other methods. Generally, as long as a method is defined within the scope of your program, it can be called anywhere.Add a new method to your code as follows:
double pi = 3.14159; PrintCircleInfo(12); PrintCircleInfo(24); void PrintCircleInfo(int radius) { Console.WriteLine($"Circle with radius {radius}"); PrintCircleArea(radius); PrintCircleCircumference(radius); }In this code, you create a new method
PrintCircleInfoto call the existing methods. The value ofradiusis also passed down to each method. Creating modularized methods can help keep your code organized and easy to read.Save and run the code to observe the following output:
Circle with radius 12 Area = 452.38896 Circumference = 75.39815999999999 Circle with radius 24 Area = 1809.55584 Circumference = 150.79631999999998
Recap
Here's what you've learned about method scope so far:
- Variables declared inside of a method are only accessible to that method.
- Variables declared in top-level statements are accessible throughout the program.
- Methods don't have access to variables defined within different methods.
- Methods can call other methods.