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

C++ program to multiply two numbers using function

Java program to multiply two numbers using method

Python program to multiply two numbers using function

C# program to multiply two numbers using function

PHP program to multiply two numbers using function

 

Operator in Java

Data types and variable in Java

Find product of two numbers using recursion in Java

Data types and variable in Java language

 

String function in C language

Arithmetic function in C language

Function in C language

User defined function in C language

Java method

input output function in C

 

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

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago