Functions with Value Parameters

Hey guys, I hope I'm not being a pain, but when I get stuck trying to understand something, it helps to come here since you guys seem to knock some sense into me haha! The program below is just a simple area calculator. The purpose of writing this is to learn how to program using Value and Reference parameters using functions. Now of course I'm choosing which ever one is easier, but I can't seem to understand how to link the arguments with the parameters. Could anyone clarify how to do this? Is there an example you could give me? Any help would always be appreciated. Thanks.

PS: Another question comes to mind. Instead of typing "using namespace std;" at the top, I see a lot of people who code it into the program. For example:
std:: cout << "Some text" << endl; I'm pretty sure I'm missing something just before the std part, but I was wondering if I need to get out of the habit of writing "using namespace std;" at the top of my programs to indicate that the whole program will be using the standard region.

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
  #include <iostream>
using namespace std;

// Function Prototype
void userInput();
void building_area(int length, int width, float& acres);

int main()
{
    userInput();
    building_area();
    
    cout << "The area is: " << area << endl;
     cout << "\n";
     cout << "The area in acres is: " << acres << endl;
    
    
    system("pause");
    return 0;
}

void userInput()
{
     int length, width;
     
     cout << "Please enter the Length of the building: ";
     cin >> length;
     
     cout << "Please enter the Width of the building: ";
     cin >> width;
     
     building_area(length, width);
}

void building_area(int length, int width, float& acres)
{
     int area = length * width;
     float acres = area / 43560.0;
}
On your first question: You have building_area() defined as taking two integers and a float reference. But when you call the building_area() function in userInput(), you are only passing the two integers.

Also, area is local in scope to the building_area() function.

You then define a local copy of acres in building_area(), over-riding the argument passed in.

Also, in main, you are calling building_area() without ANY arguments!

The fastest way to fix this is to define acres in the userInput() function;
Correct the way you are calling building_area() by passing in acres;
Put your messages in the building_area function;
Take out the call to building_area() in main.

On your second question: When you type using namespace std, you are telling the compiler to reference the functions that are contained in that namespace. That way, you can leave off the scope-resolution operator and save your fingers from having to type so much.
Thanks Kooth. That clears things up a lot more now. By doing what you're suggesting, am I still able to display the results in the main function? That's my end goal.

I must create a function for input, a function for computation which I've done and then I have to be able to output the results in main. I'm trying to rack my head around doing that haha!
Great! To be able to output the results in your main routine, I suggest that you:
make your variables local to the main routine;
Change userInput() to either:
return the calculated area;
or (probably better) add area to the parameter list to building_area (as a reference)
Topic archived. No new replies allowed.