Functions in C: solved exercises step by step

Functions in C: solved exercises step by step

If you searched for C functions solved exercises, here are the patterns that appear in any introductory C exam.

The goal is to master four key situations: void functions, functions that return a value, pass by value, and pass by reference using pointers.

Problem statement

Solve these 4 mini exercises using functions:

  1. print a greeting (void function with no parameters),
  2. compute the square of a number (function with return value),
  3. swap two integers (pass by reference with pointers),
  4. compute the factorial of n iteratively.

C solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>

/* 1. Void function: returns nothing */
void greet(void) {
    printf("Hello from a C function\n");
}

/* 2. Function with return value: returns the square */
int square(int x) {
    return x * x;
}

/* 3. Pass by reference: swaps two integers */
void swap(int *a, int *b) {
    int tmp = *a;
    *a = *b;
    *b = tmp;
}

/* 4. Iterative function: factorial */
long factorial(int n) {
    long result = 1;
    for (int i = 2; i <= n; i++) {
        result *= i;
    }
    return result;
}

int main(void) {
    greet();

    int n = 7;
    printf("square(%d) = %d\n", n, square(n));

    int x = 3, y = 9;
    swap(&x, &y);
    printf("after swap: x=%d, y=%d\n", x, y);

    printf("factorial(6) = %ld\n", factorial(6));

    return 0;
}

Expected output

1
2
3
4
Hello from a C function
square(7) = 49
after swap: x=9, y=3
factorial(6) = 720

Common mistakes

  • Forgetting to declare the function prototype before main when it is defined afterward.
  • Confusing pass by value with pass by reference: modifying the local parameter does not change the original variable.
  • Omitting return in non-void functions.
  • Using void as the return type when the function actually returns something.

Practical use

Functions are the foundation of any real C program:

  • they split logic into reusable blocks,
  • they allow pass by reference to modify the caller’s variables,
  • they make unit testing and maintenance much easier.

Mastering pass by reference with pointers is essential before working with arrays and data structures.

Guided practice and next step

If you want a complete path with progressive difficulty:

FAQ

When should I use void as the return type?

When the function does not need to return any value to the caller: it prints output, modifies variables by reference, or performs an action with no result.

What is the difference between pass by value and pass by reference?

Pass by value copies the data; the function works on the copy and the original is unchanged. Pass by reference passes the memory address (&variable) and the function can modify the original through the pointer.

Can a C function return more than one value?

Not directly. The usual approach is to return one value via return and the rest by reference (pointers), or group them in a struct.