Function Types in C++


Types Of Function in C++

Four types of Function based on argument and return type

  • Function - no argument and no return value
  • Function - no argument but return value
  • Function - argument but no return value
  • Function - argument and return value

Function Types Diagram Representation

Function Types In C++

Syntax

// Function Declaration - no argument and no return value
void funtion1(); 

// Function Declaration - no argument but return value
int funtion2(); 

// Function Declaration - argument but no return value
void funtion3(int a,int b); 

// Function Declaration - argument but return value
int funtion4(int a,int b); 

Example Program

//Simple Function Types Example Program in C++
//Function Example

#include<iostream>

using namespace std;

void funtion1(); // Function Declaration - no argument and no return value
int funtion2(); // Function Declaration - no argument but return value
void funtion3(int a, int b); // Function Declaration - argument but no return value
int funtion4(int a, int b); // Function Declaration - argument but return value

int main() {
   int x, y;

   cout << "Simple Function Types Example Program in C++\n";
   // Function Call - no argument and no return value
   funtion1();

   // Function Call - no argument but return value
   x = funtion2();
   cout << "\nReturn Value : " << x;

   // Function Call - no argument and no return value
   funtion3(10, 20);

   // Function Call - no argument but return value
   y = funtion4(100, 200);
   cout << "\nAddition : " << y;

}

void funtion1() // Function Definition - no argument and no return value
{
   cout << "\nIts simple function definition";
}

int funtion2() // Function Definition - no argument but return value
{
   int c = 1000;
   return c;
}

void funtion3(int a, int b) // Function Definition - argument but no return value
{
   int c;
   c = a + b;
   cout << "\nAddition : " << c;
}

int funtion4(int a, int b) // Function Definition - argument but return value
{
   int c;
   c = a + b;
   return c;
}

Sample Output

Simple Function Types Example Program in C++

Its simple function definition
Return Value : 1000
Addition : 30
Addition : 300
--------------------------------