You are on page 1of 87

Functions

in C++

Dr. Ahmed Telba

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 ()
{

cout << "I'm a C++ program\n";


cout << "Hello World! ";
}

int main ()
{
prrent ();
return 0;
}

Creating Functions That Use


Parameters

// my function in C++
#include <iostream>
using namespace std;
void prrent (int x)
{

cout << "my number is =\t"<<x<<endl;


}

int main ()
{
prrent (20);
return 0;
}

Functions That Use Multiple


Parameters

/ my function in C++
#include <iostream>
using namespace std;

int add (int x,int y )


{
int answer=x+y;
return answer;

int main ()
{
cout << "your addation is="<<add(55,66);
return 0;
}

// my function in C++
#include <iostream>
using namespace std;

int add (int x,int y, int m,int n)


{
int answer=x+y+m+n;
return answer;

int main ()
{
cout << "your addation is="<<add(55,66,77,88);
return 0;
}

function example

// function example
#include <iostream>
using namespace std;

int addition (int a, int b)


{
int r;
r=a+b;
return (r);
}

int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}

// void function example


#include <iostream>
using namespace std;

void printmessage ()
{
cout << "I'm a function!"<< endl;
}

int main ()
{
printmessage ();
return 0;
}

// passing parameters by reference

#include <iostream>
using namespace std;

void duplicate (int& a, int& b, int& c)


{
a=a*a*2;
b=b*b*2;
c=c*c*c;
}

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
}

// more than one returning value


#include <iostream>
using namespace std;

void prevnext (int x, int& prev, int& next)


{
prev = x-1;
next = x+1;
}

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 operate (int a, int b)


{
return (a*b);
}

float operate (float a, float b)


{
return (a/b);
}

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

// passing parameters by reference


#include <iostream>
using namespace std;
void duplicate (int& a, int& b, int& c)
{
a*=2;
b*=2;
c*=2;
}
int main ()
{
int x=1, y=3, z=7;
cout<<"x,y,z before calling duplicate";
cout <<"\nx="<<x<<", y=" << y
<<", z=" <<z<<endl;
duplicate (x, y, z);
cout<<"x,y,z after calling duplicate\n";
cout <<"x="<<x<<", y=" << y
<<", z=" <<z<<endl;
return 0;}

void function

#include <iostream>
using namespace std;
void dummyfunction (void)
{
cout << "I'm a function!\n";
}
int main()
{
dummyfunction() ;
return 0;
}

function return by value or


result

// function example The result is 8


#include <iostream>
using namespace std;
int addition (int a, int b)
{
int r = a + b;
return (r);
}
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}

returning value

/ more than one returning value


#include <iostream>
using namespace std;
void prevnext (int x,int& prev, int& next)
{
prev = x-1;
next = x+1;
}
int main ()
{
int x=100, y, z;
prevnext (x, y, z);
cout<<"Previous=" << y << ", Next="<< z << endl;
return 0;
}

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

This method copies the actual


value of an argument into the
formal parameter of the function.
In this case, changes made to the
parameter inside the function
have no effect on the argument.

Call by pointer

This method copies the address of


an argument into the formal
parameter. Inside the function, the
address is used to access the
actual argument used in the call.
This means that changes made to
the parameter affect the
argument.

Call by reference

This method copies the reference


of an argument into the formal
parameter. Inside the function, the
reference is used to access the
actual argument used in the call.

Function

Type name(parameter1, parameter2,


.)
{
Statement;
return(type);
}

// 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;
}

Default Values for Parameters:

#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;

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);
system("pause");
return 0;
}

Functions with Empty Parameter


Lists

#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 addition (int a, int b)


{
int r;
r=a+b;
return (r);
}

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));

cout<<"Area of Triangle= "<<area<<endl;

return 0;

// Creating and using a programmer-defined function.


#include <iostream>
using namespace std;
int square( int ); // function prototype
int main()
{
for ( int x = 1; x <= 10; x++ )
cout << square( x ) << " "; // function call
cout << endl;
return 0; // indicates successful termination
} // end main
// square function definition returns square of an integer
int square( int y ) // y is a copy of argument to function
{
return y * y;
// returns square of y as an int
} // end function square

