-4

I am currently working on the "half" practice problem in week1 of the CS50x course. I am having trouble reading a couple of lines in the code they provide and was hoping someone could clear up what they did for me.

The Line of code is:

float half(float bill, float tax, int tip);

My first thought was that it's what David spoke about in the abstraction section of the lecture, but I'm not 100%.

For context I will be writing code that will work out a bill amount including tip and tax and then half it.

float half(float bill, float tax, int tip);

int main(void)
{
    float bill_amount = get_float("Bill before tax and tip: ");
    float tax_percent = get_float("Sale Tax Percent: ");
    int tip_percent = get_int("Tip percent: ");

    printf("You will owe $%.2f each!\n", half(bill_amount, tax_percent, tip_percent));
}

// TODO: Complete the function
float half(float bill, float tax, int tip)
  • 1
    Go back and review "forward declaration"... – Fe2O3 Aug 31 '23 at 09:18
  • That specific line is a function *declaration* (also known as a *forward declaration* or *function prototype*). It tells the compiler that somewhere there is a function named `half`, what arguments it takes, and what it might return. Without it, the compiler will not know about that function, and it can't be called (or if using an older version of C, it can be called but the compiler will *guess* about things, and in your case it would guess wrong). – Some programmer dude Aug 31 '23 at 09:18
  • Tip: Move the code of `half()` to the top, _ahead_ of any other (like `main()`) from where the function is _called_. Then, the function **definition** is its own **declaration**... This reduces maintenance points if/when the _function signature_ needs to be changed. (This applies to cases when the function is only used within the same "translation unit", a.k.a. "C source file") – Fe2O3 Aug 31 '23 at 09:22
  • 1
    Another tip: change those `float` variables (and return value) to `double`... In the 1970s, memory was _expensive_ and processing speeds slow. In the 2020s, this is no longer the case (and the compiler will be promoting those to `double` anyway.) – Fe2O3 Aug 31 '23 at 09:33
  • Does this answer your question? [What is the difference between a definition and a declaration?](https://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration) – user16217248 Aug 31 '23 at 22:14

1 Answers1

0

It's a declaration of the function half. The actual definition may succeed the declaration or even be in another file. The declaration tells the compiler about the signature of the function. Having that signature, the compiler can generate code calling that function, even if it doesn't see the definition.

It is the job of the linker to later resolve the references to any called function.

In that particular example, you could just move the definition above its calling site (main), and you won't need a declaration. The definition would serve as a declaration, too.

Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
  • 1
    Thank you! I searched "declaration" in the transcript for week 2s lecture and I can see they go into detail about declarations. So it should be a lot clearer when I get to that video – codedbyjosh Aug 31 '23 at 09:29