Site icon Codeforcoding

Type of user defined function in Cpp language

User defined function in C programming language

C language user-defined function

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:

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
Exit mobile version