You seem to be struggling to connect the mental dots on what the computer is doing when you
- declare variables with an initial value
- define function parameters
- return a value from a function
Not sure how this question will fair with the SO community as the preference is for Q/A that is succinct and reusable (maybe some editing can help) but for your benefit let me explain some of these concepts.
Let's start with a variable declaration
int x = 5;
int y = x;
When you define int x;
it creates a space in RAM for an integer (4 bytes). Adding the = 5
initializes it immediately. It's important that the value on the right side of =
(5 in this case) is known before the computer tries to make space for x.
It's fine to use values that aren't constant for variables like this (notice the second line in the example) but x
has to be known before you declare y
. In other words, this would obviously be a problem:
int y = x;
int x = 5;
For this same reason, the line: double x = towerHeight(x);
is problematic because you're using x
when you call towerHeight
before ever defining x
When you define a function's parameters:
double towerHeight(double x) {
This tells the computer that you are going to copy the value from whatever called towerHeight
to a new place in RAM and call it "x
". This means that the value outside of the function doesn't get modified. Consider the following example:
double towerHeight(double x) {
x = 5;
std::cout << x << std::endl; // outputs 5
}
int main() {
double x = 10;
towerHeight(x);
std::cout << x << std::endl; // outputs 10
return 0;
}
Even though x
was changed in towerHeight
that was a "different copy of x
" which also happened to be called the same name.
When you return a value from a function, in the same manner as passing a function argument, the return value is copied and used in places of the function call. Let's modify the previous example slightly:
double towerHeight(double x) {
x = 5;
return x;
}
int main() {
double x = 10;
x = towerHeight(x); // returns the value "5"
std::cout << x << std::endl; // Outputs "5"
return 0;
}
You can think of towerHeight(x)
being replaced by "5
" so the code would read x = 5;
Conclusion
You should try and use different variable names for
- function arguments (the variables/values you pass to the function)
- function parameters (what they are called inside the function)
to avoid this kind of confusion. Though there may be times where using the same name makes sense (i.e. passing by reference, which is another question). It's important for you to be aware of what's really going on.
Here is what you probably intend to do:
double towerHeight()
{
double height;
std::cout << "Enter a height for the tower" << std::endl;
std::cin >> height;
return height;
}
double secondsSinceDrop()
{
double seconds;
std::cout << "How long has it been since you dropped the ball (Seconds): ";
std::cin >> seconds;
return seconds;
}
double currentBallHeight(double y0, double t)
{
return y0 - (constant::gravity * t * t / 2);
}
void printResult(double y0, double t)
{
double currentHeight = currentBallHeight(y0, t);
if (currentHeight < 0)
std::cout << "At " << t << "s the ball is on the ground." << std::endl;
else
std::cout << "At " << t << "s the ball is at: " << currentHeight << std::endl;
}
int main()
{
double y0 = towerHeight();
double t = secondsSinceDrop();
printResult(y0, t);
return 0;
}
Summarizing what I've changed:
- Renamed
x
to y0
since y(0)
/h(0)
is typically used for "initial height" in physics classes, and similarly y
with t
(though time
would be an even better name).
- Don't pass anything to
towerHeight
or secondsSinceDrop
; you're not trying to give those functions something, you're trying to get something out of them.
- Move the definition of
x
from a function parameter to a local variable defined in the function for towerHeight
and secondsSinceDrop
- Removed the duplicated call to
currentBallHeight
(no need to do the same math twice, it takes time to crunch numbers after all, however small in this case)
- Rewrote for proper usage of
std::cout
and std::endl
- Rewrote the
currentBallHeight
equation to match constant free-fall kinematics (y(t) = y(0) - 0.5g * t^2
) as an added bonus (assuming constant::gravity > 0
)
At some point it will be valuable for you to become aware of the more technical terminology and definitions for the concepts I've outlined here. Here are some recommended readings (just to get you started; keep learning, always):