PROGRAMMING POINTERS, LESSON 6
Syntax/correctness issues
6-1 The data type of the return value of a function must match the data type in the function prototype.
6-2 Forgetting to return a value from a function, which is supposed to return one, will lead to unexpected errors in a program.
6-3 In a function prototype, the parameter must include the data types of values to be passed; identifiers are optional. Also, each function prototype must end with a semicolon.
int doSomething (int x,y); // syntax error, the y needs a data type
int doItRight (int x, int y); // this is acceptable
int P (int, int); // this is also acceptable
6-4 When writing the function definition, do not place a semicolon after the parameter list.
int doItRight (int x, int y); // <--- no semicolon on this line, this will not compile
{
// some code
}
6-5 Do not define a function inside another function. C++ does not allow for nested definitions.
6-6 Function prototypes are required in C++.
Formatting suggestions
6-7 Separate the implementation of functions with blank lines to enhance their readability.
Software engineering
6-8 Function main should consist of calls to other functions which accomplish the work of the program.
6-9 Each function should solve a single, well-defined task. The name of the function should help describe the task of the function.
6-10 Functions should be no longer than one page. If possible, keep functions short. This will make it easier to read and debug your code.
6-11 A function which contains a large number of parameters may be trying to do too much. Consider breaking the function down into smaller functions.
6-12 Global variables should be avoided as they allow side-effects to occur. A side-effect is an unintended change to a global variable from inside a function. Variables which exist during the entire execution of the program should be declared in function main and then passed to other functions through parameters.