Professional Documents
Culture Documents
in C++
Function
// my function in C++
#include <iostream>
using namespace std;
void prrent (); //prototyping function
int main ()
{
prrent ();
return 0;
}
void prrent () {
cout << "I'm a C++ program\n";
cout << "Hello World! ";
}
// my function in C++
#include <iostream>
using namespace std;
void prrent ()
{
int main ()
{
prrent ();
return 0;
}
// my function in C++
#include <iostream>
using namespace std;
void prrent (int x)
{
int main ()
{
prrent (20);
return 0;
}
/ my function in C++
#include <iostream>
using namespace std;
int main ()
{
cout << "your addation is="<<add(55,66);
return 0;
}
// my function in C++
#include <iostream>
using namespace std;
int main ()
{
cout << "your addation is="<<add(55,66,77,88);
return 0;
}
function example
// function example
#include <iostream>
using namespace std;
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}
void printmessage ()
{
cout << "I'm a function!"<< endl;
}
int main ()
{
printmessage ();
return 0;
}
#include <iostream>
using namespace std;
int main ()
{
int x=1, y=3, z=7;
duplicate (x, y, z);
cout << "x=" << x << ", y=" << y << ", z=" << z;
return 0;
}
#include <iostream>
using namespace std;
int sque( int ); // function prototype
int main()
{
// loop 10 times and calculate and output
// square of x each time
for ( int x = 1; x <= 10; x++ )
cout << sque( x ) << " "; // function call
cout << endl;
return 0; // indicates successful termination
} // end main
int sque( int y ) // y is a copy of argument to function
{
return y * y;
// returns square of y as an int
}
int main ()
{
int x=100, y, z;
prevnext (x, y, z);
cout << "Previous=" << y << ", Next=" << z;
system("pause");
return 0;}
// overloaded function
#include <iostream>
using namespace std;
int main ()
{
int x=5,y=2;
float n=5.0,m=2.0;
cout << operate (x,y);
cout << "\n";
cout << operate (n,m);
cout << "\n";
system("pause");
return 0;
}
// passing parameters by
reference
void function
#include <iostream>
using namespace std;
void dummyfunction (void)
{
cout << "I'm a function!\n";
}
int main()
{
dummyfunction() ;
return 0;
}
returning value
Calling a Function:
While creating a C++ function, you give a definition of
what the function has to do. To use a function, you will
have to call or invoke that function.
When a program calls a function, program control is
transferred to the called function. A called function
performs defined task and when its return statement is
executed or when its function-ending closing brace is
reached, it returns program control back to the main
program.
To call a function, you simply need to pass the required
parameters along with function name, and if function
returns a value, then you can store returned value. For
example:
#include <iostream>
using namespace std;
// function declaration
int max(int num1, int num2);
int main ()
{
// local variable declaration:
int a = 100;
int b = 200;
int ret;
// calling a function to get max value.
ret = max(a, b);
cout << "Max value is : " << ret << endl;
system("pause");
return 0;
}
// function returning the max between two numbers
int max(int num1, int num2)
{
// local variable declaration
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
Function Arguments
Function Arguments:
If a function is to use arguments, it must declare
variables that accept the values of the
arguments. These variables are called the formal
parameters of the function.
The formal parameters behave like other local
variables inside the function and are created
upon entry into the function and destroyed upon
exit.
While calling a function, there are two ways that
arguments can be passed to a function:
Call Type
Description
Call by value
Call by pointer
Call by reference
Function
// factorial calculator
#include <iostream>
using namespace std;
long factorial (long a)
{
if (a > 1)
return (a * factorial (a-1));
else
return (1);
}
int main ()
{
long number;
cout << "Please type a number: ";
cin >> number;
cout << number << "! = " << factorial (number);
return 0;
}
#include <iostream.h>
int maximum (int, int, int);
main( )
{
int a, b, c;
cout << "Enter three integers: " ;
cin >> a >> b >> c ;
cout << " maximum is : " << maximum (a, b, c) << endl;
system("pause");
return 0;
}
int maximum (int x, int y, int z)
{
int max = x;
if (y > x)
max = y;
if (z > max)
max = z;
//Continued
return max;
}
Recursivity.
Recursivity is the property that functions have to be called
by themselves. It is useful for many tasks, like sorting or
calculate the factorial of numbers. For example, to obtain
the factorial of a number (n!) the mathematical formula
would be:
n! = n * (n-1) * (n-2) * (n-3) ... * 1
more concretely, 5! (factorial of 5) would be:
5! = 5 * 4 * 3 * 2 * 1 = 120
and a recursive function to calculate this in C++ could be:
// factorial calculator
#include <iostream>
using namespace std;
int main ()
{
long number;
cout << "Please type a number: ";
cin >> number;
cout << number << "! = " << factorial (number);
system("pause");
return 0;
}
#include <iostream>
using namespace std;
void f1 ( );
void f2 (void);
//Continued
main( )
{
f1 ( );
f2 ( );
system("pause");
return 0;
}
void f1 ( )
{
cout << "Function f1 takes no arguments" << endl;
}
void f2 (void)
{
cout << "Function f2 also takes no arguments" << endl;
}
// function example
#include <iostream>
using namespace std;
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}
#include<iostream.h>
inline float cube(float s){return s*s*s;}
main()
{
cout<<"\nEnter the side length of your cube : ";
float side;
cin>>side;
cout<<"volume of cube is "
<<cube(side)
<<endl;
system("pause");
}
//Inline Functions
#include <iostream.h>
inline int mult( int a, int b)
{
return (a*b);
}
//Continued
main( )
{
int x, y, z;
cin >> x >> y >> z;
cout << "x = " << x << " y = " << y << " z = " << z << endl;
cout << "product1" << mult (x ,y) << endl;
cout << "product2" << mult (x +2, y) << endl;
system("pause");
return 0;
}
*/
#include <iostream.h>
int square(int x){return x*x;}
//Continued
double square(double y){return y*y;}
main ()
{
cout<< " The square of integer 7 is"
<<" "<<square(7)<< endl
<<"The square of double 7.5 is"
<<" "<<square(7.5)<< endl;
system("pause");
return 0;
}
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float first,second,third;
float s,area;
cout<<"Enter size of each sides of triangle"<<endl;
cout<<"Enter size for First Side =";
cin>>first;
cout<<"Enter size for Second Side =";
cin>>second;
cout<<"Enter size for Third Side =";
cin>>third;
s = (first+second+third)/2;
area = sqrt(s*(s-first)*(s-second)*(s-third));
return 0;
#include <iostream.h>
int calculateresult (int, int);
int main( )
{
int a, b, c, d, e, f;
cin >> a;
cin >> b;
c = calculateresult (a, b);
cin >> d;
cin >> e ;
f = calculateresult (d, e);
cout << f;
return 0;
}
int calculateresult (int p, int q)
{
int r;
r = p + q + (p * q);
//ystem("pause");
return r;
}
Function passing by
reference
int main ()
{
int x=1, y=3, z=7;
duplicate (x, y, z);
cout << "x=" << x << ", y=" << y << ", z=" << z;
return 0;
}
// factorial calculator
#include <iostream>
using namespace std;
int main ()
{
long number;
cout << "Please type a number: ";
cin >> number;
cout << number << "! = " << factorial (number);
return 0;
}
#include <iostream>
using namespace std;
// function declaration
int max(int num1, int num2);
int main ()
{
// local variable declaration:
int a = 100;
int b = 200;
int ret;
// calling a function to get max value.
ret = max(a, b);
cout << "Max value is : " << ret << endl;
return 0;
}
// function returning the max between two numbers
int max(int num1, int num2)
{
// local variable declaration
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
#include <iostream>
using namespace std;
int main()
{
int i;
float y,x,s,f,m,n,d; f=y=1;
m=-1;
n=0;
cout<<"enter the power of";
cin>>d;
cout<<"enter the number";
cin>>x;
for(i=1;i<=d;i++){
f=f*i;
m=m*x;
n=-m; // inverse sgnal inevery loop
s=n/f;
y=y+s;}
cout<<y;
}
Type a number (0
Number is odd.
Type a number (0
Number is even.
Type a number (0
Number is even.
Type a number (0
Nu
to exit): 9
to exit): 6
to exit): 1030
to exit): 0
#include <iostream>
using namespace std;
int main()
{float i,j,m,sum;
j=3;
sum=0;
for(i=1;i<=100;i++)// 100 item
{
m=(i*i)/(j*j);
j=j+2;
sum=sum+m;}
cout<<"seque\n"<<sum;
}
#include <iostream>
#include <cmath>
const double PI = 3.14159;
double area(double radius);
//Returns the area of a circle with the specified radius.
double volume(double radius);
//Returns the volume of a sphere with the specified radius.
int main( )
{
using namespace std;
area_of_circle = area(radius_of_both);
volume_of_sphere = volume(radius_of_both);
#include<iostream>
#include<conio.h>
int main()
{
// Variable Declaration
int counter, n, fact = 1;
#include<iostream>
int main()
int num,factorial=1;
cin>>num;
for(int a=1;a<=num;a++)
factorial=factorial*a;
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float first,second,third;
float s,area;
cout<<"Enter size of each sides of triangle"<<endl;
cout<<"Enter size for First Side =";
cin>>first;
cout<<"Enter size for Second Side =";
cin>>second;
cout<<"Enter size for Third Side =";
cin>>third;
s = (first+second+third)/2;
area = sqrt(s*(s-first)*(s-second)*(s-third));
return 0;
#include <iostream>
using namespace std;
int main()
{
long dec,rem,i=1,sum=0;
cout<<"Enter the decimal to be converted:";
cin>>dec;
do
{
rem=dec%2;
sum=sum + (i*rem);
dec=dec/2;
i=i*10;
}while(dec>0);
cout<<"The binary of the given number is:"<<sum<<endl;
cin.get();
cin.get();
system("pause");
return 0;
}
For example 18
18 divide by 2 leaves quotient 9 remainder 0
9 divide by 2 leaves quotient 4 remainder 1
4 divide by 2 leaves quotient 2 remainder 0
2 divide by 2 leaves quotient 1 remainder 0
1 divide by 2 leaves quotient 0 remainder 1
So binary of 18 is 10010
Fibonacci Sequence
Fibonacci Sequence
#include<iostream>
using namespace std;
main()
{
using namespace std;
double a, b, c;
a=0;
b=1;
cout<<"The Fibonacci Sequence!"<<endl<<endl;
cout<<a<<"\t"<<b<<"\t"; //Manually Printing the first 2 numbers
for(int count = 3; count<=20; count++) //The Loop to print the rest of the numbers
{
c=a+b;
cout<<c<<"\t";
a=b;
b=c;
if(count % 5 == 0) //An 'endl' after every 5 numbers.
{
cout<<endl;
}
}
system("pause");
cin.get();
}
#include <iostream.h>
int calculateresult (int, int);
int main( )
{
int a, b, c, d, e, f;
cin >> a;
cin >> b;
c = calculateresult (a, b);
cin >> d;
cin >> e ;
f = calculateresult (d, e);
cout << f;
system("pause");
return 0;
}
int calculateresult (int p, int q)
{
int r;
r = p + q + (p * q);
//ystem("pause");
return r;
}
// function example
#include <iostream>
using namespace std;
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}
// function example
#include <iostream>
using namespace std;
int main ()
{
int x=5, y=3, z;
z = subtraction (7,2);
cout << "The first result is " << z << '\n';
cout << "The second result is " << subtraction (7,2) << '\n';
cout << "The third result is " << subtraction (x,y) << '\n';
z= 4 + subtraction (x,y);
cout << "The fourth result is " << z << '\n';
return 0;
}
void printmessage ()
{
cout << "I'm a function!";
}
int main ()
{
printmessage ();
return 0;
}
int main ()
{
int x=1, y=3, z=7;
duplicate (x, y, z);
cout << "x=" << x << ", y=" << y << ", z=" << z;
return 0;
}
int main ()
{
int x=100, y, z;
prevnext (x, y, z);
cout << "Previous=" << y << ", Next=" << z;
return 0;
}
int main ()
{
cout << divide (12);
cout << endl;
cout << divide (20,4);
return 0;
}
/ overloaded function
#include <iostream>
using namespace std;
int main ()
{
int x=5,y=2;
float n=5.0,m=2.0;
cout << operate (x,y);
cout << "\n";
cout << operate (n,m);
cout << "\n";
return 0;
}
Recursivity.
Recursivity is the property that functions have to be called
by themselves. It is useful for many tasks, like sorting or
calculate the factorial of numbers. For example, to obtain
the factorial of a number (n!) the mathematical formula
would be:
n! = n * (n-1) * (n-2) * (n-3) ... * 1
more concretely, 5! (factorial of 5) would be:
5! = 5 * 4 * 3 * 2 * 1 = 120
and a recursive function to calculate this in C++ could be:
// factorial calculator
#include <iostream>
using namespace std;
int main ()
{
long number;
cout << "Please type a number: ";
cin >> number;
cout << number << "! = " << factorial (number);
return 0;
}
int main ()
{
int i;
do {
cout << "Type a number (0 to exit): ";
cin >> i;
odd (i);
} while (i!=0);
return 0;
}
Function Declarations:
A function declaration tells the compiler about a function name and how to
call the function. The actual body of the function can be defined
separately.
Calling a Function:
While creating a C++ function, you give a definition of
what the function has to do. To use a function, you will
have to call or invoke that function.
When a program calls a function, program control is
transferred to the called function. A called function
performs defined task and when its return statement is
executed or when its function-ending closing brace is
reached, it returns program control back to the main
program.
To call a function, you simply need to pass the required
parameters along with function name, and if function
returns a value, then you can store returned value. For
example:
#include <iostream>
using namespace std;
// function declaration
int max(int num1, int num2);
int main ()
{
// local variable declaration:
int a = 100;
int b = 200;
int ret;
// calling a function to get max value.
ret = max(a, b);
cout << "Max value is : " << ret << endl;
return 0;
}
// function returning the max between two numbers
int max(int num1, int num2)
{
// local variable declaration
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
#include <iostream>
#include <string>
using namespace std;
string GetName()
{
string FirstName, LastName, FN;
cout << "Employee's First Name: ";
cin >> FirstName;
cout << "Employee's Last Name: ";
cin >> LastName;
FN = FirstName + " " + LastName;
return FN;
}int main()
{
string FullName;
double Hours;
double GetHours(string FullName);
FullName = GetName();
Hours = GetHours(FullName);
cout << "\nEmployee's Name: " << FullName;
cout << "\nWeekly Hours: " << Hours << " hours\n\n";
system("pause");
return 0;
}
double GetHours(string FullName)
{
double Mon, Tue, Wed, Thu, Fri, TotalHours;
cout << endl << FullName << "'s Weekly Hours\n";
cout << "Monday: ";
cin >> Mon;
cout << "Tuesday: ";
cin >> Tue;
cout << "Wednesday: ";
cin >> Wed;
cout << "Thursday: ";
cin >> Thu;
cout << "Friday: ";
cin >> Fri;
TotalHours = Mon + Tue + Wed + Thu + Fri;
return TotalHours;
#include <iostream.h>
main()
{
int i;
float y,x,s,f,m,n,d; f=y=1;
m=-1;
n=0;
cout<<"enter the power of";
cin>>d;
cout<<"enter the number";
cin>>x;
for(i=1;i<=d;i++){
f=f*i;
m=m*x;
n=-m; // inverse sgnal inevery loop
s=n/f;
y=y+s;}
cout<<y;
system("pause");
}
int main()
{
// code
return 0;
}
inline swap( int& a, int& b) { int
const tmp(b); b=a; a=tmp; }
#include <iostream>
void MyFunc( int *x )
{
std::cout << *x << std::endl; // See next section for
explanation
}
int main()
{
int i;
MyFunc( &i );
return 0;
}
#include <iostream>
int main()
{
int x=2, y=3;
swap(x,y);
cout << "X=" << x << " Y=" << y;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int number;
int abs_number;
#include <iostream>
using namespace std;
void foo(int x)
{
int y = 4;
x = 10;
cout << x << " " << y;
}
int main()
{
int x=2, y=3;
foo(x);
cout << x << " " << y;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int x=1, y=2;
foo(y, x);
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main( )
{
int n1, n2;
n1 = 1;
n2 = 2;
doStuff(n1, n2);
cout << "n1 after function call = " << n1 << endl;
cout << "n2 after function call = " << n2 << endl;
system("pause");
return 0;
Rotate 90 of matrix
#include<iostream.h>
main()
{
int i,j;
int a[3][3];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>a[i][j];
for(j=0;j<3;j++){
cout<<"\n";
for(i=2;i>=0;i--)
cout<<a[i][j]<<"\t";
}
system("pause");}
#include<iostream.h>
main()
{
int i,j;
int a[3][3];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>a[i][j];
for(i=2;i>=0;i--){
cout<<"\n";
for(j=2;j>=0;j--)
cout<<a[i][j]<<"\t";
} system("pause");}
#include<iostream.h>
main()
{
int i,j;
int a[3][3];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>a[i][j];
for(j=2;j>=0;j--){
cout<<"\n";
for(i=0;i<3;i++)
cout<<a[i][j]<<"\t";
}
system("pause");}
#include<stdio.h>
main()
{float i,j,m,sum;
j=3;
sum=0;
for(i=1;i<=100;i++)// 100 item
{
m=(i*i)/(j*j);
j=j+2;
sum=sum+m;}
printf("seque=%f",sum);}
Return by value
#include<iostream.h>
int square(int);//function prototype
main()
{
for(int x=1;x<=10;x++)
cout<<square(x)<<" ";
cout<<endl;
system("pause");}
//now function definition
int square(int y)
{ return y*y;
}
//function definition
#include <iostream.h>
int maximum(int, int, int);
int main( )
{
int a, b, c;
cout << "Enter threeintegers: ";
cin >> a >> b >> c;
cout << " maximumis: " << maximum (a, b, c) << endl;
system("pause");
return 0;
}
int maximum(int x, int y, int z)
{
int max = x;
if (y > x)
max = y;
if (z > max)
max = z;
//Continued
return max;
}
#include<iostream.h>
inline float cube(float s)
{return s*s*s;}
main()
{
cout<<"\nEnter the side length of your cube: ";
float side;
cin>>side;
cout<<"volume of cubeis "
<<cube(side)
<<endl;
system("pause");}
#include <iostream.h>
int square(int x){return x*x;}
//Continued
double square(double y){return y*y;}
main ()
{
cout<< " The square ofinteger 7is"
<<""<<square(7)<< endl
<<"The square of double 7.5is"
<<""<<square(7.5)<< endl;
system("pause");
return 0;
}