Define and Call C Functions
To customize a function, you need to follow these steps:
- To define a function prototype: Declare the function prototype at the beginning of the code file to inform the compiler of the function’s name, parameter types, and return value type.
int add(int a, int b); // 函数原型
- Implementing a function definition: Writing the specific implementation of the function in another part of the code file.
int add(int a, int b) {
return a + b;
}
- Call a custom function in the main function: Use a function that you have defined in the main function to perform a specific task.
int main() {
int result = add(3, 5);
printf("Result: %d\n", result);
return 0;
}
This way, when you run the program, the main function will call the add function to calculate the sum of 3 and 5, and then print out the result.