Download presentation
Presentation is loading. Please wait.
Published byJoanna Daniels Modified over 6 years ago
1
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
C++ CODES PART#09 COPY 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 ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
3
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
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 ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
4
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
OUTPUT….. COPY 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 ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
6
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
CONTINUE…. c=sum(); cout<<endl<<c; getch();} int sum(void){ int x=13,y=13; return x+y; } COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
7
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
OUTPUT….. COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
8
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
Method 2: #include<constream.h> int sum(void); int sum(void){ int x=13,y=13; return x+y; } COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
9
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
CONTINUE…. void main() {clrscr(); int c; c=sum(); cout<<endl<<c; getch();} COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
10
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
OUTPUT… COPY 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 ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
12
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
CONTINUE… int x; cin>>x; call(x); getch();} COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
13
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
OUTPUT…. COPY 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 ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
15
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
CONTINUE…. int x,y; cin>>x>>y; call(x,y); getch();} COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
16
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
OUTPUT… COPY 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 ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
18
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
CONTINUE…. int a; float b; cin>>a>>b; cout<<fun(a,b); getch();} COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
19
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
OUTPUT…. COPY 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 ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
21
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
CONTINUE… void main() { clrscr(); int a; cin>>a; {cout<<fact(a);} getch();} COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
22
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
OUTPUT… COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
23
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
Recursion #include<constream.h> int fact(int a){ if(a==1||a==0) return 1; else return a*fact(a-1);} COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
24
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
CONTINUE… void main() {clrscr(); int x; cin>>x; cout<<fact(x); getch();} COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
25
3 OF THE POSSIBLE OUTPUTS…
COPY 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 ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
27
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
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 ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
28
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
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 ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
29
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
CONTINUE… { return sum(sqr(a),sqr(b));} //udf square of the numbers int sqr(int c) {return c*c; COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
30
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
CONTINUE… } //udf square of the numbers int sum(int d,int e) {return d+e; COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
31
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
OUTPUT… COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
32
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
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 ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
33
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
CONTINUE… void main(void) { // declare three variable of different types clrscr(); int val1=4; float val2=4.1; double val3= ; COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
34
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
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 is" << sqr(val3) << "\n"; COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
35
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
CONTINUE… getch(); } // now declare the three function versions int sqr(int x) { return(x*x); COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
36
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
CONTINUE… float sqr(float x) { return(x*x); } double sqr(double x) { COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
37
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
OUTPUT…… COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
38
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
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 ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.