Upload
efuru
View
43
Download
0
Tags:
Embed Size (px)
DESCRIPTION
C++ CODES PART#09. USER DEFINE FUNCTION TO CALCULATE CUBE OF A NUMBER. #include int cube(int); void main() {clrscr(); int x; coutx; cout
Citation preview
C++ CODES PART#09
1
COPY RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
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:";
2
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;
}3
COPY RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
OUTPUT…..
4
COPY RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
Two different ways to write The same function
• Method1:
#include<constream.h>
int sum(void);
void main()
{clrscr();
int c;
5
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;
}
6
COPY RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
OUTPUT…..
7
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;
}
8
COPY RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
CONTINUE….
void main()
{clrscr();
int c;
c=sum();
cout<<endl<<c;
getch();}
9
COPY RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
OUTPUT…
10
COPY RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
Argument passing example
#include<constream.h>
void call(int x)
{cout<<x+8;
}
void main()
{clrscr();
11
COPY RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
CONTINUE…
int x;
cin>>x;
call(x);
getch();}
12
COPY RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
OUTPUT….
13
COPY RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
Multiple argument passing
#include<constream.h>
void call(int x,int y)
{cout<<x+y;
}
void main()
{clrscr();
14
COPY RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
CONTINUE….
int x,y;
cin>>x>>y;
call(x,y);
getch();}
15
COPY RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
OUTPUT…
16
COPY RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
Return function argument
#include<constream.h>
int fun(int x,float y)
{return x*y;
}
void main()
{clrscr();
17
COPY RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
CONTINUE….
int a;
float b;
cin>>a>>b;
cout<<fun(a,b);
getch();}
18
COPY RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
OUTPUT….
19
COPY RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
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; }
20
COPY RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
CONTINUE…
void main()
{
clrscr();
int a;
cin>>a;
{cout<<fact(a);}
getch();}
21
COPY RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
OUTPUT…
22
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);}
23
COPY RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
CONTINUE…
void main()
{clrscr();
int x;
cin>>x;
cout<<fact(x);
getch();}
24
COPY RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
3 OF THE POSSIBLE OUTPUTS…
25
COPY RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
Nested user define function
#include<constream.h>
int sumsqr(int,int);
int sum(int,int);
int sqr(int);
void main()
{clrscr();
26
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);
27
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)
28
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;
29
COPY RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
CONTINUE…
}
//udf square of the numbers
int sum(int d,int e)
{return d+e;
}
30
COPY RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
OUTPUT…
31
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);
32
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=4.123456;
33
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 4.123456 is" << sqr(val3) << "\n";
34
COPY RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
CONTINUE…
getch();
}
// now declare the three function versions
int sqr(int x) {
return(x*x);
}
• 35
COPY RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
CONTINUE…
float sqr(float x) {
return(x*x);
}
double sqr(double x) {
return(x*x);
}
36
COPY RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
OUTPUT……
37
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 RIGHT@ ENGR.SABA MUGHAL FROM
COMPUTER SYSTEMS DEPARTMENT
38