On this page (13 sections)
- 🎯 Why Learn the Categories?
- 1️⃣ Where the Data Flows
- 2️⃣ Category 1: No Arguments, No Return Value
- 3️⃣ Category 2: Arguments, No Return Value
- 4️⃣ Category 3: No Arguments, Returns a Value
- 5️⃣ Category 4: Arguments and a Return Value
- 6️⃣ One Task, Four Ways
- 7️⃣ A Mini Project: Calculator with Category 4 Functions
- 🤔 Which Category Should I Choose?
- 🎁 Bonus: Even printf Is a Category 4 Function
- ⚠️ Common Mistakes
- ❓ Frequently Asked Questions
- ✅ Key Takeaways
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 arguments | Category 1void fun(void)prints a banner, reads and prints inside | Category 3int fun(void)reads and returns a number |
| ✅ Takes arguments | Category 2void fun(int a)prints a table for a given n | Category 4int 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.
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");
}
==============================
Welcome to Computer Programming
==============================
Lecture 32: Categories of Functions
==============================
Welcome to Computer Programming
==============================
void printBanner(void);is the prototype, placed beforemain().main()callsprintBanner()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
printfor 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);
}
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.
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);
}
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 parametern. The loop then prints the table of 7.printGrade(82)andprintGrade(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
nandmarksin 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;
}
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 inprice. 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;
}
add(7, 5) = 12
average(80,90,70) = 80.00
isPrime(13) = 1
isPrime(15) = 0
add(7, 5)returns 12, andprintfprints the returned value.averagereturns afloat. We divide by3.0fso the result keeps its decimal part.isPrimereturns1for prime and0for 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.
printf("%d", add(7, 5));a = 7b = 5return a + b; computes 7 + 5add(7, 5) = 12printf("%d", 12); prints 12Figure 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;
}
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
sumV3andsumV4hand the value back. - Because they return a value, we could write
sumV4(10, 20) + sumV4(1, 2)and get 33. That is impossible with avoidfunction. sumV4is also the most flexible, because the numbers are not fixed inside the function.
| Category | Example prototype | Input | Output | Best used for |
|---|---|---|---|---|
| 1 | void menu(void); | No | No | Banners, menus, fixed messages |
| 2 | void printTable(int n); | Yes | No | Displaying results for given data |
| 3 | int readNumber(void); | No | Yes | Reading input, getting a stored value |
| 4 | int add(int a, int b); | Yes | Yes | Calculations 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;
}
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.
dividechecks for zero first and returns afloat, using(float)a / bso 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?
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.
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;
}
Hello
printf returned 6
printfreturns 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
| Mistake | Why it is wrong | Fix |
|---|---|---|
x = greet(); where greet is void | A void function returns nothing to store | Call it as greet(); |
Calling readPositive(); and ignoring the result | The returned number is lost | n = readPositive(); |
Forgetting return in an int function | The caller gets garbage | Always return on every path |
Calling add(5) for add(int a, int b) | Argument count does not match | Pass exactly two values |
| Printing inside a function that should return | The result cannot be reused | Return it, and print in main() |
Missing prototype before main() | The compiler does not know the function yet | Declare 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
voidfunction 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 functionvoid 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 functionint 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
- Lesson 1: Introduction to C Programming: Overview, Features and Applications
- Lesson 2: The Structure of a C Program: Building Your First Blueprint
- Lesson 3: Identifiers, Keywords and Variables: Naming Things in C Programming
- Lesson 4: Introduction to Algorithms: Thinking Before You Code
- Lesson 5: Introduction to Flowcharts: Drawing the Logic You Already Know
Part 2 · Data Types, Input/Output and Operators
- Lesson 6: Data Types in C: Choosing the Right Box for Every Value
- Lesson 7: Formatted Input and Output in C: Taking Control of printf and scanf
- Lesson 8: Practice Exercise Sheet: Data Types in C
- Lesson 9: Operators and Expressions in C: Assignment, Unary, and Arithmetic
- Lesson 10: Relational, Logical, and Conditional Operators in C
- Lesson 11: Bitwise and Special Operators in C
- Lesson 12: Operator Precedence and Associativity in C
Part 3 · Decisions and Loops
- Lesson 13: Decision and Case Control: if and if-else in C
- Lesson 14: The switch Statement — and switch vs. if-else Ladder
- Lesson 15: The for Loop: Definite Repetition in C
- Lesson 16: while and do-while Loops: Indefinite Repetition in C
- Lesson 17: break and continue in C: Controlling a Loop from the Inside
- Lesson 18: The goto Statement in C: Jumping Around Your Program
Part 4 · Arrays
- Lesson 19: 1-D Arrays in C: Declaration, Address Calculation, Insertion and Deletion
- Lesson 20: Two-Dimensional Arrays in C: Declaration, Initialization and Accessing Elements
- Lesson 21: Matrix Addition and Matrix Multiplication in C
Part 5 · Strings
- Lesson 22: Strings in C: Working with Character Arrays
- Lesson 23: 1-D and 2-D Character Arrays in C: Storing Names and Words
Part 6 · Functions and Scope
- Lesson 24: Functions in C: Declaration and Definition
- Lesson 25: Categories of Functions in C
- Lesson 26: Scope of Variables in C
- Lesson 27: Call by Value in C: How Parameter Passing Works
- Lesson 28: Passing Arrays to Functions in C: Search, Sum and Sort