Types of User-defined Functions in C++

functions

You’ll discover various methods in this tutorial for using functions to solve a single problem.

User-defined functions can be categorized into the following groups for easier understanding of arguments and return in functions:

  1. Function with no argument and no return value
  2. Function with no argument but return value
  3. Function with argument but no return value
  4. Function with argument and return value

Consider a situation in which you have to calculate the area of the square. This problem is solved below by making a user-defined function in 4 different ways as mentioned above.

Example 1: No arguments passed and no return value

Without an argument, a function is called without passing any data to it, and similarly, a function without a return value is called without passing any data to it. As a result, no data is transferred from the calling function to the called function.

/* program to calculate the area of square */
#include <iostream>
using namespace std;
void area();   //function prototype
int main()
{
  area();    //function call
  return 0;
}
void area()
{
  int square_area,square_side;
  cout<<"Enter the side of square :";
  cin>>square_side;
  square_area = square_side * square_side;
  cout<<"Area of Square = "<<square_area;
}
Output:
Enter the side of square :6
Area of Square = 36

EXPLATION:

The area() function in the program above calculates area and having no arguments are passed to this function. This function’s return type is void, so it has no output.

Example 2: No arguments passed but a return value

As was previously stated, a function with no arguments means that the called function does not receive any data from the calling function, and a function with a single return value means that the called function will only return one result to the caller.

#include <iostream>
using namespace std;
int area();   //function prototype with return type int
int main()
{
  int square_area;
  square_area = area();    //function call
  cout<<"Area of Square = "<<square_area;
  return 0;
}
int area()
{
  int square_area,square_side;
  cout<<"Enter the side of square :";
  cin>>square_side;
  square_area = square_side * square_side;
  return square_area;
}
Output:
Enter the side of square :6
Area of Square = 36

EXPLATION:

No arguments are passed to this function’s int area(), but it still returns an integer value called square area.

Example 3: Function with Arguments but no return value

The function in this case will accept arguments and data from the calling function, but since there is no return type, nothing will be returned to the calling program. Therefore, communication is one-way.

#include <iostream>
//geektocode.com
using namespace std;
void area( int square_side);  //function prototype
int main()
{
  int square_side;
  cout<<"Enter the side of square :";
  cin>>square_side;
  area(square_side);   //function call
  return 0;
}
void area(int square_side)
{
  int square_area;
  square_area = square_side * square_side;
  cout<<"Area of Square = "<<square_area;
}
Output:
Enter the side of square :6
Area of Square = 36

EXPLATION:

The integer value entered by the user in the square_side variable is passed to area() in this function. The called function does not return anything because its return type is void.

Example 4: Function with Arguments but no return value

The calling function and the called function will exchange data if the function has arguments and only one return value. It resembles two-way communication.

#include <iostream>
using namespace std;
int area(int square_side);   //function prototype with return type int
int main()
{
  int square_area,square_side;
  cout<<"Enter the side of square :";
  cin>>square_side;
  square_area = area(square_side);    //function call
  cout<<"Area of Square = "<<square_area;
  return 0;
}
int area(int square_side)
{
  int square_area;
  square_area = square_side * square_side;
  return square_area;
}
Output:
Enter the side of square :6
Area of Square = 36

Which method is BETTER?

The output from the four programs mentioned above is the same, and they are all technically sound programs.

Which approach should be used is not subject to any strict guidelines.

Depending on the circumstance and the desired outcome, a specific approach is chosen.

Leave a Reply

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