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

C Function Parameters

Parameters are the empty slots in a function that you fill in each time, like the blanks in a recipe for how many eggs. The values you pass in are called arguments.
Syntax
c
return_type function_name(type param1, type param2) {
  // body
}

function_name(argument1, argument2);

What are Parameters?

Parameters are the variables listed in a function's definition that receive external data when the function is called, letting the same function body operate on different values each time it runs.

Example: What are Parameters?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void greet(char name) {
	// Print formatted text to the screen
	printf("Hello, %c", name);
}
// Program execution starts in main()
int main() {
	greet('J');
	// Return 0 to signal that the program finished successfully
	return 0;
}

Single Parameter

A function with a single parameter -- like void greet(char name) -- requires the caller to pass exactly one argument whose type matches that parameter's declared type, or the compiler will flag a mismatch.

Example: Single Parameter

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void greet(char name) {
	// Print formatted text to the screen
	printf("Hello, %c", name);
}
// Program execution starts in main()
int main() {
	greet('A');
	// Return 0 to signal that the program finished successfully
	return 0;
}

Multiple Parameters

Separating several parameters with commas, like int add(int a, int b), lets a function accept multiple pieces of input at once, all available inside the function body under their own local names.

Example: Multiple Parameters

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int add(int a, int b) {
	// Send a + b back to the caller
	return a + b;
}
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", add(3, 4));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Parameter Types

C parameters can be declared as any valid data type -- int, float, double, char, and beyond -- and the arguments you supply on a call must line up with those types and their order, since C matches arguments positionally, not by name.

Example: Parameter Types

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void showInfo(int age, float height, char grade) {
	// Print formatted text to the screen
	printf("%d %.1f %c", age, height, grade);
}
// Program execution starts in main()
int main() {
	showInfo(20, 5.9f, 'A');
	// Return 0 to signal that the program finished successfully
	return 0;
}

Actual vs. Formal Arguments

The parameters listed in a function's own definition are called formal arguments, while the actual values or variables you pass in when calling it are called actual arguments -- the distinction matters when discussing how data flows into a function.

Example: Actual vs. Formal Arguments

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int add(int a, int b) {
	// Send a + b back to the caller
	return a + b;
}
// Program execution starts in main()
int main() {
	int x = 3, y = 4;
	// Print formatted text to the screen
	printf("%d", add(x, y));
	// 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. Passing arguments in the wrong order, which compiles but gives wrong results when the types match.
  2. Expecting a function to change the caller's variable when it received a copy.
  3. Passing an array and using sizeof on the parameter, which gives the size of a pointer.
🔒

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.