C++ Oop

Type of user defined function in Cpp language

Type of user defined function in C++ language

In this tutorial, we will discuss Type of user defined function in C++ language

Already, we would learn about Type of user-defined function in C language.

Now we get to know Type of user-defined function in C++ language.

The user defines functions are categorized are:

  • Function with no argument and no return value
  • Function with no argument and with the return value
  • Function with  argument and no return value
  • Function with  argument and return value

Examples

  1. void add() //No return without argument
  2. void sub(int,int)//No return argument
  3. int mul() //Return without argument
  4. float div(int ,int) //Return with argument

1. Function with no argument and no return value

#include <iostream>
#include <conio.h>
using namespace std;
void add();
int main()
{

    add();
    getch();
    return 0;
}
void add()//No arguments are passed to this function
{//void keyword never return value from function
    cout << "Function with no argument and no return value "<<"\n" << endl;
int num1,num2;
cout<<"Enter the number for num1: ";
cin>>num1;  //get input from user for num1
cout<<"Enter the number for num2: ";
cin>>num2;  //get input from user for num2
int sum=num1+num2;  //find total value
cout<<"Total value is: "<<sum<< endl;


}

When the above code is compiled and executed, it produces the following results

Function with no argument and no return value

Enter the number for num1: 67
Enter the number for num2: 54
Total value is : 121

In the above program, initially, two integer value is got from the user and stored in the variable num1,num2

Then, num1, num2 values passed to the function add().

add() is a user-defined function for add two numbers. it is called from the main function without arguments

2. Function with no argument and with the return value

#include <iostream>
#include <conio.h>
using namespace std;
int sub();
int main()
{
    cout<<"Function with no argument but return value"<<"\n"<<endl;
    int a,b;
    int result=a-b;
    cout<<"Different is :"<<sub();



getch();
    return 0;
}
int sub()//no arguments are passed to this function
{   //int keyword returns value
int num1,num2;
cout<<"Enter the number for num1 :";
cin>>num1;//get input from user for num1
cout<<"Enter the number for num2 :";
cin>>num2; //get input from user for num2
return num1-num2;


}

When the above code is compiled and executed, it produces the following results

Function with no argument but return value

Enter the number for num1: 67
Enter the number for num2: 45
Different is: 22

In the above program, initially, two integer value is got from the user and stored in the variable num1,num2

Then, num1, num2 values passed to the function sub().

sub() a user-defined function for subtraction two numbers. it is called from the main function without arguments but return a value

3. Function with argument and without a no return value

#include <iostream>
#include <conio.h>
using namespace std;
void mul(int a,int b);
int main()
{
    int num1;
    int num2;
     int multi;
    cout << "Function with argument no return value" << endl;
    cout<<"Enter the number for num1: ";
    cin>>num1; //get input from user for num1
     cout<<"Enter the number for num2: ";
     cin>>num2; //get input from user for num2
    mul(num1,num2); //passing argument to mul() function
    getch();
    return 0;
}
void mul(int a, int b)//arguments are passed this function
{
    cout<<"Multiply is: "<<a*b<<endl;         //void no return value
}

When the above code is compiled and executed, it produces the following results

Function with an argument no return value
Enter the number for num1: 12
Enter the number for num2: 23
multiply is: 276

In the above program, initially, two integer value is got from the user and stored in the variable num1,num2

Then, num1, num2 values passed to the function mul().

mul() a user-defined function for multiply two numbers. it is called from the main function with arguments and no return value

 

4. Function with argument and  return value

#include <iostream>
#include <conio.h>
using namespace std;
int addition(int a,int b);
int main()
{
    int num1;
    int num2;
    int add;
    cout << "Function with argument return value" << endl;
    cout<<"Enter the number for num1: ";
    cin>>num1; //get input from user for num1
     cout<<"Enter the number for num2: ";
     cin>>num2; //get input from user for num2
    add=addition(num1,num2);//find total value
    cout<<"Addition is: "<<add<<endl;
    getch();
    return 0;
}
int addition(int a, int b)//arguments are passed this function
{
    return(a+b);         //return value
}

When the above code is compiled and executed, it produces the following results

Function with argument and return value
Enter the number for num1: 45
Enter the number for num1: 56
Addition is: 101

In the above program, initially, two integer value is got from the user and stored in the variable num1,num2

Then, num1, num2 values passed to the function add().

add() is a user-defined function for add two numbers. it is called from the main function with arguments and return value

 

Suggested for you

String function in C language

Arithmetic function in C language

Function in C language

User defined function in C language

Method in Java programming language with example
Python program to add two number using function
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago