Types of User Defined Functions in C

In this article, I am going to discuss the Types of User-Defined Functions in C Language with examples. Please read our previous articles, where we discussed the Functions in C Language with Examples. There are four types of user-defined functions in C. They are as follows:

  1. Functions with No Argument and No Return Type.
  2. Functions with Argument and No Return Type.
  3. Functions with No Argument and with Return Type.
  4. Functions with Argument and with Return Type

Let us understand each of these function types with examples.

No Arguments Passed and No Return Value Function in C Language:

When a function has no arguments, it does not receive any data from the calling function. Similarly, when it does not return a value, the calling function does not receive any data from the called function. In effect, there is no data transfer between the calling function and the called function. A function that does not return any value cannot be used in an expression. It can only be used as an independent statement.

Example:
int main()
{
      Sum();
}
Sum()
{
      int x=10, y=20, z;
      z=x+y;
      printf(“%d”,z);
}
Program to Understand No Arguments Passed and No Return Value Function in C Language:
#include <stdio.h>
void checkPrimeNumber();
int main()
{
    checkPrimeNumber(); // argument is not passed
    return 0;
}
// return type is void meaning doesn't return any value
void checkPrimeNumber()
{
    int n, i, flag = 0;
    printf("Enter a positive integer: ");
    scanf("%d",&n);
    for(i=2; i <= n/2; ++i)
    {
        if(n%i == 0)
        {
            flag = 1;
        }
    }
    if (flag == 1)
        printf("%d is not a prime number.", n);
    else
        printf("%d is a prime number.", n);
}
Output:
Program to Understand No Arguments Passed and No Return Value Function in C Language

The checkPrimeNumber() function takes input from the user, checks whether it is a prime number or not, and displays it on the screen. The empty parentheses in checkPrimeNumber(); statement inside the main() function indicates that no argument is passed to the function. The return type of the function is void. Hence, no value is returned from the function.

No Arguments Passed but Return a Value Function in C Language:

When a function has no arguments, it does not receive any data from the calling function, but it returns a value, the calling function receives the data from the called function. In effect, there is no data transfer between the calling function to called function and data transfer from the called function to the calling function. The called function is executed line by line in a normal fashion until the return statement is encountered.

Example:
int main()
{
      int a;
      a=Sum();
}
Sum()
{
      int x, y, z;
      x=10;
      y=20;
      z=x+y;
      return(z);
}

Note: If the function returns non-integer, we have the responsibility to declare the function before it is called. In the above example, function sum returns z value to called function. When the called function returns a value, the calling function must store the return value in a variable. In the above example calling function receives z value returned by called function and assigned to variable ‘a’.

Program to Understand No Arguments Passed but Return a Value Function in C Language
#include <stdio.h>
int getInteger();
int main()
{
    int n, i, flag = 0;
    // no argument is passed
    n = getInteger();
    for(i=2; i<=n/2; ++i)
    {
        if(n%i==0)
        {
            flag = 1;
            break;
        }
    }
    if (flag == 1)
        printf("%d is not a prime number.", n);
    else
        printf("%d is a prime number.", n);
return 0;
}
// returns integer entered by the user
int getInteger()
{
    int n;
    printf("Enter a positive integer: ");
    scanf("%d",&n);
    return n;
}
Output:
Program to Understand No Arguments Passed but Return a Value Function in C Language

The empty parentheses in the n = getInteger(); statement indicates that no argument is passed to the function. And, the value returned from the function is assigned to n. Here, the getInteger() function takes input from the user and returns it. The code to check whether a number is prime or not is inside the main() function.

Argument Passed but no Return Value Function in C Language:

The nature of data communication between the calling function and the called function with arguments but no return value is shown in the figure.

Program to Understand Argument Passed but no Return Value Function in C Language
#include <stdio.h>
void checkPrimeAndDisplay(int n);
int main()
{
    int n;
    printf("Enter a positive integer: ");
    scanf("%d",&n);
    // n is passed to the function
    checkPrimeAndDisplay(n);
    return 0;
}
// return type is void meaning doesn't return any value
void checkPrimeAndDisplay(int n)
{
    int i, flag = 0;
    for(i=2; i <= n/2; ++i)
    {
        if(n%i == 0)
        {
            flag = 1;
            break;
        }
    }
    if(flag == 1)
         printf("%d is not a prime number.",n);
    else
         printf("%d is a prime number.", n);
}

Output:
Program to Understand Argument Passed but no Return Value Function in C Language

The integer value entered by the user is passed to the checkPrimeAndDisplay() function. Here, the checkPrimeAndDisplay() function checks whether the argument passed is a prime number or not and displays the appropriate message.

What are Actual and Formal Arguments

We should ensure that the function call has matching arguments. In case, the actual arguments are more than the formal arguments, the extra actual arguments are discarded. On the other hand, if the actual arguments are less the formal arguments, the unmatched formal arguments are initialized to some garbage value. Any mismatch in data type may also result in the passing of garbage values. Remember, no error message will be generated.

While the formal arguments must be valid variable names, the actual arguments may be variable names, expressions, or constants. The variables used in actual arguments must be assigned values before the function call in made.

When a function calls another function with arguments, only a copy of the values of actual arguments is passed into the called function. What occurs inside the function will have no effect on the variables used in the actual arguments list.

Argument Passed and Return Value Function in C Language:

A self-contained and independent function should behave like a “black-box” that receives an input and outputs a value. Such functions will have two-way data communication as shown below.

Example:
int main()
{
      int z;
      z=Sum(10,20);
      printf(“sum of two=%d”, z);
}
Sum(int x, int y)
{
       return(x+y);
}
Program to Understand Argument Passed and Return Value Function in C Language
#include <stdio.h>
int checkPrimeNumber(int n);
int main()
{
    int n, flag;
    printf("Enter a positive integer: ");
    scanf("%d",&n);
    // n is passed to the checkPrimeNumber() function
    // the returned value is assigned to the flag variable
    flag = checkPrimeNumber(n);
    if(flag == 1)
        printf("%d is not a prime number",n);
    else
        printf("%d is a prime number",n);
    return 0;
}
// int is returned from the function
int checkPrimeNumber(int n)
{
    int i;
    for(i=2; i <= n/2; ++i)
    {
        if(n%i == 0)
            return 1;
    }
return 0;
}
Output:
Program to Understand Argument Passed and Return Value Function in C Language

The input from the user is passed to the checkPrimeNumber() function. The checkPrimeNumber() function checks whether the passed argument is prime or not. If the passed argument is a prime number, the function returns 0. If the passed argument is a non-prime number, the function returns 1. The return value is assigned to the flag variable. Depending on whether the flag is 0 or 1, an appropriate message is printed from the main() function.

Which Approach is Better?

Well, it depends on the problem you are trying to solve. In this case, passing arguments and returning a value from the function is better. A function should perform a specific task. The checkPrimeNumber() function doesn’t take input from the user nor it displays the appropriate message. It only checks whether a number is prime or not.

Do you know how many values can be returned from C functions?

Always, only one value can be returned from a function. If you try to return more than one value from a function, only one value will be returned that appears at the rightmost place of the return statement.

For example, if you use “return a,b,c” in your function, value for c only will be returned and values a, b won’t be returned to the program. In case, if you want to return more than one value, pointers can be used to directly change the values in the address instead of returning those values to the function.

Follow Us On