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)
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).