I am trying to learn C++ by reading the following manual "C++11 for Programmers"; therefore, I don't have a mentor to put forth my inquires to hence any help that you give is much appreciated. In example #1 attached at the bottom we find a function in which int number1 through number3 are passed into a function, but then are immediately renamed to num1, num2, and num3. I am confused on why the nomenclature is altered why is the variable name not maintained as in code example #2? I have complied both examples and both worked. Thanks for your time.
Example #1:
int maximum(int num1, int num2, int num3) {
int largest;
if (num1 > num2)
largest = num1;
else
largest = num2;
if (num3 > largest)
largest = num3;
return largest;
}
int main()
{
int number1, number2, number3, max;
cout << "Enter the first number ";
cin >> number1;
cout << "Enter the second number ";
cin >> number2;
cout << "Enter the third number ";
cin >> number3;
max = maximum(number1, number2, number3);
cout << "The maximum value is " << max << endl;
return 0;
}
Example #2:
#include <iostream>
using namespace std;
int maximum(int number1, int number2, int number3) {
int largest;
if (number1 > number2)
largest = number1;
else
largest = number2;
if (number3 > largest)
largest = number3;
return largest;
}
int main()
{
int number1, number2, number3, max;
cout << "Enter the first number ";
cin >> number1;
cout << "Enter the second number ";
cin >> number2;
cout << "Enter the third number ";
cin >> number3;
max = maximum(number1, number2, number3);
cout << "The maximum value is " << max << endl;
You likely won't be calling functions exclusively from main(). You might call a function multiple times from different functions, each time in a different context, so it doesn't always make sense to preserve variable names.
Variables that mean one thing in one function may mean different things when passed as arguments to another.
When you pass variables into a function from a different function, the variables passed as argument are copied and put into variables declared in the function's argument so that we can use them within that function. Variables declared in a scope can't be accessed outside it. So the variables in main() can't be accessed from within another function. You need to pass them as arguments to a function. Therefore the variable names are not "changed" but copied into new variables.
#include <iostream>
usingnamespace std;
float add(float a, float b) //This function takes in 2 variables as argument
{ //a and b are for use within this function
float sum_a_b = a+b; //sum_a_b variable can't be accessed from any other function. Its only avail. in sum()
return sum_a_b;
}
int main()
{
float num_1, num_2;
cout << "Enter a number: ";
cin >> num_1;
cout << "Enter another number: ";
cin >> num_2;
float sum = add(num_1, num_2); //We passed num_1 and num_2 as argument to sum() function.
//These are copied into 'a' and 'b' in sum() for use in that function.
cout << "Sum: " << sum << endl;
return 0;
}