C++ : User Define Function Example ( Part -2 ) in hindi , program examples of User Define Function in c++ language

इससे पहले के article मे , function के कुछ उदाहरानो को discuss किया था | एब function से based कुछ advance उदाहरनो  को discuss करेगे | इन उदाह्रानो को पहले खुद try करे और बाद मे इसके solution को देखे | जिससे आप सभी के c++ language के syntax और concept अच्छी तरह से समज सके |

उदाहरन 1 :
Write a program to make a calculator using function .
इस उदाहरण मे , user define function से  calculator को बनाया गया है |

Expalnation
सबसे पहले यूजर द्वारा choice को input किया जाता है |
Enter ‘+’ for addition
Enter ‘-‘ for subtraction
Enter ‘*’ for multiplication
Enter ‘/’ for division
उसके बाद यूजर से choice को input करा लेते है | और इसके बाद  यूजर से values को input करा लेते है |
choice को switch statement मे pass किया जाता है | उस आधार पर function को कैल्ल किया जाता है |

add() मे ,
add() function मे दो value को addition किया जाता  है | और आउटपुट की value को display किया जाता है |
sub() मे ,
sub () function मे दो value को  subtraction किया जाता  है | और आउटपुट की value को display किया जाता है |
mul() मे ,
mul() function मे दो value को multilication किया जाता  है | और आउटपुट की value को display किया जाता है |
div() मे ,
div () function मे दो value को division किया जाता  है | और आउटपुट की value को display किया जाता है |

Source Code
#include <iostream>
#include<conio.h>
using namespace std;
// Function prototype (declaration)
int SimpleIntrest(int, int , int );
void  main()
{
int number1 , number2 ;
char choice ;
cout<<“Enter ‘+’ for addition”<<endl<<“Enter ‘-‘ for subtraction”<<endl<<“Enter ‘*’ for multiplication”,,endl<<“Enter ‘/’ for division”<<endl;
cout<<“Enter choice : “;
cin >> choice;
cout<<“Enter number1 : “;
cin >> number1;
cout<<“Enters number2 : “;
cin >> number2;
switch(choice)
{
case ‘+’ :
add(number1,number2);
break;
case ‘-‘ :
sub(number1,number2);
break;
case ‘*’ :
mul(number1,number2);
break;
case ‘/’ :
div(number1,number2);
break;
default :
cout<<“invalid choice “;
break;
}
getch();
}
// Function definition
void add(int a, int b)
{
int output;
output = a + b;
cout<<“Output : “<<output;
}
// Function definition
void div(int a, int b)
{
int output;
output = a / b;
cout<<“Output : “<<output;
}
// Function definition
void sub(int a, int b)
{
int output;
output = a – b;
cout<<“Output : “<<output;
}
// Function definition
void mul(int a, int b)
{
int output;
output = a * b;
cout<<“Output : “<<output;
}

इसका आउटपुट होगा :
Enter ‘+’ for addition
Enter ‘-‘ for subtraction
Enter ‘*’ for multiplication
Enter ‘/’ for division
Enter choice : +
Enter number1 : 12
Enter number2 : 24
output : 36

उदाहरन 2 :
Write a program to calculate cube using function .
इस उदाहरण मे , user define function से cube को calculate करने के लिए function को बनाया गया है |

Expalnation
1.सबसे पहले header feild मे int cube(int) को declare किया गया है | इस declartion से ये define होता है की function cube() से एक value return होती है जिसका  type integer है | और इस function मे pass argument की सख्या ‘1’ है और type integer है |
2.उसके बाद main() function मे एक variable number को declare किया गया है इसमें यूजर द्वारा input की गयी value को assign किया गया है |
3.यूजर द्वारा  value को input करा लेते है और इन value को  variable number मे assign करा देते है |
4.उसके बाद एक और variable ‘o’ को declare करते है | जिसमे function cube() के द्वार return की value को assign किया जाता है |
5.उसके बाद ‘o’ की value को display कर देते है |

cube () मे ,
1.सबसे पहले function definition मे cube(int n ) को define किया जाता है | यह पर ‘n’ formal parameter है जिसमे actual parameter number से intial किया जाता है |
2.उसके बाद output = n*n ; से cube को calculate कर लेते है |
3.return statement से output की value को main () मे pass कर देते है |

Source Code
#include <iostream>
#include<conio.h>
using namespace std;
// Function prototype (declaration)
int cube(int);
void  main()
{
int  number ;
cout<<“Enter number : “;
cin >> number;
// Function call
int o  = cube(number);
cout<<“cube of:”<<number << “is”<<intrest;
getch();
}
// Function definition
int cube ( int n)
{
int output;
output = n*n*n;
return output ;
}

इसका आउटपुट होगा :
Enters number : 10
cube of 10 is 1000

उदाहरन 3 :
Write a program to swaping two numbers  using function .
इस उदाहरण मे , user define function से  two numbers swap करने के लिए function को बनाया गया है |

Expalnation
1.सबसे पहले header feild मे  void swap(int) को declare किया गया है | इस declartion से ये define होता है की function swap() से  कोई भी value return नहीं होती है |
2.उसके बाद main() function मे  variable number1 , number2 को declare किया गया है इसमें यूजर द्वारा input की गयी values को assign किया गया है |
3.यूजर द्वारा  value को input करा लेते है और इन value को  variable number1 , number2 मे assign करा देते है |
4.function swap () मे variable number1 , number2 को pass किया जाता है |

swap () मे ,
1.सबसे पहले function definition मे swap (int a,int b ) को define किया जाता है | यह पर ‘a’ और ‘b’ formal parameter है जिसमे actual parameter number1 , number2 से intial किया जाता है |
2.उसके बाद ,  swaping किया जाता है  |
3.फिर उसके बाद number1 , number2 की value को display किया जाता है |

Source Code
#include <iostream>
#include<conio.h>
using namespace std;
// Function prototype (declaration)
void swap (int , int );
void  main()
{
int  number1 , number2   ;
cout<<“Enter number1  : “;
cin >> number1 ;
cout<<“Enter number2  : “;
cin >> number2 ;
// Function call
swap (number1,number2);
getch();
}
// Function definition
void swap  ( int a,int b )
{
int temp;
temp = a;
a=b;
b=temp;
cout<<“Number1 :”<<a;
cout<<“Number2 :”<<b;
}

इसका आउटपुट होगा :
Enter number1  : 120
Enter number2  : 130
Number1 : 130
Number2 : 120