0% found this document useful (0 votes)
9 views

Functions

Uploaded by

shivani.garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Functions

Uploaded by

shivani.garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Functions

The main Function


• In C++, the main() returns a value of type int to the operating system.
• The functions that have a return value should use the return
statement for termination. The main() function in C++ is, therefore
defined as follows:
int main()
{
……
……
return 0;
}
Function Prototyping
• The prototype describes the function interface to the compiler by giving details
such as the number and types of arguments and the type of return values.
• Function prototype is a declaration statement in the calling program and is of
the following form:
type function-name (argument-list);
• The argument-list contains the types and names of arguments that must be
passed to the function.
• Example
float volume (int x, float y, float z);
• In a function declaration, the names of the arguments are dummy variables
and therefore they are optional, which means, the form
float volume (int, float, float);
is acceptable at the place of declaration.
Call by Reference
• Provision of reference variables in C++ permits us to pass parameters
to the functions by reference.
• When arguments are passed by reference, the ‘formal’ arguments in
the called function become aliases to the ‘actual’ arguments in the
calling function.
Return by Reference
• A function can also return a reference. Consider the following
function:
int & max(int &x, int &y)
{
if (x>y)
return x;
else
return y;
}
• Since the return type of max() is int &, the function returns reference
to x or y.
Inline Functions
• An inline function is a function that is expanded in line when it is invoked.
That is, the compiler replaces the function call with the corresponding
function code.
• The inline functions are defined as follows:
inline function-header
{
function body
}
• Example:
inline double cube(double a)
{
return a*a*a;
}
Default Arguments
• C++ allows us to call a function without specifying all its arguments. In
such cases, the function assigns a default value to the parameter
which does not have a matching argument in the function call.
• Default values are specified when the function is declared. The
compiler looks at the prototype to see how many arguments a
function uses and alerts the program for possible default values.
float amount (float principal, int period, float rate=0.15);
• A subsequent function call like
value = amount (5000,7);
• passes the value of 5000 to principal and 7 to period and then lets
the function use default value of 0.15 for rate.
Function Overloading
• A feature of OOP where two or more functions can have the same
name but different parameters.
• In function overloading, function name should be the same and the
arguments should be different. This is known as function
polymorphism in OOP.
• Function overloading allows programmers to design a family of
functions with one function name but different argument lists.
• The correct function to be invoked is determined by checking the
number and type of arguments but not on the function type.
• For example, an overloaded add() function handles different types of
data as shown.
• A function call first matches the prototype having the same number of
arguments and then calls the appropriate function for execution.
• A best match must be unique.
• The function selection involves the following steps:
1. The compiler first tries to find an 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.
3. When either of them fails, the compiler tries to use the built-in conversions
to the actual arguments and then uses the function whose match is unique.
If the conversion is possible to have multiple matches, then the compiler
will generate error message.
4. If all of the steps fail, then the compiler will try the user-defined conversions
in combination with integral promotions and built-in conversions to find a
unique match. User-defined conversions are often used in handling class
objects.

You might also like