C++ User-defined Function Types

 C++ allows the programmer to define their own function.

C++ Functions: A function is that part of the code, which contains a certain set of statements, which yield a certain result, on giving different inputs. A function is something, which gives us results when it is called. There are various types of functions and can be user-defined as well. Functions can be simply considered as a machine, which takes some input, and gives us some output according to the function definition.

This article is all about the C++ User-defined functions and their types. So if you don’t know about the Functions in C++ then check out the following article C++ Functions.

A user-defined function is a group of code to perform a specific task and that group of code is given a name called an identifier.

The user-defined function makes programmer build their own functions. The most important thing behind these functions is the programmer can create applications with reusable code.

When the function is invoked from any part of the program, it all executes the codes defined in the function’s body.

There are four types of user-defined functions:

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

Let us understand them one by one.

  • Function with no arguments and no return value: – Function with no argument and no return type does not return value because its return type is void. In this type of function, there are no parameters passed in the function and the function also does not return any value from the function call to the main function.

Code:

C++ Code

#include<bits/stdc++.h>
using namespace std;

// Return type of function is void because value is not returned.
void is_even() {

  int num=4;
  if (num % 2 == 0) {
    cout << num << " is Even number.";
  } else {
    cout << num << " is Odd number.";
  }
}

int main() {
  // No argument is passed to is_even()
  is_even();
 // is_even() does not returns any value.
  return 0;
}

Output: 4 is Even number.

In the above program, the is_even() function is not taking any value as an argument and also it does not return any value.

  • Functions with no arguments and a return value: – In the program given below, is_even() function is called from the main() with no arguments. is_even() takes a positive integer from the user. Since the return type of the function is a boolean, it either returns true or returns false back to the calling main() function depending upon the input given by the user.

Code:

C++ Code

#include<bits/stdc++.h>
using namespace std;

// Return type of function is bool so it will either return ture of false.
bool is_even() {

  int num=4;

  if (num % 2 == 0) {
    return true;
  } else {
    return false;
  }
}

int main() {
  // No argument is passed to is_even()
  bool ans;
  ans = is_even(); // is_even return the boolean value.
  if (ans) {
    cout << "Num is even number";
  } else {
    cout << "Num is odd number";
  }
  return 0;
}

Output: Num is even number

  • Function with arguments and no return value: – In the below program, a positive number is first asked from the user which is stored in the variable num. Then, the num is passed to the is_even() function where, whether the number is even or not is checked and printed. Since the return type of is_even() is a void, no value is returned from the function.

Code:

C++ Code

#include<bits/stdc++.h>
using namespace std;

// Return type of function is void so it will not return any value.
void is_even(int num) {

  if (num % 2 == 0) {
    cout << "Num is even number";
  } else {
    cout << "Num is odd number";
  }
}

int main() {
  // Number as an argument is passed to is_even()
  int num=4;
  is_even(num);
  return 0;
}

Output: Num is even number

  • Function with arguments and a return value: – In the below program, a positive integer is asked from the user and stored in the variable num. Then, num is passed to the function is_even() where, whether the number is even or not is checked. Since, the return type of is_even() is a bool, “true” or “false” is returned to the main() calling function. If the number is an even number, “true” is returned. If not, “false” is returned. Back in the main() function, the returned boolean value “true” or “false” is stored in the variable flag, and the corresponding condition is printed onto the screen.

Code:

C++ Code

#include<bits/stdc++.h>
using namespace std;

// Return type of function is bool so it either return a true of false.
bool is_even(int num)
{

    if (num % 2 == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

int main()
{
    // Number as an argument is passed to is_even()
    int num=4;
    bool flag;
    
    flag = is_even(num);
    if(flag)
    {
        cout << "Num is even number";
    }
    else
    {
        cout << "Num is odd number";
    }
    return 0;
}

Output: Num is even number

Special thanks to Abhishek Yadav for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article