Back to Blog

C in 100 Seconds: Functions | Episode 9

Daryl WongDaryl Wong

Video: C in 100 Seconds: Functions — Define Once Call Anywhere | Episode 9 by Taught by Celeste AI - AI Coding Coach

Watch full page →

Functions — Reusable Code in C

C in 100 Seconds, Episode 9


Functions let you name a block of code and call it whenever you need it. They're how you go from one giant main to organized, reusable pieces.

Defining a Function

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

This function takes two integers, adds them, and returns the result. The return type (int) tells the caller what kind of value to expect.

Calling a Function

int result = add(10, 25);
printf("10 + 25 = %d\n", result);

You pass arguments in, and the function hands back a value. The arguments are copies — changing a inside add won't affect the original variable.

Void Functions

Not every function needs to return something:

void greet(char name[]) {
  printf("Hello, %s!\n", name);
}

void means "returns nothing." You call it for its side effects — in this case, printing to the screen.

greet("Alice");
greet("Bob");

Why Functions Matter

Without functions, you'd be copying and pasting the same code everywhere. Functions give you:

  • Reusability — write once, call many times
  • Readabilityadd(10, 25) is clearer than inline arithmetic in a complex expression
  • Testability — isolated logic is easier to verify

Full Code

#include <stdio.h>

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

void greet(char name[]) {
  printf("Hello, %s!\n", name);
}

int main() {
  int result = add(10, 25);
  printf("10 + 25 = %d\n", result);

  greet("Alice");
  greet("Bob");

  return 0;
}

Compile and Run

gcc functions.c -o functions
./functions

Next episode: Arrays — storing multiple values under one name.

Student code: github.com/GoCelesteAI/c-in-100-seconds/episode09