Local Variables

Not only can you pass in variables to the function, but you also can declare variables within the body of the function. This is done using local variables, so named because they exist only locally within the function itself. When the function returns, the local variables are no longer available.


Local variables are defined like any other variables. The parameters passed in to the function are also considered local variables and can be used exactly as if they had been defined within the body of the function. Listing 5.2 is an example of using parameters and locally defined variables within a function. The output reflects running the program three times, providing different values with each invocation.

Listing 5.2. Demonstrates Use of Local Variables and Parameters
 0:  #include <iostream>
 1:
 2:  float Convert(float);
 3:
 4:  int main()
 5:  {
 6:      float TempFer;
 7:      float TempCel;
 8:
 9:      std::cout << "Please enter the temperature in Fahrenheit: ";
10:      std::cin >> TempFer;
11:      TempCel = Convert(TempFer);
12:      std::cout << "
Here's the temperature in Celsius: ";
13:      std::cout << TempCel << std::endl;
14:      return 0;
15:  }
16:
17:  float Convert(float TempFer)
18:  {
19:      float TempCel;
20:      TempCel = ((TempFer - 32) * 5) / 9;
21:      return TempCel;
22:  }


Please enter the temperature in Fahrenheit: 212
Here's the temperature in Celsius: 100

Please enter the temperature in Fahrenheit: 32
Here's the temperature in Celsius: 0

Please enter the temperature in Fahrenheit: 85
Here's the temperature in Celsius: 29.4444

The output reflects running the program three times, providing the values 212, 32 and 85, respectively. On lines 6 and 7, two float variables are declared, one to hold the temperature in Fahrenheit and one to hold the temperature in degrees Celsius. The user is prompted to enter a Fahrenheit temperature on line 9, and that value is passed to the function Convert().


Execution jumps to the first line of the function Convert() on line 19, where a local variable, also named TempCel, is declared. Note that this local variable is not the same as the variable TempCel on line 7. This variable exists only within the function Convert(). The value passed as a parameter, TempFer, is also just a local copy of the variable passed in by main().

This function could have named the parameter FerTemp and the local variable CelTemp, and the program would work equally well. You can reenter these names and recompile the program to see this work.

The local function variable Tempcel is assigned the value that results from subtracting 32 from the parameter TempFer, multiplying by 5, and then dividing by 9. This value is then returned as the return value of the function, and on line 11 it is assigned to the variable TempCel in the main() function. It is printed on line 13.

The program is run three times. The first time the value 212 is passed in to ensure that the boiling point of water in degrees Fahrenheit (212) generates the correct answer in degrees Celsius (100). The second test is the freezing point of water. The third test is a random number chosen to generate a fractional result.

As an exercise, try reentering the program with other variable names as illustrated here in Listing 5.3:

Listing 5.3. Demonstrates Use of Local Variables and Parameters
 0:  #include <iostream>
 1:
 2:  float Convert(float);
 3:
 4:  int main()
 5:  {
 6:      float TempFer;
 7:      float TempCel;
 8:
 9:      std::cout << "Please enter the temperature in Fahrenheit: ";
10:      std::cin >> TempFer;
11:      TempCel = Convert(TempFer);
12:      std::cout << "
Here's the temperature in Celsius: ";
13:      std::cout << TempCel << std::endl;
14:      return 0;
15:  }
16:
17:  float Convert(float Fer)
18:  {
19:      float Cel;
20:      Cel = ((Fer - 32) * 5) / 9;
21:      return Cel;
22:  }

You should get the same results.

A variable has scope, which determines how long it is available to your program and where it can be accessed. Variables declared within a block are scoped to that block; they can be accessed only within that block and “go out of existence” when that block ends. Global variables have global scope and are available anywhere within your program.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.156.202