Hi spoodlz!
Your main problem is how you name those variables.
For example:
int age, age3;
int FatherAge(int age3)
void DisplayFatherAge(int age2)
Why is the name "age3" used both as a variable name and as a input variable name in the function?
It's confusing.And why does the secont function, DisplayFatherAge, have an input variable named age2?Why not age3 just like the first function??
The way you named them really creates confusion.
Where you afraid that name clashes would occur??They never do if they are names for parameters of that function.Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include <iostream>
bool is_it_minor(int age)
{
if ( age<18)
return 1;
else return 0;
}
int main()
{
int age=15;
if ( is_it_minor(age) )
cout<<"Is minor";
else cout<<"Is major";
return 0;
}
| |
The variable int age has nothing to do with the parameter int age from the function.Even if they are named the same, the name only matters outside functions, in case of variables.The name of function parameters is only so you know wich parameter you are accesing inside the function.
For example:
1 2 3 4 5 6
|
int max_number( int, int )
{
if (int < int )
return int;
else return int;
}
| |
The code above obviously is stupid and doesn't even compile, but the idea is that there is no way of knowing wich parameter is wich, because they have no manes.But if you do:
1 2 3 4 5 6
|
int max_number( int number1, int number2)
{
if (number1 < number2 )
return number2;
else return number1;
}
| |
Now you know exactly which parameter you are using.
Anyway, the names of variables and the names of parameters do not clash with each other, so you can use the same name if you want.Even for more functions.
Here's an example of your program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <conio.h>
#include <math.h>
const int NotUsed = system( "color 0a" ); /*change text color to light green
and BG to black*/
/*-------------------------------------------------------*/
/* Function to ask for father's age, and store it in
variable age3. Then return the variable to main(). */
/*-------------------------------------------------------*/
int FatherAge(int age)
{
std::cout << "What is your father's age? ";
std::cin >> age;
return(age);
}
/*-------------------------------------------------------*/
/* Function to pass value of age3 to age2, and print
father's age to screen.*/
/*-------------------------------------------------------*/
void DisplayFatherAge(int age)
{
std::cout << "Your father's age is: "
<< age << '\n';
}
int main()
{
int age, age2; //declare variables to pass
FatherAge(age); //call function asking father's age
std::cout << '\n'; //create a blank to make output look nicer
DisplayFatherAge(age); //call function that prints father's age to screen
return 0;
}
| |
Of course the program still haves the issues reported in the previous answers , but as you can see there is no problem in using the same name.
What about global variables???
There you might have a problem.For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include <iostream>
int age = 15; // a global variable
bool can_vote(int age) // a function
{
if (age >= 18) // oops!!! wich age?? age the global variable or age the parameter ??
return 1;
else return 0;
}
int main()
{
if( can_vote(age) ) // no problem here
cout<<"The person can vote";
else cout<<"The person cannot vote";
return 0;
}
| |
That's why a lot of people say "don't use global variables".
Anyway, try to give clear names to both variables and functions, and yes, "don't use global variables" :) unless you are completely shore there name doesn't conflict whit parameter's names, for the functions that you use.
Good luck!