C Functions

Declare, define and call functions.

✍️ 2 exercises in this lesson
  • Syntax: returnType name(parameters) { ... }
  • A function must be declared before it is used. Put a prototype at the top (int add(int, int);) and the definition below main.
  • Arguments are passed by value (copied). To let a function change a variable, pass a pointer.
  • Recursive functions call themselves.

Key Takeaways: C Functions

Mastering C Functions helps you write clean and reliable C code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

C Functions Code Example

C
#include <stdio.h>

int add(int a, int b);
void swap(int *a, int *b);
int factorial(int n);

int main() {
    int x = 3, y = 9;
    printf("add: %d\n", add(x, y));
    swap(&x, &y);
    printf("after swap: x=%d y=%d\n", x, y);
    printf("5! = %d\n", factorial(5));
    return 0;
}

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

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

int factorial(int n) { return n <= 1 ? 1 : n * factorial(n - 1); }
Output
add: 12
after swap: x=9 y=3
5! = 120
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

C Functions Practice Exercises

Check what you learned in “C Functions”.

/ 2 solved
Fill in the blank

A function prototype declares a function before main. Complete it.

int add(int a, int b)
✓ Correct! Not quite. Try again.
Multiple choice

How are arguments passed to C functions by default?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services