Code with multiple functions

Hi there! First off, thank you for taking the time to read/lend a hand with this question. To the long-timers on the forum, I realize you see these kind of questions ALL the time - that said, I'm incredibly grateful for anyone who can help out a beginner trying to teach himself C++ :) Seriously. Thanks.

So as you can see, the code is a simple program to compute the area and circumference of a circle given the radius, straight out of the book. That said, I cannot for the life of me understand how the user-inputted value for Radius (which then goes into the call Area function) translates into the function prototypes at the top. In other words, how does the program compute the value of double Area (double InputRadius), when I've only inputted values for a very different, double Area (double Radius)?

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

const double Pi = 3.14159;

// Function Declarations (Prototypes)
double Area (double InputRadius); 
double Circumference (double InputRadius);

int main () 
{ 
cout << "Enter Radius: "; 
double Radius = 0; 
cin >> Radius;

// Call function "Area" 
cout << "Area is: " << Area (Radius) << endl;

// Call function "Circumference" 
cout << "Circumference is: " << Circumference (Radius) << endl;

return 0;
}

// Function definitions (implementations)
double Area (double InputRadius) 
{ 
return Pi * InputRadius * InputRadius; 
}

double Circumference (double InputRadius) 
{ 
return 2 * Pi * InputRadius; 
}
I cannot for the life of me understand how the user-inputted value for Radius (which then goes into the call Area function) translates into the function prototypes at the top.

A function prototype has nothing to do with the actual calculation. A function prototype tells the compiler what your function looks like (return type, arguments), when the compiler hasn't seen the actual function yet.

If you omitted the function prototypes at lines 7 and 8, the compiler would complain about an attempt to call an undefined function because when it tried to compile lines 17 and 20, it would not know what the signature of your functions were.

With the function prototypes included, when the compiler gets to lines 17 and 20, it know how to generate a call to the respective functions.
ok I think I understand your question. You are talking about the names used to identify the variable. The user inputes a value which is stored into the 'double Radius' variable.

This variable is passed into the function called 'area' on line 17. That function then renames the variable within itself calling it 'InputRadius' but it is still the same variable.
I think renaming the variable is not accurate, but rather it is the value of 'double radius' which is passed into the function and stored as parameter 'double InputRadius'.
Okay, so that's huge to know. Then, the question that follows in my mind is this:

In the main function, where the code specifies the variable Double Radius that the user defines, why name the variable Radius instead of InputRadius? Would that confuse the compiler?
Would that confuse the compiler?

No. Radius has local scope. Only main can see it.

Likewise, in your functions, InputRadius has local scope. It can be seen only within your functions.

When you call your functions, the compiler makes the association between your local variable (Radius) and the argument in the function call.

Perfect, that's exactly what I needed to know. AbstractionAnon, Manga, thank you both once again for your help!
Topic archived. No new replies allowed.