Lecture 67
Lecture 67
Lecture 6-7
What is function????
• Library functions
– These are the in- -built functions of ‘C++ ’library.
– These are already defined in header files.
– e.g. Cout<<; is a function which is used to print at output.
It is defined in ‘iostream.h ’ file .
• User defined functions.
– Programmer can create their own function in C++ to
perform specific task
Why use function?
• Writing functions avoids rewriting of the same code
again and again in the program.
• Using function large programs can be reduced to
smaller ones. It is easy to debug and find out the
errors in it.
• Using a function it becomes easier to write program
to keep track of what they are doing.
//Function Declaration
retn_type func_name(data_type 1,data_type par2);
//Function Defination
rent_type func_name(data_type par1,data_type par2)
{
// body of the function
}
//Function Call
func_name(par1,par2);
Function prototype
}
void sum( int x, int y) // function definition
{
int c=x+y;
cout<< “ sum is”<<c;
}
#include<conio.h>
int sum(int, int);
main()
{
int a=10,b=20;
int c=sum(a,b); /*actual arguments
Cout<<“sum is” << c;
getch();
}
int sum(int x, int y) /*formal arguments
{
int s;
s=x+y;
return(s); /*return value
}
Where does the execution of the
program starts?
a) user-defined function
b) main function
c) void function
d) else function
Where does the execution of the
program starts?
a) user-defined function
b) main function
c) void function
d) else function
What are mandatory parts in the
function declaration?
a) return type, function name
b) return type, function name, parameters
c) parameters, function name
d) parameters, variables
What are mandatory parts in the
function declaration?
a) return type, function name
b) return type, function name, parameters
c) parameters, function name
d) parameters, variables
What is the scope of the variable
declared in the user defined function?
a) whole program
b) only inside the {} block
c) the main function
d) header section
What is the scope of the variable
declared in the user defined function?
a) whole program
b) only inside the {} block
c) the main function
d) header section
Categories of functions
#include<iostream>
using namespace std;
int main()
{
int a=10,b=20;
void mul(int,int);
mul(a,b); /*actual arguments
getch();
}
void mul(int x, int y) /*formal arguments
{
int s;
s=x*y;
cout<<“mul is” << s;
}
A function with parameter and return value
#include<iostream>
using namespace std; int max(int x, int y)
int main() {
if(x>y)
{ return(x);
else
int a=10,b=20,c; {
return(y);
int max(int,int); }
}
c=max(a,b);
cout<<“greatest no is” <<c;
}
A function without parameter and return value
#include<iostream>
using namespace std;
int main()
{
int a=10,b=20;
int sum();
int c=sum(); /*actual arguments
Cout<<“sum is”<< c;
}
int sum() /*formal arguments
{
int x=10,y=20;
return(x+y); /*return value
}
What is the default return
type of a function ?
A. int
B. void
C. float
D. char
What is the default return
type of a function ?
A. int
B. void
C. float
D. char
What is the output of this program?
1. #include<iostream>
2. using namespace std;
3. int sum(int x, int y, int z=0, int w=0) // Here there are two values in the
default arguments
4. { // Both z and w are initialised to zero
5. return (x + y + z + w); // return sum of all parameter values
6. }
7. int main()
8. {
9. cout << sum(10, 15) << endl; // x = 10, y = 15, z = 0, w = 0
10. cout << sum(10, 15, 25) << endl; // x = 10, y = 15, z = 25, w = 0
11. cout << sum(10, 15, 25, 30) << endl; // x = 10, y = 15, z = 25, w = 30
12. return 0;
13. }
Characteristics for defining the default
arguments
#include<iostraam.h>
#include<conio.h>
int main() {
cout<<"Entar name"<<endl;
cout<<"My name is Ram";
return 0;
}
Output
Enter name
My name is Ram
Dec, Oct , Hex Manipulator
All the numbers are displayed and read in decimal
notation by default. However, you may change the
base of an integer value to octal or hexadecimal or
back to a decimal using the manipulator’s oct, hex
or dec, respectively. These manipulators are
preceded by the appropriate variables to be used
with.
#include<iostream>
int main() {
int i;
cout<<"Enter hexadecimal number =";
cin>>hex>>i;
cout:<<"Hexadecimal value = "<<hex<<i<<endl;
cout<<"Octal Value = "<<oct<<i<<endl;
cout<<"Dcimal Value = "<<dec<<i<<endl;
return 0;
}
Output :
Enter hexadecimal = f
Hexadecimal = f
Octal value = 17
Decimal value = 15
setbase(b) Manipulator
• 1. ______________are
used to format the data
display in CPP?
a. Iterators
b. Punctuators
c. Manipulators
d. Allocators
MCQ
• 1. ______________are
used to format the data
display in CPP?
a. Iterators
b. Punctuators
c. Manipulators
d. Allocators
2. Which of the following manipulator is used for
the representing octal equivalent of a given
decimal number ?
a. oct
b. setbase(8)
c. tobase(8)
d. both a and b
2. Which of the following manipulator is used for
the representing octal equivalent of a given
decimal number ?
a. oct
b. setbase(8)
c. tobase(8)
d. both a and b
3.Predict the output:
float x= 3.1496;
cout<<setpricision(2)<<x;
a. 3.14
b. 3.15
c. 3.00
d. None of these
3.Predict the output:
float x= 3.1496;
cout<<setpricision(2)<<x;
a. 3.14
b. 3.15
c. 3.00
d. None of these
ANSWER: d. None of these
Explanation: setprecision(int ) is predefined manipulator. Due to spelling mistake here,
compiler will generate error.
FUNCTION OVERLOADING
Overloading in C++
❑What is overloading
– Overloading means assigning multiple
meanings to a function name or operator
symbol
– It allows multiple definitions of a function with
the same name, but different signatures.
❑C++ supports
– Function overloading
– Operator overloading
Why is Overloading Useful?
• Type Conversion.
• Function with default arguments.
• Function with pass by reference.
Type Conversion:
#include<iostream>
using namespace std;
void fun(int);
void fun(float);
void fun(int i)
{
std::cout << "Value of i is : " <<i<< std::endl;
}
void fun(float j)
{
std::cout << "Value of j is : " <<j<< std::endl;
}
int main()
{
fun(12);
fun(1.2);
return 0;
}
The above example shows an error "call of overloaded 'fun(double)' is
ambiguous". The fun(10) will call the first function. The fun(1.2) calls the
second function according to our prediction. But, this does not refer to any
function as in C++, all the floating point constants are treated as double
not as a float. If we replace float to double, the program works. Therefore,
this is a type conversion from float to double.
Function with Default Arguments
#include<iostream>
using namespace std;
void fun(int);
void fun(int,int);
void fun(int i)
{
std::cout << "Value of i is : " <<i<< std::endl;
}
void fun(int a,int b=9)
{
std::cout << "Value of a is : " <<a<< std::endl;
std::cout << "Value of b is : " <<b<< std::endl;
}
int main()
{
fun(12);
return 0;
}
The above example shows an error "call of overloaded 'fun(int)' is
ambiguous". The fun(int a, int b=9) can be called in two ways: first is by
calling the function with one argument, i.e., fun(12) and another way is
calling the function with two arguments, i.e., fun(4,5). The fun(int i)
function is invoked with one argument. Therefore, the compiler could not
be able to select among fun(int i) and fun(int a,int b=9).
Function with pass by reference
#include <iostream>
using namespace std;
void fun(int);
void fun(int &);
int main()
{
int a=10;
fun(a); // error, which f()?
return 0;
}
void fun(int x)
{
std::cout << "Value of x is : " <<x<< std::endl;
}
void fun(int &b)
{
std::cout << "Value of b is : " <<b<< std::endl;
}
The above example shows an error "call of overloaded 'fun(int&)' is
ambiguous". The first function takes one integer argument and the second
function takes a reference parameter as an argument. In this case, the
compiler does not know which function is needed by the user as there is
no syntactical difference between the fun(int) and fun(int &).
Which of the following permits function
overloading on c++?
a) type
b) number of arguments
c) type & number of arguments
d) number of objects
Which of the following permits function
overloading on c++?
a) type
b) number of arguments
c) type & number of arguments
d) number of objects
In which of the following we cannot overload the
function?
a) return function
b) caller
c) called function
d) main function
In which of the following we cannot overload the
function?
a) return function
b) caller
c) called function
d) main function
Scope of Variables in C++
• Output:
• Age is: 18
Global Variables
// global variable
int global = 5;
// main function
int main()
{
// local variable with same
// name as that of global variable
int global = 2;
cout << global << endl;
}
How to access a global variable when there is a local variable
with same name?
1. Scope Resolution
operator
2. Address operator
3. Assignment operator
4. Pointer operator
Q2.What is the output of the following code snippet?
class class_name
{
friend data_type function_name(argument/
s); // syntax of friend function.
};
Characteristics of a Friend function:
}
y=i;
• Output:
};
friend void min(A,B); // friend function
• 10
In the above example, min() function is friendly to
two classes, i.e., the min() function can access the
private members of both the classes A and B.
C++ Friend class
A. private
B. protected
C. public
D. Both A and B
Q1. A friend class can access ____________________ members
of other class in which it is declared as friend.
A. private
B. protected
C. public
D. Both A and B
Q2. A friend function can be
A. TRUE
B. FALSE
C. Can be true and false
D. Can not say
Q3. If class A is a friend of B, then B doesn’t become a friend of
A automatically
A. TRUE
B. FALSE
C. Can be true and false
D. Can not say
Q4.Which of the following is false?