4 Types of User-Defined Functions in C: Arguments & Return Values | Unit 3 | BKNMU BCA
BKNMU Junagadh | BCA Sem 1 | Unit 3
4 Types of User-Defined Functions in C Programming
Complete Classification based on Arguments Passed and Values Returned with Syntax & Code Examples
Based on the presence or absence of arguments and return values, functions are divided into:
1. Independent Routine
Takes no input from caller and returns nothing. Input, processing, and output display all happen entirely inside the function.
2. One-Way Input
Receives data parameters from caller, performs calculations, and prints results directly without sending values back.
3. Producer Routine
Receives no data from caller, collects user input or generates data internally, and sends computed output back to caller.
4. Complete Two-Way
Receives input arguments, executes logic independently, and returns computed result back to caller for further operations.
A complete side-by-side structural comparison of prototypes, data flow, and calling styles:
| Category Type | Prototype Pattern | Data Flow & Calling Syntax |
|---|---|---|
Type 1: No Arg, No Return |
void sum(void); |
No data transfer. Invoked simply as sum();. Function handles both input and output. |
Type 2: With Arg, No Return |
void sum(int, int); |
One-way data transfer into function. Invoked as sum(x, y);. Results printed inside. |
Type 3: No Arg, With Return |
int sum(void); |
One-way data transfer back to caller. Invoked as res = sum();. Input taken inside. |
Type 4: With Arg, With Return |
int sum(int, int); |
Two-way data transfer. Invoked as res = sum(x, y);. Pure modular computing block. |
- void Keyword: When used as return type, it means the function returns no value. When used in parameter list
(void), it signifies no incoming arguments. - Reusability Best Practice: Type 4 (With Arguments and Return Value) is considered the most modular and industry-standard approach because the function remains completely independent of I/O operations (scanf/printf).
Type 1: Function with No Arguments and No Return Value:
#include <stdio.h>
// Declaration
void add_Type1(void);
int main() {
add_Type1(); // Call without arguments
return 0;
}
void add_Type1(void) {
int a = 10, b = 20;
printf("Type 1 Sum: %d\n", a + b);
}
Type 2: Function with Arguments and No Return Value:
#include <stdio.h>
// Declaration
void add_Type2(int a, int b);
int main() {
int x = 15, y = 25;
add_Type2(x, y); // Passes actual arguments x and y
return 0;
}
void add_Type2(int a, int b) {
printf("Type 2 Sum: %d\n", a + b);
}
Type 3: Function with No Arguments and With Return Value:
#include <stdio.h>
// Declaration
int add_Type3(void);
int main() {
int result;
result = add_Type3(); // Receives returned integer
printf("Type 3 Sum received in main(): %d\n", result);
return 0;
}
int add_Type3(void) {
int a = 30, b = 40;
return (a + b); // Returns calculation result
}
Type 4: Function with Arguments and With Return Value:
#include <stdio.h>
// Declaration
int add_Type4(int a, int b);
int main() {
int num1 = 50, num2 = 50;
int total;
total = add_Type4(num1, num2); // Two-way communication
printf("Type 4 Total calculated: %d\n", total);
return 0;
}
int add_Type4(int a, int b) {
return (a + b);
}
Step 1: Check Input Dependency
Does the sub-routine require external variables from main()? If YES, choose a function with arguments.
Step 2: Check Output Dependency
Does the caller need the calculated result for further operations? If YES, declare a function with return value.
Step 3: Select Appropriate Category
Combine your findings to implement Type 1, Type 2, Type 3, or Type 4 according to program design requirements.
int) but lacks a return statement, the compiler returns an unpredictable garbage value to the caller. Always pair non-void functions with an explicit return.
- 4 Categories: (1) No Arg, No Return, (2) With Arg, No Return, (3) No Arg, With Return, (4) With Arg, With Return.
- No Arg, No Return: Self-contained; performs input, computation, and display internally.
- With Arg, No Return: Caller supplies inputs; function displays output directly.
- No Arg, With Return: Function produces output and delivers it back to caller.
- With Arg, With Return: Full modularity; takes inputs from caller and returns result.