Newbie needs help- Local Variables and Functions
I'm learning C++, and at the moment we're learning functions. I can declare and define a function in my application just fine. I can use a local variable and my app just as well, but my question is: what's the difference between a local variable and function? They look almost exactly the same! Here are two examples... perhaps someone can point out what the differences are, and when I should use a local variable instead of a function, and all that jazz!
// Function
typedef unsigned short USHORT;
#include // The message board won't make the iostream.h show up...
USHORT FindArea(USHORT length, USHORT width); // function prototype
int main()
{
USHORT lengthOfYard;
USHORT widthOfYard;
USHORT areaOfYard;
cout << "\nHow wide is your yard? ";
cin >> widthOfYard;
cout << "\nHow long is your yard? ";
cin >> lengthOfYard;
areaOfYard= FindArea(lengthOfYard,widthOfYard);
cout << "\nYour yard is ";
cout << areaOfYard;
cout << " square feet\n\n";
return 0;
}
USHORT FindArea(USHORT l, USHORT w)
{
return l * w;
}
now the local variable-
// this is an example of using local variables with different temperatures.
#include
float Convert(float);
int main()
{
float TempCel;
float TempFer;
cout << "Please enter the temperature in Fahrenheit: ";
cin >> TempFer;
TempCel = Convert(TempFer);
cout << "\nHere's the temperature in Celsius: ";
cout << TempCel << endl;
return 0;
}
float Convert(float TempFer)
{
float TempCel;
TempCel = ((TempFer - 32) * 5) /9;
return TempCel;
}
Thanks in advance!!
Edited by - Fredric on 1/30/00 9:34:37 AM
3D Math- The type of mathematics that'll put hair on your chest!
A local variable and a function are completely different a local variable is like int a. A function does certain tasks like add two numbers together.
A local variable is a variable that cant be acessed by other parts of the program except that section is declared in for example
for(int a = 0; a < 3; a++
{
// local variable
int index++
cout << index;
}
// if you try to acess index here it wont work
// this would be wrong
cout << index
A function is like this
int addxy(int x, int y)
{
return(x + y);
}
this adds two numbers together. Hope it helps, later.
Edited by - Chris F on 1/30/00 10:11:07 AM
Edited by - Chris F on 1/30/00 10:11:57 AM
A local variable is a variable that cant be acessed by other parts of the program except that section is declared in for example
for(int a = 0; a < 3; a++
{
// local variable
int index++
cout << index;
}
// if you try to acess index here it wont work
// this would be wrong
cout << index
A function is like this
int addxy(int x, int y)
{
return(x + y);
}
this adds two numbers together. Hope it helps, later.
Edited by - Chris F on 1/30/00 10:11:07 AM
Edited by - Chris F on 1/30/00 10:11:57 AM
"I have realised that maths can explain everything. How it can is unimportant, I want to know why." -Me
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement