Theory
Common mistakes
With functions the compiler checks more: whether the function is declared, whether there are as many arguments as needed, whether every path returns a value. A few mistakes are left to you.
Common mistakes
- A function used before it is declared.
implicit declaration of function 'round_to_int': the function is written belowmain. Move it above, or write a prototype abovemain. - Not every path returns a value.
control reaches end of non-void function: e.g. anifwith areturn, but noreturnafter it. A function that isn'tvoidmust return a value in every case. - The wrong number of arguments.
too few arguments to function 'round_to_int'(ortoo many). Below it,note: declared hereshows how many parameters the function has. - Expecting the function to change a variable.
double_it(a)doesn't changea: the parameter is a copy. Return the result:a = double_it(a);. - The result thrown away.
round_to_int(average);with no assignment orprintf: the result is computed and lost. The compiler doesn't warn about it. ()instead of(void).int read_grade()compiles, but thenread_grade(5)would compile too. Write(void).- Recursion without a base case.
exit code -1073741571 (0xC00000FD): the call stack overflowed. Check that every call gets closer to the base case. - An action inside
assert.assert(read_int("How many? ") > 0)disappears in Release mode, together with the question. Only checks go insideassert.