我不明白参数和参数如何协同工作

我在理解每个函数如何知道某些变量是一个问题。

#include <iostream>
#include "constants.h"

double getTowerHeight()
{

    std::cout << "enter the height of the tower in meters: ";
    double towerHeight{};
    std::cin >> towerHeight;

    return towerHeight;
}

double calculateHeight(double towerHeight, int seconds)
{
    double distanceFallen{ (gravity * (seconds * seconds)) / 2.0 };
    double currentHeight{ towerHeight - distanceFallen };

    return currentHeight;
}

void printHeight(double height, int seconds)
{
    if (height > 0.0)
        std::cout << " At " << seconds << " seconds, the ball is at height: " << height << " meters\n";
    else
        std::cout << " At " << seconds << " seconds, the ball is on the ground\n";

}

void calculateAndPrintHeight(double towerHeight, int seconds)
{

    double height{ calculateHeight(towerHeight, seconds) };
    printHeight(height, seconds);
}

int main()
{

    const double towerHeight{ getTowerHeight() };

    calculateAndPrintHeight(towerHeight, 0);
    calculateAndPrintHeight(towerHeight, 1);
    calculateAndPrintHeight(towerHeight, 2);
    calculateAndPrintHeight(towerHeight, 3);
    calculateAndPrintHeight(towerHeight, 4);
    calculateAndPrintHeight(towerHeight, 5);

    return 0;
}

那么这是如何工作的呢?据我了解,主函数的第一行创建一个const变量“ towerHeight”,并调用一个函数“ getTowerHeight()”。然后调用函数“ calculateAndPrintHeight”,函数的参数创建“ double height”和“ int seconds”,函数如何知道高度,其他函数中的秒为...

评论