Professional Documents
Culture Documents
Chapter 1
Chapter 1
Chapter 1
Chapter One
Function in C++
Why Function?
You put a bookmark in your book, make the phone call, and
when you are done with the phone call, you return to your book
where you left off.
Each function has its own name, and when that name is
encountered, the execution of the program branches to the body
of that function. When the function returns, execution resumes on
the next line of the calling function.
Cont’d
Built-in(predefined) function
are part of your compiler package in which they are
supplied by the manufacturer for your use.
Some of the predefined functions are pow(x, y), sqrt(x),
and floor(x) which calculates the largest whole number that
is less than or equal to x. For Ex:- pow(2, 3)= 8.0, sqrt(2.25)
is 1.5, floor(48.79) is 48.0.
N.B To use predefined function in a program, you must
include the header file (cmath and cctype) that contains the
function specification.
Other predefined functions and their purpose
Example
Varieties Of Function Cont’d
agree exactly about the return type, the name, and the
parameter list. If they do not agree, you will get a
compile-time error.
The function prototype does not need to contain the
N.B
All functions have a return type. If none is explicitly stated,
the return type defaults to int.
Function prototype - a function declaration that does not include
the body of the function and ends with ;.
Function definition – a function declaration that includes the
body of the function with in { }.
Exercise
1. Write a value returning function that receives three integers and returns
the largest of the three. Assume the integers are not equal to one
another.
2. Write a value returning function that receives two floating point
numbers and returns true if the first formal parameter is smaller than
the second.
3. Write a value returning function that receives a character and returns
true if the character is a vowel and false otherwise. For this example,
vowels include the characters ‘A/a', ‘E/e', ‘I/i', ‘O/o', and ‘U/u‘.
4. Do the above exercise(1-3) in a void function.
5. Write a void function that receives an integer values and outputs
the message palindrome if a number is palindrome number,
unless it displays a message as number is not palindrome.
Calling the function
# include<iostream>
using namespace std;
void func1(); //prototype of function definition
int main()
{
……
func1();//function call (notice the semicolon)
……
}
void func1() // notice no semicolon
{ function
function body definition
} // notice no semicolon
Example
# include<iostream>
using namespace std;
void Greeting(); //prototype of function definition
int main()
{
Greeting();//function call (notice the semicolon)
Cout<<“hello again”;
cout<<“I wanna say ”;
return 0;
}
void Greeting() // notice no semicolon in function definition .
{
cout<<“Well come to Interesting class!”;// function body
example
# include<iostream>
Using namespace std;
int Sum(int, int);
Int main() {
int x, y, result ;
cout<<“Enter two integers to get their sum ”;
cin>>x>>y;
result = Sum(x,y); //function call (notice the semicolon)
cout<<“\n The sum is : ”<<result;
return 0;
}
int Sum(int a, int b) { Or you can simply put this in
int c; the body
c = a+b; return (a+b);
return c;
}
Example (without prototype)
# include<iostream>
using namespace std;
int Sum(int a, int b){
int c;
c = a+b;
return c;
}
int main(){
int x, y, result ;
cout<<“Enter two integers to get their sum ”;
cin>>x>>y;
result = Sum(x,y); //function call (notice the semicolon)
cout<<“\n The sum is : ”<<result;
return 0;
Scope of variables
When a local variable has the same name as a global variable, all
references to the variable name made with in the scope of the
local variable access the local variable
# include<iostream>
using namespace std;
float num = 42.8; //global variable
int main()
{
float num = 26.4; //local variable
cout<<”the value of num is:”<<num<<endl;
return 0; The output :the value of num is: 26.4
}
Cont’d
# include<iostream>
using namespace std;
float num = 42.8; //global variable
int main()
{
float num = 26.4; //local variable
cout<<”the value of num is:”<< ::num<<endl;
return 0;
} The output is: the value of the number is 42.8
Function arguments
#include <iostream>
using namespace std;
void swap(int x, int y);
int main(){
int x = 5, y = 10;
cout << "Main. Before swap, x: " << x << " y: " <<y<< "\n“;
swap(x,y); Output:
cout << "Main. After swap, x: " << x << " y: " <<y << "\n“; Main. Before swap, x: 5 y:
return 0; 10
} Swap. Before swap, x: 5 y:
void swap (int x, int y) 10
{ Swap. After swap, x: 10 y: 5
int temp; Main. After swap, x: 5 y: 10
cout << "Swap. Before swap, x: " << x << " y: " <<y<< "\n“;
temp=x;
x=y;
y=temp;
cout << "Swap. After swap, x: " << x << " y: " << y << "\n“;
}
Cont’d
2) Pass by Reference
In C++, passing by reference is accomplished in two ways: using
pointers and using references. The syntax is different, but the
net effect is the same. Rather than a copy being created within the
scope of the function, the actual original object is passed into the
function. Passing an object by reference allows the function to
change the object being referred to.
When a variable is passed by reference we are not passing a copy
of its value, but we are somehow passing the variable itself to the
function and any modification that we do to the local variables
will have an effect in their counterpart variables passed as
arguments in the call to the function.
Cont’d
#include <iostream>
using namespace std;
void prevnext (int x, int &prev, int &next)
{
prev = x-1; The output is:
next = x+1; Previous=99, Next=101
}
int main ()
{
int x=100, y, z;
prevnext (x, y, z);
cout << "Previous=" << y << ", Next=" << z;
return 0;
}
Cont’d
#include <iostream>
using namespace std;
void prevnext (int x, int *prev, int *next)
{
*prev = x-1; The output is:
*next = x+1; Previous=99, Next=101
}
int main (){
int x=100, y, z;
prevnext (x, y, z);
cout << "Previous=" << y << ", Next=" << z;
return 0; }
Cont’d
#include<iostream>
using namespace std;
float AreaOfCircle(int r){
float pie = 3.14;
return (pie*r*r);}
float AreaOfCylinder(int Area, int h) {
return(Area*h);}
void main(){
float radius = 4.0, h= 2.0, CylinderArea ;
CylinderArea = AreaOfCylinder(AreaOfCircle(radius), h);
cout<<“The area of the cylinder is : ” << CylinderArea ;
}
Default parameters
#include<iostream>
using namespace std;
int addition(int a, int b=1)
{
return(a+b);
}
int main()
{ Output:
cout<<addition(5,2)<<endl; 7
5
cout<<addition(4);
return 0;
}
Cont’d
#include<iostream>
using namespace std;
int AreaOfCube (int height=1, int length=1, int width=1); Output:
int main() 1
2
{
8
cout<< AreaOfCube ()<<endl; 48
cout<< AreaOfCube ( 2) <<endl;
cout<< AreaOfCube (2,4) <<endl;
cout<< AreaOfCube (2,4,6);
}
int AreaOfCube (int height , int length, int width )
{
return(height* length*width); }
Overloaded functions
Suppose that you wrote a function that returned the absolute value of what
ever number you passed to it:
int iabs(int i) {
if(i<0)
return (i*-1);
else Without using overloading, you have
return (i); } to call the function as:
float fabs(float x)
int ans = iabs(weight);//for int
{
arguments
if(x<0.0)
float ans = fabs(weight);//for float
return (x * -1.0);
arguments
else
return (x); }
Cont’d
#include<iostream>
using namespace std;
void CountDown(int nValue)
{
cout << nValue << endl;
if (nValue > 0) // termination condition
CountDown(nValue-1);
}
int main(){
CountDown(10);
return 0;}
Cont’d