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 = nothing back)
(you choose this)
(empty = no inputs)
(the actual job)
#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; }
Hello, World! Hello, World! Hello, World!
greet() once. Every time you call greet(), that same code runs again. This is the whole point — write once, use many times.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
each has a type
#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; }
Sum = 30 Sum = 8 Sum = 100
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.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
#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; }
Result = 12 10 + 20 = 30
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.
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.
#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; }
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.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).
#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; }
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
Function Syntax — The Complete Picture
General syntax template
| Return type | Parameters | Example | Meaning |
|---|---|---|---|
| void | none | void greet() |
Does something, gives nothing back, takes nothing in |
| void | has params | void displaySum(int a, int b) |
Takes inputs, does something with them, gives nothing back |
| int | has params | int add(int a, int b) |
Takes inputs, computes, sends back an integer |
| int | one param | int square(int n) |
Takes one number, sends back one number |
| float | has params | float average(int a, int b) |
Takes inputs, sends back a decimal number |
| char | has params | char getGrade(float avg) |
Takes a float, sends back one character like 'A' or 'B' |
/* 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 } */
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.
#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; }
Enter first number : 4 Enter second number: 5 --- Results --- Sum = 9 Product = 20 Square of 4 = 16 Square of 5 = 25
Enter first number : 5 Enter second number: 10 --- Results --- Sum = 15 Product = 50 Square of 5 = 25 Square of 10 = 100
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.- 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