Functions Functions: Functions Functions Functions Functions
Functions Functions: Functions Functions Functions Functions
y General Format:
Format:
function type functionname (datatype argument1, datatype argument2....)
{
body of the function
------------------------
-------------------------
return something
}
y Declaration of Function Type:
y Function refers to the value it
would return to the calling portion
of the program
y e.g.
y int,
i t float,
fl t char,
h etct
y int function_name(…………
function_name(…………))
y float function_name(…………
function_name(…………))
y Function name :
y Name confirming to the syntax rules
of the variables
y Normally,
Normally function name is made
relevant with the function operation
y e.g.
y counter()
counter();;
y square()
square();;
y Formal Arguments:
y Function Prototype
y is a declaration statement in the
calling program and is of the
following form:
y type function-name (argument
function- argument- list));
-list
y Argument list consists of the types and
names off the
h arguments that
h must be b
passed to the function
y e.g.
g
y float volume(int x, float y, float z);
int main(void)
{ // Start of main function
cheers (5); // function call
cout<<"Enter
cout<< Enter a Number:\n";
Number:\n ;
double side;
cin>>side;
double volume = cube(side); //function
//call
system("pause");
return
t 0;
0
}// Main function finishes here
void cheers (int n)
{
for (int i=0; i<n;i++)
cout<<"Cheers!
cout<< Cheers! ";;
cout<<"\n";
}
Enter a Number:
2
int main()
{ // Start of main function
system("pause");
return 0;
}// Main function finishes here
float cube ((float y)
{
return y*y*y;
y y y;
}
Enter length of side of a cube in ft:
96
9.6
A9.6foot cube has a volume
of884.736cubic feet :
Press any key to continue . . .
y Till now all parameters have been passed
b value
by l
y Meaning
y Expression
p used in function call is
evaluated first
y Then the resulting value is passed on to
the corresponding parameter in the
function’ss parameter list
function
y Before function executes
y e.g.
y For cube(x) , with x having value 4
y 4 is ppassed to the local variable before
function execution starts
y e.g.
y cube(2*x-3)
cube(2*x 3)
y Expression inside the parentheses will be
evaluated to a single
g value and then the
value is passed to the function
Passing by Reference
y There are some situations where a
function needs to change the value of the
parameter p
p passed to it
y This makes
Thi k th Local
the L l Variable
V i bl a
Reference to the actual parameter
passedd to
t it
y Actual parameter is read-write
instead of read-only
// Swapping
#include <iostream>
using namespace std;
int main()
{ // Start of main function
fl t a=27.0;
float 27 0
float b=-5.041;
system("pause");
t (" ")
return 0;
}// M
Main
i ffunction
ti fi finishes
i h here
h
void swap (float& x, float& y)
{
cout<<"\n"<<"We have stepped inside the
function"<<"\n";
cout<<"\n"<<"x="<<x<<"y="<<y<<"\n";
float temp=x;
x=y;
y=temp;
cout<<"Swapping
<<"S i isi over iinside
id the
h
function"<<"\n";
cout
cout<<"\n"<<"x="<<x<<"y="<<y<<"\n";
\n x x y y \n ;
return;
}
a=27 b=-5.041
x=27y=-5.041
27 5 041
Swapping is over inside the function
x=-5.041y=27
a=-5.041 b=27
Press any key to continue . . .
y The reference operator & makes x and y
y y of the actual parameters
synonyms p passed
p
to the function
Passing By Constant Reference
y If the actual p
parameter that is p
passed to a
function takes up a lot of storage space,
y Alternative:
y passing by constant reference
y It works in the same way as passing by
reference,
f exceptt that
th t the
th function
f ti i
is
prevented from changing the value of the
parameter
t
y Effect is
Eff i that
h the
h function
f i h access to
has
the actual parameter by means of its
f
formall parameter alias,
li
#include <iostream>
using
sin namespace std;
std
void
id f(int
f(i t x, iint&
t& y, constt iint&
t& z);
)
iint main()
i ()
{ // Start of main function
int a=22, b=33,c=44;
cout<<"\n"<<" "<<"a="<<a<<" "<<"b="<<b<<" "<<"c="<<c<<"\n";
cout<<"\n"<<"Going inside the
f
function"<<"\n";
i "<<"\ "
f(a,b,c);
cout<<"\n"<<"We are out of the function"<<"\n";
cout<<"\n"<<" "<<"a="<<a<<" "<<"b="<<b<<" "<<"c="<<c<<"\n";
system("pause");
return 0;
}// Main function finishes here
void f(int x, int& y, const int& z)
{
cout<<"\n"<<"We have just entered into the function"<<"\n";
cout<<"\n"<<" "<<"x="<<x<<" "<<"y="<<y<<" "<<"z="<<z<<"\n";
x x z;
x=x+z;
y=y+z;
cout<<"\n"<<"Final Function Results at the last line of
function
function"<<"\n";
\n ;
cout<<"\n"<<" "<<"x="<<x<<" "<<"y="<<y<<" "<<"z="<<z<<"\n";
return;
}
a=22 b=33 c=44
x=22
x 22 y=33
y 33 z=44
z 44
y e.g.
y float amount(float principal, int period, float rate=0.15);
y Argument rate has a default vale of 0.15
y value=amount(5000,7);
y Instead,,
y value = amount(5000,5,0.12);
y Rate is no more default but now is 0.12
0 12