Function Definition and Calling
A function is a named block of code that groups a set of operations together. Defining and calling functions improves code reusability and readability. In C, if you use a function before its definition, you need to declare it first using a "function prototype."
Syntax
// Function prototype declaration (forward declaration).
return_type function_name(param_type1, param_type2, ...);
// Function definition.
return_type function_name(param_type1 param1, param_type2 param2) {
// body
return value;
}
// Use void when the function has no return value.
void function_name(param_type param) {
// body
}
// Use void explicitly when the function takes no parameters (C convention).
return_type function_name(void) {
// body
return value;
}
Function Definition Elements
| Element | Description |
|---|---|
| Return type | Specifies the type of value the function returns. Use void if the function returns nothing. |
| Function name | The identifier used to call the function. Snake case (lowercase with underscores) is the common convention. |
| Parameters | The data the function receives. Multiple parameters are separated by commas. |
| return statement | Returns a value to the caller and exits the function. Can be omitted in void functions. |
| Prototype declaration | Required when the function is used before its definition. Declares the return type and parameter list ahead of the definition. |
Sample Code
#include <stdio.h>
// Prototype declarations (needed because the definitions appear after main).
int add(int a, int b);
double average(int scores[], int count);
void print_separator(void);
int main(void) {
// Call the functions.
int result = add(3, 5);
printf("3 + 5 = %d\n", result); // Prints "3 + 5 = 8".
int scores[] = {80, 75, 90, 65, 88};
double avg = average(scores, 5);
printf("Average: %.1f\n", avg); // Prints "Average: 79.6".
print_separator(); // Prints a separator line.
// You can also use a function call directly inside an expression.
printf("10 + 20 = %d\n", add(10, 20));
return 0;
}
// Takes two integers and returns their sum.
int add(int a, int b) {
return a + b;
}
// Takes an array and its size, and returns the average.
double average(int scores[], int count) {
int sum = 0;
for (int i = 0; i < count; i++) {
sum += scores[i];
}
return (double)sum / count;
}
// A function with no return value.
void print_separator(void) {
printf("--------------------\n");
}
Notes
C functions use pass-by-value (copy) by default. Modifying a parameter inside a function does not affect the variable in the caller. Arrays are automatically converted to pointers when passed, so changes made inside the function are reflected in the caller's array. You also need to pass the array length as a separate argument.
To modify the caller's variable from within a function, use a pointer. See Pointer Basics for details on passing by pointer.
For treating functions as data using "function pointers," see Function Pointers. Recursive functions (functions that call themselves) use the same syntax and are fully supported, as long as you are mindful of stack depth.
If you find any errors or copyright issues, please contact us.