Types of User-Defined Functions in C
A function can take inputs or not, and it can give back a result or not. Combining these two choices gives 4 types of user-defined functions.
"Explain the categories of functions with examples" is a very common exam question. In this lesson you will see the same task, adding two numbers, written in all 4 ways, so the differences are crystal clear.
What You Will Learn
- The 4 types (categories) of user-defined functions.
- How data flows in and out of each type.
- One program written in all 4 ways.
- Which type is best in real programs, and why.
Types of User-Defined Functions in C
Exam definition: Based on arguments and return value, user-defined functions are of four types: (1) no arguments, no return value; (2) arguments, no return value; (3) no arguments, return value; (4) arguments and return value.
| Type | Takes arguments? | Returns a value? | Prototype example |
|---|---|---|---|
| 1 | No | No | void sum(void); |
| 2 | Yes | No | void sum(int a, int b); |
| 3 | No | Yes | int sum(void); |
| 4 | Yes | Yes | int sum(int a, int b); |
Memory trick: Think of a shop. Type 1: you give nothing, get nothing back (the shop just shows an ad). Type 2: you give money, get nothing back (a donation). Type 3: you give nothing, get something (a free sample). Type 4: you give money, get a product (a real purchase, the most useful one).
How to recognise each type
voidbefore the name → no return value.(void)or empty brackets → no arguments.- A type like
intbefore the name → returns a value. - Variables inside the brackets → takes arguments.
Syntax of the Four Types
void f1(void); /* 1: no arguments, no return */
void f2(int a, int b); /* 2: arguments, no return */
int f3(void); /* 3: no arguments, return */
int f4(int a, int b); /* 4: arguments and return */
Example 1: Sum of Two Numbers Written in All 4 Types
#include <stdio.h>
/* Type 1: no arguments, no return value */
void sum1(void)
{
int a = 10, b = 20;
printf("Type 1: %d\n", a + b);
}
/* Type 2: arguments, no return value */
void sum2(int a, int b)
{
printf("Type 2: %d\n", a + b);
}
/* Type 3: no arguments, return value */
int sum3(void)
{
int a = 10, b = 20;
return a + b;
}
/* Type 4: arguments and return value */
int sum4(int a, int b)
{
return a + b;
}
int main(void)
{
sum1();
sum2(10, 20);
printf("Type 3: %d\n", sum3());
printf("Type 4: %d\n", sum4(10, 20));
return 0;
}
Output
Type 1: 30 Type 2: 30 Type 3: 30 Type 4: 30
Code Explanation
- sum1(): the numbers are fixed inside, and it prints the answer itself.
main()gives nothing and gets nothing. - sum2(10, 20):
main()sends the numbers, but sum2 prints the answer itself and returns nothing. - sum3(): takes nothing, but returns 30 so
main()can print or use it. - sum4(10, 20): takes the numbers and returns the answer.
main()is in full control.
How Data Flows in Each Type
| Type | main() → function | function → main() | Flexibility |
|---|---|---|---|
| 1 | Nothing | Nothing | Lowest: does one fixed job |
| 2 | Data | Nothing | Can work on any data, but only prints |
| 3 | Nothing | Result | Result can be reused, but input is fixed |
| 4 | Data | Result | Highest: any input, reusable result |
Type 4 is the most common in real programs: the function does only the calculation, and the caller decides what to do with the result (print it, store it, use it in another calculation).
Example 2: Why Type 4 is the Most Useful
#include <stdio.h>
int sum4(int a, int b)
{
return a + b;
}
int main(void)
{
int total = sum4(sum4(10, 20), sum4(5, 5)); /* reuse the results */
printf("(10+20) + (5+5) = %d\n", total);
if (sum4(3, 4) > 5)
printf("3 + 4 is greater than 5\n");
return 0;
}
Output
(10+20) + (5+5) = 40 3 + 4 is greater than 5
Because sum4 returns its answer, we can put it inside another call or a condition. You can't do that with sum1 or sum2, which only print.
Important Points to Remember
- 4 types based on arguments and return value.
voidreturn = no value comes back.(void)parameters = no arguments.- Type 4 (arguments + return) is the most flexible and most used.
- A function that returns a value can be used inside expressions.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
x = sum1(); where sum1 is void | Error: void value not ignored | Use a function that returns a value |
| Returning a value from a void function | Error or warning | Change the return type, or remove the value |
| Not using the returned value | The result is lost | Store it or print it |
| Printing inside every function | Results can't be reused | Return the result; print in main() |
Types of Functions Practice Questions
- Name the four types of user-defined functions.
- Write a prototype for a function that takes no arguments and returns a float.
- Which type of function is
void show(int n);? - Why is the "arguments and return value" type the most flexible?
- What happens if you try to store the result of a void function in a variable?
Show answers
- No arguments no return; arguments no return; no arguments with return; arguments with return.
float f(void);- Type 2: takes arguments, returns no value.
- It works on any input given by the caller and gives back a result that the caller can reuse.
- A compile error, because a void function returns no value.
Coding Exercise
Write a Type 4 function float percentage(int marks, int total) and use it to print the percentage for 432 out of 500.
Show solution
#include <stdio.h>
float percentage(int marks, int total)
{
return (float)marks / total * 100;
}
int main(void)
{
printf("Percentage = %.2f%%\n", percentage(432, 500));
return 0;
}
Percentage = 86.40%
Quick Quiz: Types of User-Defined Functions (MCQ)
-
int getValue(void);is which type?A) No arguments, no return B) Arguments, no return C) No arguments, return D) Arguments and return
-
void display(void);is which type?A) Type 1 B) Type 2 C) Type 3 D) Type 4
-
Which type can be used inside an expression like
x = f(2) + 1?A) Only void functions B) Functions that return a value C) None D) All
-
The most flexible type is:
A) No arguments, no return B) Arguments and return C) No arguments, return D) Arguments, no return
-
voidas a return type means:A) Returns 0 B) Returns nothing C) Returns any type D) Error
Show answers
- C: no arguments, returns a value.
- A: Type 1.
- B: functions that return a value.
- B: arguments and return value.
- B: returns nothing.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Type 1 | No args, no return: void f(void) |
|---|---|
| Type 2 | Args, no return: void f(int a) |
| Type 3 | No args, return: int f(void) |
| Type 4 | Args and return: int f(int a) |
| Most used | Type 4 |
| void return | Nothing comes back |
Frequently asked questions
- On what basis are functions classified into 4 types?
- On whether they take arguments and whether they return a value.
- Which type is best and why?
- Arguments + return value: it works on any input and its result can be reused.
- What does f(void) mean in C?
- The function takes no arguments.
Remember
- Shop trick: ad, donation, free sample, real purchase.
- Return the result, print it in main().
Next Topic
Next lesson: Call by Value vs Call by Reference. You will learn the two ways of passing arguments: by value and by reference.