Type of Functions in C Programming Language

Spread the love
Type-of-Functions-in-C-Language-with-Example-Programs

Type of Functions in C Language Introduction:

In our previous article, We have looked at the introduction to C functions and how to create and call functions. In this article, We are going to look at the Type of Functions in C programming language.

Pre-Requisite:

We need to know the basics of the functions. So Please go through the following article to learn more

Different Types of Functions in C Language:

  1. Functions with no arguments and no return value
  2. Functions with arguments and no return value
  3. Functions with no arguments and with a return value
  4. Functions with arguments and with a return value

Let’s look at them one by one.

Type of Functions in C – Function with no Arguments and no Return value:

Here is the syntax of the function without arguments and without return values

If you look at the above syntax, The above function ( function_name)

  • Doesn’t have any arguments, We specified the argument_list as void. which means the function doesn’t accept any input values.
  • Similarly, we specified the return type as void. So the function doesn’t return any value back to the caller.

Let’s look at a couple of examples for the Functions with no arguments and no return value.

Example 1 – Greet Function: Function without Arguments and without Return value:

Here is a program with a greet function.

Here we have defined a function called greet. The greet function doesn’t take any input values (no arguments) and doesn’t return anything (no return type).

Here are the greet function details,

  • function_name: greet
  • arguments_list : void
  • return_value : void

So To call the greet function, We don’t need to pass any arguments, We can simply call it using greet()

Program Output:

Function-with-no-arguments-and-no-return-value-program-output

As you can see from the above output, The greet function displayed the message Welcome to SillyCodes and not returned any value.

Example 2: Sum of numbers Function with no Arguments and no Return value:

Let’s look at another program, Where we want to calculate the sum of the first 10 natural numbers and display the result.

Here we have created a function named sum_of_numbers. This function is also a function with no arguments and no return value. So don’t accept and return anything.

Here are the sum_of_numbers function details

  • function_name: sum_of_numbers
  • arguments_list : void
  • return_value : void

The function sum_of_numbers calculates the sum of natural numbers from 1 to 10 using for loop and displays the results.

📢 We have already covered the program to calculate the Sum of the first N natural number please go through the following program

Program Output:

As you can see from the output, The program calculates the sum of the first 10 numbers.

Type of Functions in C – Function with Arguments and no Return value:

Here is the syntax of the function with arguments and without return values

If you look at the above syntax,

  • The function has the argument_list i.e (datatype1 arg1, datatype2 arg2, ..), So the function accepts the input values from the caller.
    • Make sure to use the same datatypes you specified in the function declaration and function call.
  • But the above function doesn’t have a return type, We have specified the return type as void. So the function doesn’t return any value back to the caller.

Example 1: Calculate Sum of Two Numbers using Function with Arguments and without Return value:

Write a C Program to calculate the sum of two numbers and display the results.

Here we have defined a function ( add). which calculates and displays the sum of two integer numbers.

The add function is a function with two arguments and no return value. So add function accepts two integers as input from the caller but doesn’t return anything.

Here are the add function prototype details.

  • function_name: add
  • arguments_list : (int n1, int n2)
  • return_value : void

To call the add function, We need to pass two parameters (actual arguments) during the function call. Here is an example of add function call

add( 100, 200);

Here 100 and 200 are the actual arguments, Which are copied to the formal arguments of the add function

📢 Note, As we are calling the add function before defining it, So we have forward-declared the add function at the start of the program. void add(int n1, int n2);

Program Output:

Type-of-functions-in-c-Function-with-Arguments-and-no-Return-value

Example 2: Check Prime Number – Function with Arguments and no Return value:

C Program to check if the given number is a prime number.

The program accepts a number from the user and checks if it prime number using the function.

Here we have defined a function ( isPrime). which checks if the given number is a prime number or not.

The isPrime function is a function with a single argument and no return value. So isPrime function accepts an integer from the caller but doesn’t return anything.

Here are the isPrime function prototype details.

  • function_name: isPrime
  • arguments_list : (int n)
  • return_value : void

To call the program, Pass an integer variable like below.

isPrime(20);

or

isPrime(num1);

The isPrime function takes the num1 as input and calculates if the num1 is a prime number or not and displays the results on the console.

Program Output:

