</>
Skip to content
C lessons (17/28)

C — Functions

Defining a function

#include <stdio.h>

// Function declaration (prototype)
int add(int a, int b);

// Function definition
int add(int a, int b) {
    return a + b;
}

int main() {
    int result = add(3, 4);
    printf("3 + 4 = %d\n", result);
    return 0;
}

Function prototypes

Declare functions before main() to allow any call order:

#include <stdio.h>

// Prototypes
int factorial(int n);
void printArray(int arr[], int size);

int main() {
    printf("5! = %d\n", factorial(5));
    int nums[] = {1, 2, 3, 4, 5};
    printArray(nums, 5);
    return 0;
}

int factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

Return values

#include <stdio.h>

// Return a value
int square(int n) {
    return n * n;
}

// Return multiple values via pointers
void divmod(int a, int b, int *quotient, int *remainder) {
    *quotient = a / b;
    *remainder = a % b;
}

// No return value
void greet(const char *name) {
    printf("Hello, %s!\n", name);
}

int main() {
    printf("Square: %d\n", square(5));

    int q, r;
    divmod(10, 3, &q, &r);
    printf("10 / 3 = %d remainder %d\n", q, r);

    greet("Alice");
    return 0;
}

Pass by value

C passes all arguments by value — the function gets a copy:

#include <stdio.h>

void tryToModify(int x) {
    x = 100; // Only modifies the local copy
    printf("Inside function: %d\n", x);
}

int main() {
    int a = 5;
    tryToModify(a);
    printf("After function: %d\n", a); // Still 5
    return 0;
}

Pass by pointer

Modify the caller's variables:

#include <stdio.h>

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int x = 5, y = 10;
    printf("Before: x=%d y=%d\n", x, y);
    swap(&x, &y);
    printf("After:  x=%d y=%d\n", x, y);
    return 0;
}

Arrays as parameters

Arrays decay to pointers when passed to functions:

#include <stdio.h>

// Array parameter is actually a pointer
void fillArray(int arr[], int size, int value) {
    for (int i = 0; i < size; i++) {
        arr[i] = value;
    }
}

void printArray(const int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main() {
    int nums[5];
    fillArray(nums, 5, 42);
    printArray(nums, 5); // 42 42 42 42 42
    return 0;
}

Recursive functions

A function that calls itself:

#include <stdio.h>

int fibonacci(int n) {
    if (n <= 0) return 0;
    if (n == 1) return 1;
    return fibonacci(n - 1) + fibonacci(n - 2);
}

void printNumbers(int n) {
    if (n <= 0) return;
    printf("%d ", n);
    printNumbers(n - 1);
}

int main() {
    for (int i = 0; i < 10; i++) {
        printf("fib(%d) = %d\n", i, fibonacci(i));
    }
    printNumbers(5); // 5 4 3 2 1
    return 0;
}

Function pointers

Pass functions as arguments:

#include <stdio.h>

int add(int a, int b) { return a + b; }
int multiply(int a, int b) { return a * b; }

int apply(int a, int b, int (*operation)(int, int)) {
    return operation(a, b);
}

int main() {
    printf("Add: %d\n", apply(3, 4, add));       // 7
    printf("Multiply: %d\n", apply(3, 4, multiply)); // 12

    // Array of function pointers
    int (*ops[])(int, int) = {add, multiply};
    printf("ops[0]: %d\n", ops[0](5, 3)); // 8
    printf("ops[1]: %d\n", ops[1](5, 3)); // 15
    return 0;
}

Variadic functions

Accept variable number of arguments:

#include <stdio.h>
#include <stdarg.h>

int sum(int count, ...) {
    va_list args;
    va_start(args, count);

    int total = 0;
    for (int i = 0; i < count; i++) {
        total += va_arg(args, int);
    }

    va_end(args);
    return total;
}

int main() {
    printf("Sum: %d\n", sum(3, 1, 2, 3));     // 6
    printf("Sum: %d\n", sum(5, 10, 20, 30, 40, 50)); // 150
    return 0;
}

Mini Practice

Write C code that:

  1. Defines a function that swaps two integers using pointers
  2. Creates a recursive function to calculate factorials
  3. Passes an array to a function that doubles each element
  4. Uses a function pointer to select between addition and subtraction

Up Next

In the next lesson, you'll learn about Arrays — declaring and using arrays in C.

Related Topics

Frequently Asked Questions about Functions

What is Functions in C?

Functions is a fundamental concept in C. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Functions?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Functions.

Why is Functions important in C?

Functions is essential for C development. Understanding this concept will help you write better code and solve real-world problems more effectively.