You’ve just learned from the preceding chapters how to create and invoke a function as follows:
// Create a function void myFunction() { printf(“I just got executed!”); } int main() { |
A function comprises two components:
void myFunction() { // declaration // the body of the function (definition) } |
For enhanced code optimization, it’s advisable to segregate the declaration and definition of functions. It’s a common practice in C programming to place function declarations above the main() function and function definitions below it. This promotes better code organization and readability:
// Function declaration void myFunction(); // The main method // Function definition |
If we refer back to the example provided in the preceding chapter regarding function parameters and return values:
int myFunction(int x, int y) { return x + y; } int main() { |
Writing it in the following manner is considered good practice:
// Function declaration int myFunction(int, int); // The main method // Function definition |