Lesson 2 of 8 · Functions and Arrays · 3 min read · Updated
The four types of functions
Whether a function takes arguments, and whether it returns a value — the four combinations and when to use each.
Functions are usually grouped by two questions: does it take arguments, and does it return a value? The answers give four types. All four appear in one program below.
c
#include <stdio.h>
/* 1. No arguments, no return value */
void greet(void)
{
printf("Vanakkam!\n");
}
/* 2. Arguments, no return value */
void print_square(int n)
{
printf("%d squared is %d\n", n, n * n);
}
/* 3. No arguments, returns a value */
int current_year(void)
{
return 2026;
}
/* 4. Arguments and a return value */
int add(int a, int b)
{
return a + b;
}
int main(void)
{
greet();
print_square(6);
printf("Year: %d\n", current_year());
printf("add(3, 4) = %d\n", add(3, 4));
return 0;
}
Output
Vanakkam!
6 squared is 36
Year: 2026
add(3, 4) = 7
- No arguments, no return (
void f(void)) — does a fixed job: print a menu, a header, a greeting. - Arguments, no return (
void f(int x)) — does a job with your data and shows the result itself. - No arguments, returns (
int f(void)) — produces a value on its own: read a number from the user, give a setting. - Arguments and returns (
int f(int a, int b)) — the most useful kind: takes data, gives back an answer the caller can use.
Note
Prefer the fourth type where you can. A function that returns its answer can be reused anywhere — printed, stored, compared — while one that prints the answer itself can only print.
float average(int a[], int n) — which type is it?
Try this now
- Write one function of each type for a shop: print the shop name; print a bill line for a price; read a quantity; calculate a total from price and quantity.
- Change
print_squareintosquarethat returns the answer, and print the squares of 1 to 10 with it.
Create a free account to save your progress, or sign in.