Functions for Beginners
0%
C Functions  ·  Step by Step

Functions —
Beginner's Guide

Start from zero. Learn functions step by step — from the simplest "say hello" to taking user input, adding, multiplying, and finding squares all in one program.

1 void greet()
2 void with params
3 int return value
4 square()
5 factorial
6 syntax summary
7 add + multiply + square
Step 1

void greet() — Simplest Possible Function

This is the simplest function you can write. It takes no inputs and gives nothing back. It just does one job — prints a message.

void means "this function returns nothing". You call it by writing its name followed by ().

The 4 parts of every function — shown on greet()
void
① return type
(void = nothing back)
greet
② function name
(you choose this)
( )
③ parameters
(empty = no inputs)
{ printf("Hello, World!\n"); }
④ body
(the actual job)
step1_greet.c
C
#include <stdio.h>

/* Define the function — ABOVE main */
void greet() {
    printf("Hello, World!\n");
}

int main() {
    greet();   /* Call it — just write the name + () */
    greet();   /* Call it again — runs the same code */
    greet();   /* Call it a third time */
    return 0;
}
output
Hello, World!
Hello, World!
Hello, World!
The function is defined once but called three times. You write the code inside greet() once. Every time you call greet(), that same code runs again. This is the whole point — write once, use many times.
step 2
Step 2

void displaySum(int a, int b) — Function with Parameters

Now the function takes inputs — called parameters. You put them inside the (). The function uses those values to do its job.

void still means it returns nothing — it just prints the result directly. The values a and b inside the function are copies of what you passed in.

displaySum — void with two int parameters
void
① void — no return
displaySum
② name
(int a, int b)
③ two inputs
each has a type
{ printf("Sum = %d\n", a + b); }
④ uses a and b
step2_display_sum.c
C
#include <stdio.h>

void displaySum(int a, int b) {
    printf("Sum = %d\n", a + b);
}

int main() {
    displaySum(10, 20);   /* a=10, b=20 */
    displaySum(5,  3);    /* a=5,  b=3  */
    displaySum(99, 1);    /* a=99, b=1  */
    return 0;
}
output
Sum = 30
Sum = 8
Sum = 100
The numbers 10 and 20 are called arguments. When you write displaySum(10, 20), the value 10 goes into parameter a, and 20 goes into b. Each call can pass different numbers — the function works for all of them.
step 3
Step 3

int add(int a, int b) — Function that Returns a Value

Instead of printing inside the function, this function computes a result and sends it back to whoever called it. The return type is int — it gives back one integer.

The keyword return sends the value back. The caller stores it in a variable or uses it directly in printf.

add() — int return type
int
① gives back an int
add
② name
(int a, int b)
③ two inputs
{ return a + b; }
④ return sends the answer back
step3_add.c
C
#include <stdio.h>

int add(int a, int b) {
    return a + b;   /* sends the sum back to the caller */
}

int main() {
    int result = add(5, 7);   /* store the returned value */
    printf("Result = %d\n", result);

    /* Or use directly in printf — no extra variable needed */
    printf("10 + 20 = %d\n", add(10, 20));

    return 0;
}
output
Result = 12
10 + 20 = 30
void vs int — what's the difference?
void displaySum() — prints inside the function. You can't store or reuse the result.
int add() — sends the answer back. You can store it, print it, or use it in more calculations.
step 4
Step 4

int square(int num) — One Input, One Output

square() takes one number and returns its square (number × itself). It is the cleanest kind of function — one input in, one output out, pure math, no printing.

Call it once to get the square of 6. Call it again to get the square of 9. Same function, different answers each time based on what you pass in.

step4_square.c
C
#include <stdio.h>

int square(int num) {
    return num * num;
}

int main() {
    printf("square(6) = %d\n", square(6));   /* 36  */
    printf("square(9) = %d\n", square(9));   /* 81  */
    printf("square(3) = %d\n", square(3));   /* 9   */

    /* Use the result in a calculation */
    int s = square(5) + square(4);   /* 25 + 16 = 41 */
    printf("5² + 4² = %d\n", s);

    return 0;
}
output
square(6) = 36
square(9) = 81
square(3) = 9
5² + 4² = 41
square(5) + square(4) — you can use a function's returned value directly in an expression, just like a variable. square(5) returns 25, square(4) returns 16, so the whole thing equals 41.
step 5
Step 5

Recursive Function — Factorial

A recursive function is one that calls itself. factorial(5) = 5 × factorial(4). factorial(4) = 4 × factorial(3)... and so on until factorial(0) = 1 (the stopping point, called the base case).

