C Programming · December 2, 2026

Categories of Functions in C

Module II · Functions · Categories of Functions


🎯 Why Learn the Categories?

Think about four people at a shop. One person switches on the lights: no instructions needed, nothing handed back. A second is told which item to pack and packs it, nothing handed back. A third counts the cash in the drawer and tells you the total, with no instructions needed. A fourth is given two prices and returns the sum.

All four are doing a job, but they differ in two simple things: do they receive anything? and do they give anything back? C functions are the same, and functions are grouped into four categories based on arguments (input) and return value (output).

❌ Returns NO value✅ Returns a value
❌ NO argumentsCategory 1
void fun(void)
prints a banner, reads and prints inside
Category 3
int fun(void)
reads and returns a number
✅ Takes argumentsCategory 2
void fun(int a)
prints a table for a given n
Category 4
int fun(int a, int b)
add, average, isPrime

Figure 1: The four categories of functions: with or without arguments, with or without a return value.

📌 Category 1: no arguments, no return value. Category 2: arguments, no return value. Category 3: no arguments, return value. Category 4: arguments and return value. Choosing the right category is part of designing a good program.

1️⃣ Where the Data Flows

Look at the two arrows in each row. The right-pointing arrow carries arguments into the function. The left-pointing arrow carries the return value back to the caller.

goes in →← comes back
Category 1
main()
✖ nothing
fun()
✖ nothing
Category 2
main()
a ➜
fun(a)
✖ nothing
Category 3
main()
✖ nothing
fun()
➜ result
Category 4
main()
a, b ➜
fun(a, b)
➜ result

Figure 2: What goes into the function and what comes back, for each category.

2️⃣ Category 1: No Arguments, No Return Value

The function does everything on its own. It takes nothing and returns nothing. The return type is void and the parameter list is (void) (an empty list also works, but (void) says clearly “no inputs”).

void functionName(void);

Program 1: A reusable banner

#include <stdio.h>

void printBanner(void);

int main(void) {
    printBanner();
    printf("Lecture 32: Categories of Functions\n");
    printBanner();
    return 0;
}

void printBanner(void) {
    printf("==============================\n");
    printf("   Welcome to Computer Programming\n");
    printf("==============================\n");
}
🖥️ Output:
==============================
Welcome to Computer Programming
==============================
Lecture 32: Categories of Functions
==============================
Welcome to Computer Programming
==============================
  • void printBanner(void); is the prototype, placed before main().
  • main() calls printBanner() twice. The same three lines print each time, so we wrote them once.
  • The function returns nothing, so we call it as a plain statement, never inside printf or an assignment.

Program 2: Category 1 that reads its own input

#include <stdio.h>

void addTwoNumbers(void);

int main(void) {
    addTwoNumbers();
    return 0;
}

void addTwoNumbers(void) {
    int a, b;
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);
    printf("Sum = %d\n", a + b);
}
🖥️ Output:
Enter two numbers: 12 8
Sum = 20
  • addTwoNumbers() reads two numbers, adds them and prints, all inside the function.
  • Every call asks for fresh input and prints. main() never sees the sum.
⚠️ Limitation: The result is locked inside the function. main() cannot reuse the sum for anything else, so a category 1 function is best for printing and displaying jobs: menus, banners, instructions.

3️⃣ Category 2: Arguments, No Return Value

main() sends values in, and the function uses them but sends nothing back. The function does its work, usually printing, and ends.

void functionName(int a, float b);

Program 3: Print a table and a grade

#include <stdio.h>

void printTable(int n);
void printGrade(int marks);

int main(void) {
    printTable(7);
    printGrade(82);
    printGrade(41);
    return 0;
}

void printTable(int n) {
    int i;
    for (i = 1; i <= 5; i++) {
        printf("%d x %d = %d\n", n, i, n * i);
    }
}

void printGrade(int marks) {
    if (marks >= 75)
        printf("%d marks: Distinction\n", marks);
    else if (marks >= 40)
        printf("%d marks: Pass\n", marks);
    else
        printf("%d marks: Fail\n", marks);
}
🖥️ Output:
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
82 marks: Distinction
41 marks: Pass
  • printTable(7) copies 7 into the parameter n. The loop then prints the table of 7.
  • printGrade(82) and printGrade(41) reuse the same function with different data. This is the benefit over category 1.
  • The values 7, 82 and 41 in the calls are actual arguments. The names n and marks in the definitions are formal parameters.

4️⃣ Category 3: No Arguments, Returns a Value

The function needs nothing from main(), but it produces a result and hands it back with return. The return type must match the type of the value returned.

int functionName(void);

Program 4: Read a valid price and return a fixed discount

