Back to C Programming
2026-07-125 min read

Function with no arguments and no return value

Learn Function with no arguments and no return value step by step with clear examples and exercises.

Why This Matters

In this lesson, we will focus on a crucial aspect of C programming: functions with no arguments (also known as void functions) and no return value. Understanding how to define and use these functions is essential because they help keep your code cleaner, more modular, and prepare you for real-world programming scenarios where such functions are often employed.

Prerequisites

To follow along with this lesson, you should have a basic understanding of:

  1. C programming syntax
  2. Variables and data types
  3. Basic input/output operations (printf, scanf)
  4. Control structures (if-else, loops)
  5. Understanding the concept of functions and their basic usage in C
  6. Familiarity with the stdlib.h library for using functions like atoi() to convert strings to integers

Core Concept

A function without arguments or return value can be defined in C using the void keyword. This indicates that the function doesn't take any parameters and doesn't return a value. Here's an example of a simple void function:

void greet() {
printf("Hello, World!\n");
}

In this example, we define a function called greet. The keyword void indicates that the function doesn't return any value. Inside the curly braces {}, we have the code to be executed when the function is called. In this case, it's just a simple print statement.

To call or execute the function, you can use its name followed by parentheses:

int main() {
greet(); // Calling the greet function
return 0;
}

When the greet function is called within the main function, it executes the code inside the function, and the program outputs "Hello, World!".

Function with Multiple Statements

Functions can contain multiple statements. For example:

void displayMessage() {
printf("Welcome to our program!\n");
printf("This is a void function.\n");
}

In this example, the displayMessage function contains two print statements that are executed one after another when the function is called.

Worked Example

Let's create a more complex void function that calculates and prints the factorial of a number using recursion.

#include <stdio.h>

void factorial(int n, int result) {
if (n == 0 || n == 1) {
printf("Factorial of %d is: %d\n", n, result);
return;
}

result *= n;
factorial(n - 1, result);
}

int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);

if (num < 0) {
printf("Invalid input. Please enter a positive integer.\n");
return 1;
}

int result = 1; // Initialize result variable
factorial(num, result); // Calling the factorial function with initial value of 1 for 'result'
return 0;
}

In this example, we define a factorial function that takes two arguments: an integer n and an integer result. The function calculates the factorial using recursion. Inside the main function, we prompt the user to enter a positive integer and validate the input before calling the factorial function with the entered number and initializing the result variable to 1.

Common Mistakes

  1. Forgetting to include the void keyword when defining a function without return value. This will result in a compile-time error.
  2. Not passing any arguments to a void function when calling it. If you try to access undefined variables inside a void function, the program will fail at runtime.
  3. Not initializing variables before using them inside a void function. Uninitialized variables can lead to undefined behavior and potential crashes.
  4. Not handling invalid input properly. Failing to validate user input can cause the program to crash or behave unexpectedly.
  5. Not returning from the main function when the program is done executing. If you forget to return 0 from the main function, your program may not exit cleanly, and some operating systems will consider it as a hanging process.
  6. Using recursion in void functions without properly handling the base case. Recursive functions must have a base case that ends the recursion and prevents infinite loops.
  7. Not understanding the difference between void functions and functions with return values. Void functions don't return any value, while functions with return values return a specific data type (e.g., int, double).
  8. Not properly managing memory when using dynamic memory allocation inside void functions. If you allocate memory within a void function, make sure to free it before the function exits or in the calling function.

Practice Questions

  1. Write a void function that prints the current date and time using the C library functions time.h and localtime.
  2. Implement a void function that checks whether a given number is prime or not.
  3. Create a void function that displays a pattern like this:
*
**
**
**
**
  1. Write a void function that calculates the sum of all numbers from 1 to a given limit (passed as an argument).
  2. Implement a void function that finds the largest prime number among the first n natural numbers (passed as an argument).
  3. Create a void function that sorts an array of integers in ascending order using bubble sort algorithm.

FAQ

  1. Why can't I return a value from a void function?

A void function doesn't have a return type, so it cannot return any value to the caller. Instead, it performs some action or manipulates variables within its scope.

  1. Can I call a void function multiple times in my program?

Yes, you can call a void function as many times as needed throughout your program.

  1. How do I test a void function without returning a value?

You can use print statements or debugging tools to verify the behavior of a void function during execution.

  1. Can I define a void function inside another function in C?

Yes, you can define and call functions within other functions in C, but it's generally considered bad practice due to readability concerns. Instead, keep your functions separate and modular.

  1. What happens if I try to return a value from a void function?

If you try to return a value from a void function, the program will generate a compile-time error because the function is not declared to return any value.

  1. Can I use recursion in void functions?

Yes, you can use recursion in void functions, but make sure to handle the base case properly and manage memory if necessary.