Module2 C++
Module2 C++
Module-2
Functions in C++: Tokens – Keywords – Identifiers and constants – Operators in C++ – Scope resolution
operator – Expressions and their types – Special assignment expressions – Function prototyping – Call by
reference – Return by reference – Inline functions -Default arguments – Function overloading.
FUNCTIONS IN C++
2.1 Tokens
The smallest individual units in a program are known as Tokens, most of the C++ tokens similar to
the C tokens with exception of some addition and minor modification. Types of C++ tokens are as follows:
Keywords
Identifiers
Constants
Strings
Operators
Constants: constant refer to fixed values that do not change during the execution of a program
Like C, C++ supports several kinds of literal constant, they include integers, characters, floating point
numbers and strings, literal constant do not have memory location.
Example:
123 //decimal integers
12.34 //floating point integer
037 //octal integer
0X2 //hexadecimal integer
“C++” //string constant
‘A’ //character constant
L‘ab’ //wide character constant
The wchar_t type is a wide character literal introduced by ANSI C++ and intended for character sets
that cannot fit in a single byte. Wide character literal begin with letter L
// function call
cout << add ( 5, 10); //uses prototype 1
cout << add ( 5, 10.0); //use prototype 5
cout << add ( 12.5, 7.5); //use prototype 4
cout << add ( 5, 10, 15); //use prototype 2
cout << add ( 5, 10, 15, 20); //use prototype 3
cout << add ( 0.75, 5); //use prototype 5
the function call first matches the prototype having the same number and type of arguments and then call the
appropriate function for execution a best matches must be unique the function selection involves the
following steps
1. The complier tries to find and exact match in which the types of actual arguments are the same and
use that function
2. If an exact match is not found the compiler uses the integral promotions to the actual arguments such
as
char to int
float to double
3. When either ( above 2) of them fails the complier tries to use the built in conversion to the actual
arguments and then uses the function whose match in unique
long square ( long n)
double square ( double x)
#include <iostream>
using namespace std;
int volume ( int );
double volume ( double, int );
long volume ( long, int, int);
{
cout << volume (10) << “\n”;
cout << volume ( 2.5, 8) << “\n”;
cout << volume ( 100L, 75,15) << “\n”;
return 0;
}
int volume ( int s)
{
return ( s*s*s);
}
double volume (double r, int h)
{
return ( 3.14519*r*r*h);
}
long volume (long l, int b, int h )
{
return ( l*b*h);
}
Output
1000
157.26
112500
Q2. Discuss operators and its types with suitable examples. (Page 29, 30, 31)
Q3. With example program explain the scope regulation operator (Page 30, 31)
Q4. Discuss expressions and their types with suitable examples. (Page 31, 32)
Q4. What are the Special assignment expressions and explain each with suitable example. (Page 33)
Q5. Explain the function prototyping with suitable example program. (Page 33, 34)
Q6. What are reference variables in C++ (call by reference and return by reference) Discuss with examples.
(Page 35, 36)
Q7. Illustrate Inline function and its syntax. Write a C++ program to implement Inline function. (Page 36,
37)
Q8. Explain default arguments with example program (Page 37, 38)
Q9. What are steps to fallow to process in function overloading explain in detail with example program.
(Page 39, 40)