#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

// passing parameters by reference


#include <iostream>
using namespace std;

void duplicate (int& a, int& b, int& c)


{
a*=2;
b*=2;
c*=2;
}

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;

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>
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;
}

// declaring functions prototypes


#include <iostream>
using namespace std;
void odd (int a);
void even (int a);
int main ()
{
int i;
do {
cout << "Type a number (0 to exit): ";
cin >> i;
odd (i);
} while (i!=0);
return 0;
}
void odd (int a)
{
if ((a%2)!=0) cout << "Number is odd.\n";
else even (a);
}
void even (int a)
{
if ((a%2)==0) cout << "Number is even.\n";
else odd (a);
}

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;

double radius_of_both, area_of_circle, volume_of_sphere;

cout << "Enter a radius to use for both a circle\n"


<< "and a sphere (in inches): ";
cin >> radius_of_both;

area_of_circle = area(radius_of_both);
volume_of_sphere = volume(radius_of_both);

cout << "Radius = " << radius_of_both << " inches\n"


<< "Area of circle = " << area_of_circle
<< " square inches\n"
<< "Volume of sphere = " << volume_of_sphere
<< " cubic inches\n";
system("pause");
return 0;
}

double area(double radius)


{
using namespace std;

double volume(double radius)


{
using namespace std;

return (PI * pow(radius, 2));

return ((4.0/3.0) * PI * pow(radius, 3));


system("pause");
}

#include<iostream>
#include<conio.h>

using namespace std;

int main()
{

// Variable Declaration
int counter, n, fact = 1;

// Get Input Value


cout<<"Enter the Number :";
cin>>n;

//for Loop Block


for (int counter = 1; counter <= n; counter++)
{
fact = fact * counter;
}

cout<<n<<" Factorial Value Is "<<fact;


// Wait For Output Screen
getch();
return 0;
}

Factorial using for loop

#include<iostream>

using namespace std;

int main()

int num,factorial=1;

cout<<" Enter Number To Find Its Factorial: ";

cin>>num;

for(int a=1;a<=num;a++)

factorial=factorial*a;

cout<<"Factorial of Given Number is ="<<factorial<<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));

cout<<"Area of Triangle= "<<area<<endl;

return 0;

C++ program to convert decimal number


into binary number

#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();
}

// Creating and using a programmer-defined function.


#include <iostream.h>
int square( 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 << square( x ) << " "; // function call
cout << endl;
system("pause");
return 0; // indicates successful termination
} // end main
// square function definition returns square of an integer
int square( int y ) // y is a copy of argument to function
{
return y * y;
// returns square of y as an int
} // end function square

#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 addition (int a, int b)


{
int r;
r=a+b;
return (r);
}

int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}

// function example
#include <iostream>
using namespace std;

int subtraction (int a, int b)


{
int r;
r=a-b;
return (r);
}

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 function example


#include <iostream>
using namespace std;

void printmessage ()
{
cout << "I'm a function!";
}

int main ()
{
printmessage ();
return 0;
}

// passing parameters by reference


#include <iostream>
using namespace std;

void duplicate (int& a, int& b, int& c)


{
a*=2;
b*=2;
c*=2;
}

int main ()
{
int x=1, y=3, z=7;
duplicate (x, y, z);
cout << "x=" << x << ", y=" << y << ", z=" << z;
return 0;
}

// more than one returning value


#include <iostream>
using namespace std;

void prevnext (int x, int& prev, int& next)


{
prev = x-1;
next = x+1;
}

int main ()
{
int x=100, y, z;
prevnext (x, y, z);
cout << "Previous=" << y << ", Next=" << z;
return 0;
}

// default values in functions


#include <iostream>
using namespace std;

int divide (int a, int b=2)


{
int r;
r=a/b;
return (r);
}

int main ()
{
cout << divide (12);
cout << endl;
cout << divide (20,4);
return 0;
}

/ overloaded function
#include <iostream>
using namespace std;

