C Functions Introduction
In this page:
return_type function_name(parameters) {
// body
}
function_name(arguments); // call
What is a Function?
A function bundles a specific task into a reusable, named block of code, so you can call that same logic from multiple places in your program instead of retyping it, and fix a bug in one place instead of many.
Example: What is a Function?
// Include standard input/output (printf, scanf)
#include <stdio.h>
void greet() {
// Print formatted text to the screen
printf("Hello");
}
// Program execution starts in main()
int main() {
greet();
greet();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Defining a Function
Defining a function means writing its return type, a unique name, and a body in curly braces containing the statements it runs -- the compiler uses this definition to know exactly what the function does and expects.
Example: Defining a Function
// Include standard input/output (printf, scanf)
#include <stdio.h>
int square(int n) {
// Send n * n back to the caller
return n * n;
}
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", square(4));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Calling a Function
Calling a function -- writing its name followed by parentheses -- transfers control to that function's body; once it finishes, execution resumes right where the call was made, optionally with a value handed back.
Example: Calling a Function
// Include standard input/output (printf, scanf)
#include <stdio.h>
int square(int n) {
// Send n * n back to the caller
return n * n;
}
// Program execution starts in main()
int main() {
// Declare result and set it to square(5)
int result = square(5);
// Print formatted text to the screen
printf("%d", result);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Void Functions
A void function performs an action (like printing something or updating a variable) without handing any value back to whoever called it -- the void keyword as the return type explicitly signals "nothing comes back."
Example: Void Functions
// Include standard input/output (printf, scanf)
#include <stdio.h>
void printMessage() {
// Print formatted text to the screen
printf("This function returns nothing");
}
// Program execution starts in main()
int main() {
printMessage();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Multiple Helper Functions
Splitting a program into several small helper functions, each responsible for one clear task, makes each piece easy to test and debug independently, rather than hunting for a bug inside one enormous block of code.
Example: Multiple Helper Functions
// Include standard input/output (printf, scanf)
#include <stdio.h>
int square(int n) {
// Send n * n back to the caller
return n * n;
}
int cube(int n) {
// Send n * n * n back to the caller
return n * n * n;
}
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d %d", square(3), cube(3));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Calling a function before it is declared, which causes an implicit declaration warning or error.
- Forgetting
returnin a non-void function, which is undefined behavior if the result is used. - Writing
foo;instead offoo();, which does not call the function.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: