C Functions
What is a Function?
A function is a reusable block of code that performs a specific task. Functions help break a large program into smaller, manageable pieces. Every C program has at least one function — main().
Function Declaration, Definition and Call
- Declaration (Prototype) — tells the compiler about the function's name, return type, and parameters. Placed before
main(). - Definition — the actual body of the function with its implementation.
- Call — invoking the function to execute it.
// Declaration (prototype)
int add(int a, int b);
// Definition
int add(int a, int b) {
return a + b;
}
// Call
int result = add(5, 3); // result = 8
Variable Scope
- Local variables — declared inside a function, exist only within that function
- Global variables — declared outside all functions, accessible everywhere
- Static local variables — retain their value between function calls
#include <stdio.h>
// Function prototypes (declarations)
int add(int a, int b);
float average(int a, int b, int c);
void greet(char name[]);
void countCalls();
int main() {
printf("Sum: %d\n", add(10, 5));
printf("Average: %.2f\n", average(10, 20, 30));
greet("Alice");
// Static variable demo
countCalls();
countCalls();
countCalls();
return 0;
}
int add(int a, int b) {
return a + b;
}
float average(int a, int b, int c) {
return (a + b + c) / 3.0f;
}
void greet(char name[]) {
printf("Hello, %s!\n", name);
// no return statement needed for void
}
void countCalls() {
static int count = 0; // retains value between calls
count++;
printf("Function called %d time(s)\n", count);
}
/*
Output:
Sum: 15
Average: 20.00
Hello, Alice!
Function called 1 time(s)
Function called 2 time(s)
Function called 3 time(s)
*/
#include <stdio.h>
// Recursive factorial: n! = n * (n-1)!
long long factorial(int n) {
if (n == 0 || n == 1) return 1; // base case
return n * factorial(n - 1); // recursive call
}
// Recursive Fibonacci: fib(n) = fib(n-1) + fib(n-2)
int fibonacci(int n) {
if (n <= 1) return n; // base cases: fib(0)=0, fib(1)=1
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
// Factorial
for (int i = 0; i <= 10; i++) {
printf("%d! = %lld\n", i, factorial(i));
}
// Fibonacci series
printf("\nFibonacci (first 10): ");
for (int i = 0; i < 10; i++) {
printf("%d ", fibonacci(i));
}
printf("\n");
return 0;
}
/*
0! = 1
1! = 1
...
10! = 3628800
Fibonacci (first 10): 0 1 1 2 3 5 8 13 21 34
*/
#include <stdio.h>
// Call by value — a copy is passed; original is NOT modified
void doubleByValue(int x) {
x = x * 2;
printf("Inside doubleByValue: %d\n", x);
}
// Call by reference — pointer is passed; original IS modified
void doubleByRef(int *x) {
*x = *x * 2;
printf("Inside doubleByRef: %d\n", *x);
}
// Swap using pointers
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int num = 10;
doubleByValue(num);
printf("After doubleByValue: %d\n\n", num); // still 10
doubleByRef(&num);
printf("After doubleByRef: %d\n\n", num); // now 20
int a = 5, b = 8;
printf("Before swap: a=%d, b=%d\n", a, b);
swap(&a, &b);
printf("After swap: a=%d, b=%d\n", a, b);
return 0;
}
/*
Inside doubleByValue: 20
After doubleByValue: 10
Inside doubleByRef: 20
After doubleByRef: 20
Before swap: a=5, b=8
After swap: a=8, b=5
*/
Ready to Level Up Your Skills?
Explore 500+ free tutorials across 20+ languages and frameworks.