1 / 38

C++ CODES PART#09

C++ CODES PART#09. USER DEFINE FUNCTION TO CALCULATE CUBE OF A NUMBER. #include&lt;constream.h&gt; int cube(int); void main() {clrscr(); int x; cout&lt;&lt;&quot;<br> enter any number:&quot;;. CONTINUE…. cin&gt;&gt;x; cout&lt;&lt;&quot;<br> cube oft&quot; &lt;&lt;x&lt;&lt; &quot;t is&quot; &lt;&lt;&quot;t&quot;&lt;&lt;cube(x); getch();} //user define function

efuru
Télécharger la présentation

C++ CODES PART#09

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ CODES PART#09 COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  2. USER DEFINE FUNCTION TO CALCULATE CUBE OF A NUMBER #include<constream.h> int cube(int); void main() {clrscr(); int x; cout<<"\n enter any number:"; COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  3. CONTINUE… cin>>x; cout<<"\n cube of\t" <<x<< "\t is" <<"\t"<<cube(x); getch();} //user define function int cube(int x) { return x*x*x; } COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  4. OUTPUT….. COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  5. Two different ways to write The same function • Method1: #include<constream.h> int sum(void); void main() {clrscr(); int c; COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  6. CONTINUE…. c=sum(); cout<<endl<<c; getch();} int sum(void){ int x=13,y=13; return x+y; } COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  7. OUTPUT….. COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  8. Method 2: #include<constream.h> int sum(void); int sum(void){ int x=13,y=13; return x+y; } COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  9. CONTINUE…. void main() {clrscr(); int c; c=sum(); cout<<endl<<c; getch();} COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  10. OUTPUT… COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  11. Argument passing example #include<constream.h> void call(int x) {cout<<x+8; } void main() {clrscr(); COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  12. CONTINUE… int x; cin>>x; call(x); getch();} COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  13. OUTPUT…. COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  14. Multiple argument passing #include<constream.h> void call(int x,int y) {cout<<x+y; } void main() {clrscr(); COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  15. CONTINUE…. int x,y; cin>>x>>y; call(x,y); getch();} COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  16. OUTPUT… COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  17. Return function argument #include<constream.h> int fun(int x,float y) {return x*y; } void main() {clrscr(); COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  18. CONTINUE…. int a; float b; cin>>a>>b; cout<<fun(a,b); getch();} COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  19. OUTPUT…. COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  20. Factorial program by function #include<constream.h> int fact(int a) {int b,c=1; for(b=a;b>=1;--b) {c=c*b;} return c; } COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  21. CONTINUE… void main() { clrscr(); int a; cin>>a; {cout<<fact(a);} getch();} COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  22. OUTPUT… COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  23. Recursion #include<constream.h> int fact(int a){ if(a==1||a==0) return 1; else return a*fact(a-1);} COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  24. CONTINUE… void main() {clrscr(); int x; cin>>x; cout<<fact(x); getch();} COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  25. 3 OF THE POSSIBLE OUTPUTS… COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  26. Nested user define function #include<constream.h> int sumsqr(int,int); int sum(int,int); int sqr(int); void main() {clrscr(); COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  27. CONTINUE…. int x,y; cout<<"\n enter any two numbers:"; cin>>x>>y; cout<<"\nthe square of "<<x<<"is"<<sqr(x); cout<<"\n the square of "<<y<<"is"<<sqr(y); COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  28. CONTINUE… cout<<"\n the sum of the squares is"<<sumsqr(x,y); getch(); } //udf sum of the squares int sumsqr(int a,int b) COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  29. CONTINUE… { return sum(sqr(a),sqr(b));} //udf square of the numbers int sqr(int c) {return c*c; COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  30. CONTINUE… } //udf square of the numbers int sum(int d,int e) {return d+e; } COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  31. OUTPUT… COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  32. Function overloading #include <constream.h> // This program illustrate the use of Function overloading int sqr(int x); float sqr(float x); double sqr(double x); COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  33. CONTINUE… void main(void) { // declare three variable of different types clrscr(); int val1=4; float val2=4.1; double val3=4.123456; COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  34. CONTINUE… // get the data from the user and call each version of function cout << "Square of 4 is " << sqr(val1) << "\n"; cout << "Square of 4.1 is " << sqr(val2) << "\n"; cout << "Square of 4.123456 is" << sqr(val3) << "\n"; COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  35. CONTINUE… getch(); } // now declare the three function versions int sqr(int x) { return(x*x); } COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  36. CONTINUE… float sqr(float x) { return(x*x); } double sqr(double x) { return(x*x); } COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  37. OUTPUT…… COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

  38. NICE WORDINGS • Apnay dushman ko hazar mouqa do k woh tumara dost ban jaye. LEKIN apnay dost ko 1 bhi moqa na do k woh tumara dushman ban jaiy. COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT

More Related