0% found this document useful (0 votes)
2 views10 pages

C++ assignment chapter 4 Function

The document contains multiple examples of C++ code snippets demonstrating various programming concepts such as functions, variable scope, and inline functions. Each example illustrates different functionalities including arithmetic operations, passing parameters by reference, and using default arguments. The code is structured with input/output operations and showcases basic programming constructs.

Uploaded by

agetachewseid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

C++ assignment chapter 4 Function

The document contains multiple examples of C++ code snippets demonstrating various programming concepts such as functions, variable scope, and inline functions. Each example illustrates different functionalities including arithmetic operations, passing parameters by reference, and using default arguments. The code is structured with input/output operations and showcases basic programming constructs.

Uploaded by

agetachewseid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

EXAMPLE 1

#include <iostream.h>
#include <conio.h>
void add();
void sub();
void mult();
void main()
{
clrscr();
add();
mult();
sub();
getch();
}
void add()
{
int x,y,sum;
cout<<"please enter the first number\n";
cin>>x;
cout<<"please enter the second number\n";
cin>>y;
sum=x+y;
cout<<"sum="<<sum<<endl;
}
void mult()
{
int a,b,mult;
cout<<"please enter the first number\n";
cin>>a;
cout<<"please enter the second number\n";
cin>>b;
mult=a*b;
cout<<"mult="<<mult<<endl;
}
void sub()
{
int j,k,sub;
cout<<"please enter the first number\n";
cin>>j;
cout<<"please enter the second number\n";
cin>>k;
sub=j-k;
cout<<"sub="<<sub<<endl;
}
EXAMPLE 4
#include <iostream.h>

#include <conio.h>

void foo(int num);

int main(void)

clrscr();

int x=10;

foo(x);

cout<<"x="<<x<<'\n';

return 0;

void foo(int num)

num =0;

cout<<"num="<<num<<'\n';

10
EXAMPLE 5
#include <iostream.h>

#include <conio.h>

void foo(int & num);

int main (void)

clrscr();

int x=10;

foo(x);

cout<<"x="<<x<<'\n';

return 0;

void foo(int & num)

num=0;

cout<<"num="<<num<<'\n';

10
EXAMPLE 6
#include <iostream.h>
#include <conio.h>
void getdata(int&devnd,int&divisr);
void divide(int divdn,int divisr,int&quot,int&rem);
void print(int quot,int rem);
int main(void)
//a function without a parameter can be either left free or void as a parameter
{
int a,b,c,d;
getdata(a,b);
print(c,d);
return 0;
}
void getdata(int&dividn,int&divisr)
{
cout<<"enter two entegers";
cin>>dividn>>divisr;
return;
}
void divide(int dividn,int divisr,int&quot,int&rem)
{
quot=dividn/divisr;
rem=dividn%divisr;
return;
}
void print(int quot,int rem)
{
cout<<"Quotient is="<<quot<<endl;
cout<<"remainder is="<<rem<<endl;
return;
}
EXAMPLE 7
#include <iostream.h>

#include <conio.h>

int year=1994;//global variable

int Max(int,int);//global function

int main(void)//global function

clrscr();

int x,y;

cout<<"enter first number"<<endl;

cin>>x;

cout<<"enter the second number"<<endl;

cin>>y;

Max(x,y);

int Max(int x,int y)

if(x>y)

cout<<"the minimum is"y;

else if(y<x)

cout<<"the minimum is"x;

else

cout<<"they are equal";

}
EXAMPLE 8
#include <iostream.h>

#include <conio.h>

int x=12;

void main()

clrscr();

int x=5;//this x is local to the main function

if(x>=::x)//here the first x refers to the local x,and the next x refers to the global x.

cout<<"the local x is greater"<<x<<endl;

else if(x==::x);

cout<<"both are equal the local x is=:"<<x<<"the global x is=:"<<::x<<endl;

else

cout<<"the global x is greater it's value is"<<::x<<endl;

}
EXAMPLE 9
#include <iostream.h>

#include <conio.h>

int devide(int a,int b=2);

int main()

clrscr();

cout<<devide(12);//here the default vallue 2 is passed as a second argument.

cout<<endl;

cout<<devide(20,4);

return 0;

int devide (int a,int b)

int r;

r=a/b;

return (r);

}
EXAMPLE 10
#include <iostream.h>
#include <conio.h>
int devide(int a,int b=2);
int devide(int z,int r,int y);
float devide (float a,float b);
float devide (float n,float m);
int main()
{
clrscr();
int x=20,y=2;
float n=5.0,m=2.0;
cout<<devide(x,y);
cout<<endl;
cout<<devide(n,m);
cout<<endl;
cout<<devide(n,m,m);
cout<<endl;
return 0;
}
int devide (int a,int b)
{
return a/b;
}
int devide (int a,int b,int c)
{
int w=a/b;
return w/c;
}
float devide (float x,float y)
{
return x/y;
}

EXAMPLE 11
#include <iostream.h>

#include <conio.h>

inline int Abs(int n)

return (n>0?n:-n);

int main()

clrscr();

int n=-3,m=4;

cout<<Abs(n);

cout<<Abs(m);

return 0;

EXAMPLE 12
include <iostream.h>

#include <conio.h>

int factorial (unsigned int n);

int main()

clrscr();

int n;

cout<<"enter a+ve number";

cin>>n;

cout<<"!"<<n<<"=:"<<factorial(n);

return 0;

int factorial (unsigned n)

if(n==0)

return 1;

else

return (n*(factorial(n-1)));

You might also like