Every recursive function needs two parts: a base case (stops it) and a recursive case (calls itself with a smaller number).

step5_factorial.c
C
#include <stdio.h>

int factorial(int n) {
    if (n == 0)           /* base case — STOP here */
        return 1;
    return n * factorial(n - 1);  /* calls itself */
}

int main() {
    printf("factorial(0) = %d\n", factorial(0));  /* 1   */
    printf("factorial(1) = %d\n", factorial(1));  /* 1   */
    printf("factorial(5) = %d\n", factorial(5));  /* 120 */
    printf("factorial(6) = %d\n", factorial(6));  /* 720 */
    return 0;
}
output
factorial(0) = 1
factorial(1) = 1
factorial(5) = 120
factorial(6) = 720

Trace of factorial(5):

factorial(5) = 5 × factorial(4)
             = 5 × 4 × factorial(3)
             = 5 × 4 × 3 × factorial(2)
             = 5 × 4 × 3 × 2 × factorial(1)
             = 5 × 4 × 3 × 2 × 1 × factorial(0)
             = 5 × 4 × 3 × 2 × 1 × 1 = 120

step 6 — syntax summary
Step 6

Function Syntax — The Complete Picture

General syntax template
return_type
int / float / void / char
function_name
you choose a clear name
(parameters)
inputs — can be empty
{ statements; return value; }
body — what it does
Return typeParametersExampleMeaning
voidnone void greet() Does something, gives nothing back, takes nothing in
voidhas params void displaySum(int a, int b) Takes inputs, does something with them, gives nothing back
inthas params int add(int a, int b) Takes inputs, computes, sends back an integer
intone param int square(int n) Takes one number, sends back one number
floathas params float average(int a, int b) Takes inputs, sends back a decimal number
charhas params char getGrade(float avg) Takes a float, sends back one character like 'A' or 'B'
syntax_example.c
C
/* The example from the syntax lesson */
int multiply(int x, int y)
{
    return x * y;
}

/* Rules:
   - Define the function BEFORE main (or declare a prototype above main)
   - Return type must match what you actually return
   - void functions do NOT have a return value statement
   - Every { must have a matching }
   - Semicolon ends statements INSIDE the body, NOT after the } */
step 7 — all together
Step 7

add + multiply + square — Three Functions, User Input

Now we put it all together. The user enters two numbers. Three separate functions — add(), multiply(), square() — each do their one job. main() just reads the input and calls them.

Notice square() is called twice — once for each number. Same function, different argument each time.

add_multiply_square.c
C
#include <stdio.h>

/* ── Three functions — each does ONE job ─── */
int add(int a, int b) {
    return a + b;
}

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

int square(int n) {
    return n * n;
}

/* ── main: get input, call functions, print ─ */
int main() {
    int num1, num2;

    printf("Enter first number : ");
    scanf("%d", &num1);

    printf("Enter second number: ");
    scanf("%d", &num2);

    printf("\n--- Results ---\n");
    printf("Sum          = %d\n", add(num1, num2));
    printf("Product      = %d\n", multiply(num1, num2));
    printf("Square of %d  = %d\n", num1, square(num1));
    printf("Square of %d  = %d\n", num2, square(num2));

    return 0;
}
output — user enters 4 and 5
Enter first number : 4
Enter second number: 5

--- Results ---
Sum          = 9
Product      = 20
Square of 4  = 16
Square of 5  = 25
output — user enters 5 and 10
Enter first number : 5
Enter second number: 10

--- Results ---
Sum          = 15
Product      = 50
Square of 5  = 25
Square of 10 = 100
Function calling explained:
add(num1, num2) — num1 goes into parameter a, num2 goes into b, function returns a+b.
square(num1) — num1 goes into parameter n, returns n×n. Called a second time with num2.
The returned value goes directly into printf as the %d argument.
checklist
  • Step 1 — void greet() takes no inputs, returns nothing, just runs its body when called
  • Step 2 — void displaySum(int a, int b) takes two inputs but still returns nothing
  • Step 3 — int add(int a, int b) uses return to send the answer back to the caller
  • Step 3 — Difference: void prints inside, int return lets the caller use the result
  • Step 4 — square(5) + square(4) — function result used directly in an expression
  • Step 5 — Recursive function has two parts: base case (stop) and recursive case (smaller call)
  • Step 6 — Syntax: return_type name(params) { body; return value; }
  • Step 7 — Three separate functions, one main, user input — I can write this from scratch