User Defined Functions Types in C

In this lesson, you will learn about different types of User-Defined functions in C. We will create four programs that provide the same output, but we will take a different approach in each example.

1 – No arguments passed, and no return value

Let’s create a simple program that lets the user input two numbers and then returns the sum. Everything, including the output, will be performed in the function addTheNumbers(). It would look like this:

#include <stdio.h>

int A, B;

int main() {
  addTheNumbers();

  return 0;
}

int addTheNumbers() {
  printf("Enter the first number: ");
  scanf("%d", & A);

  printf("Enter the second number: ");
  scanf("%d", & B);

  int sum;
  sum = A + B;

  printf("The sum is %d", sum);
}

The function takes input from the user and returns the sum of two numbers. Since the parentheses are empty, it means that function takes no arguments. Also, its return type is void, meaning that the function returns no values; rather, it prints the result directly to the console.

2 – No arguments passed, but the function returns a value

Now let’s take a look at a different approach to solving the same problem. In this case, the function will serve only to calculate the sum and then return the value.

#include <stdio.h>

int A, B;

int main() {
  printf("Enter the first number: ");
  scanf("%d", & A);

  printf("Enter the second number: ");
  scanf("%d", & B);

  printf("The sum is %d", addTheNumbers());

  return 0;
}

int addTheNumbers() {
  int sum;
  sum = A + B;

  return sum;
}

Again, the empty parentheses in addTheNumbers() mean that we passed no arguments to the function. However, this time the return type is int because the function returns the sum, which is also int.

3 – An argument passed, but no return value

This time, we passed the values that the user entered, so the function statement is addTheNumbers(A, B);

Also, since the function returns no value, its return type is void again.

#include <stdio.h>

int A, B;

int main() {
  printf("Enter the first number: ");
  scanf("%d", & A);

  printf("Enter the second number: ");
  scanf("%d", & B);

  addTheNumbers(A, B);

  return 0;
}

void addTheNumbers(int A, int B) {
  int sum;
  sum = A + B;

  printf("The sum is %d", sum);
}

4 – An argument passed, and the function returns a value

Finally, we have a function that takes arguments and returns a value. We passed the numbers as arguments and called the function within the printf function. Since its return type is int, the printf function will also process it as an int.

#include <stdio.h>

int A, B;

int main() {
  printf("Enter the first number: ");
  scanf("%d", & A);

  printf("Enter the second number: ");
  scanf("%d", & B);

  printf("The sum is %d", addTheNumbers(A, B));

  return 0;
}

int addTheNumbers(int A, int B) {
  int sum;
  sum = A + B;

  return sum;
}

Which approach is the best?

There is no best approach in general. It depends mainly on what kind of problem you need to solve. In this case, using the 2nd approach provides you with the cleanest and most straightforward piece of code.

Back to: Learn C Programming > Functions in C

Leave a Reply

Your email address will not be published. Required fields are marked *