Type-of-functions-in-c-Function-with-Args-and-no-Return-value-program-output

Type of Functions in C – Function with no Arguments and with Return value:

Here is the syntax of the function without arguments and with return values

If you look at the above syntax,

  • The function doesn’t accept any arguments, As we specified the argument_list as void.
  • But the function does return a value. The has the return type as datatype. For example int, So it will return an Integer value to the caller.

Let’s look at a few examples of no-argument and return value functions.

Example 1: Calculate the product Numbers – Function with no Arguments and with Return value

Write a C Program Calculate the product of the first 5 (1 to 5) numbers and return the results.

Here we have defined a function ( prod). which calculates to the product of numbers from 1 to 5.

The prod function is a function without arguments and with a return value. So prod function doesn’t accept any arguments from the caller but returns an integer back.

Here are the prod function prototype details.

  • function_name: prod
  • arguments_list : void
  • return_value : int

To call the prod function, we use

result = prod();

The prod function calculates the product and returns the results and we are storing the results in result variable.

Program Output:

Function-with-no-args-and-with-return-value-in-c

Example 2: Sum of Odd Number – Function with no Arguments and with Return value:

In this program, We have created a function called sumOfOddNumber which takes no values but returns an integer, which is the sum of all Odd numbers from number 1 to 10.

Here are the sumOfOddNumbers function prototype details.

  • function_name: sumOfOddNumbers
  • arguments_list : int
  • return_value : void

Program Output:

Type-of-functions-in-c-programming-language

Type of Functions in C – Function with Arguments and with Return value:

Here is the syntax of the function with arguments and with return values

If you look at the above syntax,

  • The function has the argument_list i.e (datatype1 arg1, datatype2 arg2, ..), So the function accepts the input values from the caller.
    • Make sure to use the same datatypes you specified in the function declaration and function call.
  • The function also returns a value back to the caller. Here we have specified the return data type as datatype, It can be any valid C data type like int, float,etc.
    • If you use the int as the return type, Make sure to return the integer value from the function. Similarly, if you use the float datatype, return the float data.

Example 1 – Number of Digits – Function with Arguments and with Return value:

C Program to calculate the number of digits in a given number.

Here we have defined a function ( noOfDigits). which calculates the number of digits in a given number.

The noOfDigits function is a function with a single argument and a single return value. So noOfDigits function accepts an integer as input from the caller and returns an Integer back.

Here are the noOfDigits function prototype details.

  • function_name: noOfDigits
  • arguments_list : (int num)
  • return_value : int

Call the function using,

res = noOfDigits(number);

Here we are passing a number (i.e variable number) to the function during the function call, And the function returns an Integer variable, Which will be stored in the res variable.

Program Output:

Type-of-functions-in-c-Function-with-Args-and-with-Return-value-program

As we can see from the output, The program is calculating the number of digits in the given number and returning back to the main() function, Then we are displaying the results using the printf() function.

Example 2: Factorial of Number – Function with Arguments and With Return value:

Write a C Program to calculate the factorial of a number using functions.

Here we have defined a function named factorial to calculate the factorial of the given number.

The factorial function takes an integer value and calculates the factorial and returns the value(Integer) back to the main() function.

Here are the factorial function prototype details.

  • function_name: factorial
  • arguments_list : (int num)
  • return_value : int

To call the factorial function, We used

result = factorial(number);

Here the number is the user input and the main() function passed number to the factorial function, Once the factorial function is completed and returns the return value, It will be stored in the result variable.

Program Output:

Function-with-Args-and-with-Return-value-program-output

As you can see from the above output, The program is giving appropriate results.

Which type of function you should use?

It depends upon your requirement, If you need to send the data to the function and get the data back from the function, Then use the function with arguments and with the return type. Similarly, You don’t really need a return value but need to send some data to the function, Then use a function with arguments and without return type.

So Which type of function to use purely depends upon your use case.

Type of Functions in C Language Conclusion:

We have discussed about the Type of Functions in C language with example programs. We looked at the variables of functions like The function without arguments and without a return value, The function with arguments and without a return value, The function without arguments and with a return value, and The function with arguments and with a return value.

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] have looked at the Introduction to C Functions and Types of C functions in our earlier posts. In today’s article, We are going to look at Call by Value and Call by […]

Leave a Reply