Skip to content
StudyFame

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.

TypeTakes arguments?Returns a value?Prototype example
1NoNovoid sum(void);
2YesNovoid sum(int a, int b);
3NoYesint sum(void);
4YesYesint 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

  • void before the name → no return value.
  • (void) or empty brackets → no arguments.
  • A type like int before 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

  1. sum1(): the numbers are fixed inside, and it prints the answer itself. main() gives nothing and gets nothing.
  2. sum2(10, 20): main() sends the numbers, but sum2 prints the answer itself and returns nothing.
  3. sum3(): takes nothing, but returns 30 so main() can print or use it.
  4. sum4(10, 20): takes the numbers and returns the answer. main() is in full control.

How Data Flows in Each Type

Typemain() → functionfunction → main()Flexibility
1NothingNothingLowest: does one fixed job
2DataNothingCan work on any data, but only prints
3NothingResultResult can be reused, but input is fixed
4DataResultHighest: 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.
  • void return = 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

MistakeWhat happensFix
x = sum1(); where sum1 is voidError: void value not ignoredUse a function that returns a value
Returning a value from a void functionError or warningChange the return type, or remove the value
Not using the returned valueThe result is lostStore it or print it
Printing inside every functionResults can't be reusedReturn the result; print in main()

Types of Functions Practice Questions

  1. Name the four types of user-defined functions.
  2. Write a prototype for a function that takes no arguments and returns a float.
  3. Which type of function is void show(int n);?
  4. Why is the "arguments and return value" type the most flexible?
  5. What happens if you try to store the result of a void function in a variable?
Show answers
  1. No arguments no return; arguments no return; no arguments with return; arguments with return.
  2. float f(void);
  3. Type 2: takes arguments, returns no value.
  4. It works on any input given by the caller and gives back a result that the caller can reuse.
  5. 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)

  1. int getValue(void); is which type?

    A) No arguments, no return   B) Arguments, no return   C) No arguments, return   D) Arguments and return

  2. void display(void); is which type?

    A) Type 1   B) Type 2   C) Type 3   D) Type 4

  3. 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

  4. The most flexible type is:

    A) No arguments, no return   B) Arguments and return   C) No arguments, return   D) Arguments, no return

  5. void as a return type means:

    A) Returns 0   B) Returns nothing   C) Returns any type   D) Error

Show answers
  1. C: no arguments, returns a value.
  2. A: Type 1.
  3. B: functions that return a value.
  4. B: arguments and return value.
  5. B: returns nothing.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

Type 1No args, no return: void f(void)
Type 2Args, no return: void f(int a)
Type 3No args, return: int f(void)
Type 4Args and return: int f(int a)
Most usedType 4
void returnNothing 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.