#include <stdio.h>

int readPositive(void);
int getDiscountPercent(void);

int main(void) {
    int price, discount, finalPrice;

    price = readPositive();
    discount = getDiscountPercent();
    finalPrice = price - price * discount / 100;

    printf("Price %d, discount %d%%, you pay %d\n", price, discount, finalPrice);
    return 0;
}

int readPositive(void) {
    int n;
    do {
        printf("Enter a positive price: ");
        scanf("%d", &n);
    } while (n <= 0);
    return n;
}

int getDiscountPercent(void) {
    return 15;
}
🖥️ Output:
Enter a positive price: -5
Enter a positive price: 400
Price 400, discount 15%, you pay 340
  • readPositive() keeps asking until the user types a positive number, then returns it. We typed -5 first, so it asked again, then accepted 400.
  • price = readPositive(); stores the returned value in price. Without that assignment the value would be lost.
  • getDiscountPercent() takes nothing and always returns 15. Real programs often return a setting, a count or a reading this way.
  • Check the maths: 400 − 400 × 15 / 100 = 400 − 60 = 340.

5️⃣ Category 4: Arguments and a Return Value

This is the most useful and most common category. The function takes inputs, calculates, and returns the answer, so the caller decides what to do with it: print it, store it, or use it in a bigger expression.

int functionName(int a, int b);

Program 5: Add, average and prime check

#include <stdio.h>

int add(int a, int b);
float average(int a, int b, int c);
int isPrime(int n);

int main(void) {
    printf("add(7, 5)        = %d\n", add(7, 5));
    printf("average(80,90,70) = %.2f\n", average(80, 90, 70));
    printf("isPrime(13)      = %d\n", isPrime(13));
    printf("isPrime(15)      = %d\n", isPrime(15));
    return 0;
}

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

float average(int a, int b, int c) {
    return (a + b + c) / 3.0f;
}

int isPrime(int n) {
    int i;
    if (n < 2)
        return 0;
    for (i = 2; i * i <= n; i++) {
        if (n % i == 0)
            return 0;
    }
    return 1;
}
🖥️ Output:
add(7, 5) = 12
average(80,90,70) = 80.00
isPrime(13) = 1
isPrime(15) = 0
  • add(7, 5) returns 12, and printf prints the returned value.
  • average returns a float. We divide by 3.0f so the result keeps its decimal part.
  • isPrime returns 1 for prime and 0 for not prime. A function used as a yes/no answer is common.
  • return 0; inside the loop leaves the function early, as soon as a divisor is found.
Line: printf("%d", add(7, 5));
① main() calls with actual arguments 7 and 5
add(7, 5)
⬇ copies 7 and 5 into the parameters
② inside add()
a = 7
b = 5
⬇ return a + b; computes 7 + 5
③ the value 12 returns to the call
add(7, 5) = 12
⬇ the call is replaced by 12
printf("%d", 12); prints 12

Figure 3: Following one category 4 call, add(7, 5), from the call to the printed result.

6️⃣ One Task, Four Ways

Adding two numbers can be written in all four categories. Comparing them shows the real difference.

#include <stdio.h>

void sumV1(void);            /* no input, no output value   */
void sumV2(int a, int b);    /* input, no output value      */
int  sumV3(void);            /* no input, returns a value   */
int  sumV4(int a, int b);    /* input and returns a value   */

int main(void) {
    sumV1();
    sumV2(5, 3);
    printf("V3 sum = %d\n", sumV3());
    printf("V4 sum = %d\n", sumV4(5, 3));

    /* Only category 4 can be reused inside an expression */
    printf("sumV4(10, 20) + sumV4(1, 2) = %d\n", sumV4(10, 20) + sumV4(1, 2));
    return 0;
}

void sumV1(void) {
    int a = 5, b = 3;
    printf("V1 sum = %d\n", a + b);
}

void sumV2(int a, int b) {
    printf("V2 sum = %d\n", a + b);
}

int sumV3(void) {
    int a = 5, b = 3;
    return a + b;
}

int sumV4(int a, int b) {
    return a + b;
}
🖥️ Output:
V1 sum = 8
V2 sum = 8
V3 sum = 8
V4 sum = 8
sumV4(10, 20) + sumV4(1, 2) = 33
  • All four versions print 8, but only sumV3 and sumV4 hand the value back.
  • Because they return a value, we could write sumV4(10, 20) + sumV4(1, 2) and get 33. That is impossible with a void function.
  • sumV4 is also the most flexible, because the numbers are not fixed inside the function.
