Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C function argument and return values
In C programming, functions can be categorized based on two criteria: whether they accept arguments (parameters) and whether they return a value. This gives us four distinct types of functions that cover all possible combinations.
Syntax
return_type function_name(parameter_list) {
// Function body
return value; // Optional, depends on return_type
}
Types of Functions
- Function with no arguments and no return value − Takes no input and returns nothing (void)
- Function with no arguments but returns a value − Takes no input but returns a value
- Function with arguments but no return value − Takes input parameters but returns nothing (void)
- Function with arguments and return value − Takes input parameters and returns a value
Example 1: No Arguments, No Return Value
This function takes no parameters and returns nothing −
#include <stdio.h>
void my_function() {
printf("This is a function that takes no argument, and returns nothing.");
}
int main() {
my_function();
return 0;
}
This is a function that takes no argument, and returns nothing.
Example 2: No Arguments, Returns Value
This function takes no parameters but returns an integer value −
#include <stdio.h>
int my_function() {
printf("This function takes no argument, but returns 50<br>");
return 50;
}
int main() {
int x;
x = my_function();
printf("Returned Value: %d", x);
return 0;
}
This function takes no argument, but returns 50 Returned Value: 50
Example 3: Takes Arguments, No Return Value
This function accepts an integer parameter but returns nothing −
#include <stdio.h>
void my_function(int x) {
printf("This function is taking %d as argument, but returns nothing", x);
}
int main() {
int x = 10;
my_function(x);
return 0;
}
This function is taking 10 as argument, but returns nothing
Example 4: Takes Arguments and Returns Value
This function accepts an integer parameter and returns its squared value −
#include <stdio.h>
int my_function(int x) {
printf("This will take an argument, and will return its squared value<br>");
return x * x;
}
int main() {
int x, res;
x = 12;
res = my_function(x);
printf("Returned Value: %d", res);
return 0;
}
This will take an argument, and will return its squared value Returned Value: 144
Key Points
- Use
voidas return type when function doesn't return any value - Empty parentheses
()indicate no parameters - Functions with return values must use
returnstatement
Conclusion
Understanding these four function types helps in designing modular C programs. Choose the appropriate type based on whether your function needs input data and whether it should produce output for the calling code.