int operate (int a, int b)


{
return (a*b);
}

float operate (float a, float b)


{
return (a/b);
}

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;

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;
}

// declaring functions prototypes


#include <iostream>
using namespace std;

void odd (int a);


void even (int a);

int main ()
{
int i;
do {
cout << "Type a number (0 to exit): ";
cin >> i;
odd (i);
} while (i!=0);
return 0;
}

void odd (int a)


{
if ((a%2)!=0) cout << "Number is odd.\n";
else even (a);
}

void even (int a)


{
if ((a%2)==0) cout << "Number is even.\n";
else odd (a);
}

// 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 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.

A function declaration has the following parts:

return_type function_name( parameter list );


For the above defined function max(), following is the function declaration:

int max(int num1, int num2);


Parameter names are not importan in function declaration only their type
is required, so following is also valid declaration:

int max(int, int);


Function declaration is required when you define a function in one source
file and you call that function in another file. In such case, you should
declare the function at the top of the file calling the function.

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;
}

//This illustrates the technique used to pass arguments by value to a function.

#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; }

//Global functions declaration


int subtraction_function( int parameter1, int
parameter2 ) { return ( parameter1 - parameter2 );
}

//Call to the above function using 2 extra variables


so the relation becomes more evident
int argument1 = 4;
int argument2 = 3;
int result = subtraction_function( argument1,
argument2 );
// will have the same result as
int result = subtraction_function( 4, 3 );

#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;
}

using namespace std;


void swap(int &x, int &y)
{
x=y;
y=x;
}

#include <iostream>
using namespace std;

int main()
{
int number;
int abs_number;

// Ask for input


cout << "This program finds the absolute value of an integer." << endl;
cout << "Enter an integer (positive or negative): ";
cin >> number;
// Find the absolute value
if(number >= 0)
{
abs_number = number;
}
else
abs_number = -number;
// Print out output
cout << "The absolute value of " << number << " is " << abs_number;
cout << endl;
return 0;
}

int Abs(int i); // Function prototype


int main()
{
int number;
int abs_number;
cout << "This program finds the absolute value of an integer." << endl;
cout << "Enter an integer (positive or negative): ";
cin >> number;
// Calling the function Abs()
abs_number = Abs(number);
cout << "The absolute value of " << number << " is " << abs_number;
cout << endl;
return 0;
}
// Function definition
int Abs(int i)
{
if( i >= 0)
return i;
else
return -i; }

#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;
}

void foo(int &x)


{
int y = 4;
x = 10;
cout << x << " " << y;
}

#include <iostream>
using namespace std;

void foo(int x, int y);

int main()
{
int x=1, y=2;
foo(y, x);
system("pause");
return 0;
}

void foo(int x, int y)


{
cout << "X = " << x << " Y = " << y << endl;
return;
}

#include <iostream>
using namespace std;

void doStuff(int par1Value, int& par2Ref);


//par1Value is a call-by-value formal parameter and
//par2Ref is a call-by-reference formal parameter.

int main( )
{
int n1, n2;

void doStuff(int par1Value, int& par2Ref)


{
par1Value = 111;
cout << "par1Value in function call = "
<< par1Value << endl;
par2Ref = 222;
cout << "par2Ref in function call = "
<< par2Ref << endl;
}

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;

3. What is the output of the following program?


#include <iostream>
using namespacestd;
Void figureMeOut(int& x, inty, int& z);
intmain( )
{
inta, b, c;
a = 10;
b = 20;
c = 30;
figureMeOut(a, b, c);
cout << a << " " << b << " " << c << endl;
return0;
}
voidfigureMeOut(int& x, inty, int& z)
{
cout << x << " " << y << " " << z << endl;
x = 1;
y = 2;
z = 3;
cout << x << " " << y << " " << z << endl;
}

for(i=1; i<=10; i++)


{
result = Factorial(i);
cout << i << "! = " << result << endl;
}
and:
binomialCoefficient = Factorial(n)/
(Factorial(k) * Factorial(n-k));

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");}

Rotate 180 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(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;
}

Find maximum value

//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;
}