← Back to C Course | Chapter 5: Functions | Lesson 1 of 8

C Functions Introduction

A function is a named set of steps you can use over and over, like a recipe. You write it once and call it whenever you need it.
Syntax
c
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?

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Calling a function before it is declared, which causes an implicit declaration warning or error.
  2. Forgetting return in a non-void function, which is undefined behavior if the result is used.
  3. Writing foo; instead of foo();, which does not call the function.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.