CategoryExample prototypeInputOutputBest used for
1void menu(void);NoNoBanners, menus, fixed messages
2void printTable(int n);YesNoDisplaying results for given data
3int readNumber(void);NoYesReading input, getting a stored value
4int add(int a, int b);YesYesCalculations and checks

7️⃣ A Mini Project: Calculator with Category 4 Functions

Each operation becomes a small function that takes two numbers and returns the answer. main() only reads the input and prints.

#include <stdio.h>

int add(int a, int b);
int subtract(int a, int b);
int multiply(int a, int b);
float divide(int a, int b);

int main(void) {
    int x, y;

    printf("Enter two numbers: ");
    scanf("%d %d", &x, &y);

    printf("%d + %d = %d\n", x, y, add(x, y));
    printf("%d - %d = %d\n", x, y, subtract(x, y));
    printf("%d * %d = %d\n", x, y, multiply(x, y));
    printf("%d / %d = %.2f\n", x, y, divide(x, y));
    return 0;
}

int add(int a, int b)      { return a + b; }
int subtract(int a, int b) { return a - b; }
int multiply(int a, int b) { return a * b; }

float divide(int a, int b) {
    if (b == 0) {
        printf("Cannot divide by zero!\n");
        return 0;
    }
    return (float)a / b;
}
🖥️ Output:
Enter two numbers: 20 4
20 + 4 = 24
20 – 4 = 16
20 * 4 = 80
20 / 4 = 5.00
  • We typed 20 4, so we got 24, 16, 80 and 5.00.
  • divide checks for zero first and returns a float, using (float)a / b so the division keeps decimals.
  • If a function body is one line, it can be written on one line as int add(int a, int b) { return a + b; }.

🤔 Which Category Should I Choose?

Ask two questions: does it need data, and must it give a result back?

Does the function need data from main()?
No
Must it give a result back?
No ➜
Category 1
Yes ➜
Category 3
Yes
Must it give a result back?
No ➜
Category 2
Yes ➜
Category 4

Figure 4: A two-question guide for choosing a category.

💡 Rule of thumb: Let functions calculate and return, and let main() do the printing. A function that returns a value is easier to test and reuse than one that prints inside.

🎁 Bonus: Even printf Is a Category 4 Function

#include <stdio.h>

int main(void) {
    int count;

    count = printf("Hello\n");   /* printf is a category 4 function */
    printf("printf returned %d\n", count);
    return 0;
}
🖥️ Output:
Hello
printf returned 6
  • printf returns the number of characters it printed. "Hello\n" has 5 letters plus the newline, so it returns 6.
  • We normally ignore this value, and C allows that. Calling a value-returning function as a plain statement is legal, but the value is thrown away.

⚠️ Common Mistakes

MistakeWhy it is wrongFix
x = greet(); where greet is voidA void function returns nothing to storeCall it as greet();
Calling readPositive(); and ignoring the resultThe returned number is lostn = readPositive();
Forgetting return in an int functionThe caller gets garbageAlways return on every path
Calling add(5) for add(int a, int b)Argument count does not matchPass exactly two values
Printing inside a function that should returnThe result cannot be reusedReturn it, and print in main()
Missing prototype before main()The compiler does not know the function yetDeclare it first

❓ Frequently Asked Questions

Q1. What is the difference between void f() and void f(void)?
In C, f() means “unspecified parameters”, while f(void) clearly means none. Prefer (void).

Q2. Can a function return more than one value?
Not directly with return. Pointers, which come in later lectures, solve this.

Q3. Can a void function use return?
Yes, but only as return; with no value, to leave early.

Q4. Which category is best?
Category 4 is the most flexible, but use the one that fits the job. A menu is naturally category 1.

✅ Key Takeaways

  • Functions fall into four categories by arguments and return value.
  • Category 1 takes and gives nothing; category 2 takes only; category 3 gives only; category 4 does both.
  • Actual arguments are copied into the formal parameters on each call.
  • Store or use a returned value, or it is lost. A void function has no value to store.
  • Prefer functions that return results, and print in main().
  • Always declare a prototype before main().
🎓 Practice questions: (1) Write a category 1 function that prints your name and branch inside a box of stars. (2) Write a category 2 function void printSquare(int n). (3) Write a category 3 function that reads a mark between 0 and 100 and returns it, asking again for invalid input. (4) Write a category 4 function int maxOf3(int a, int b, int c). (5) Rewrite the same simple-interest program in all four categories and compare them.
All 29 lessons in this series (open course page)

Part 1 · Getting Started

Part 2 · Data Types, Input/Output and Operators

Part 3 · Decisions and Loops

Part 4 · Arrays

Part 5 · Strings

Part 6 · Functions and Scope

Part 7 · Pointers