0% found this document useful (0 votes)
17 views18 pages

03.revision Series-4 Basics of C++

This document provides an overview of functions in C++, including the definition and syntax of inline functions, their advantages and disadvantages, and the use of default arguments. It explains how inline functions can enhance program efficiency but may increase executable size and require recompilation upon changes. Additionally, it outlines scenarios where the compiler may not perform inlining.

Uploaded by

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

03.revision Series-4 Basics of C++

This document provides an overview of functions in C++, including the definition and syntax of inline functions, their advantages and disadvantages, and the use of default arguments. It explains how inline functions can enhance program efficiency but may increase executable size and require recompilation upon changes. Additionally, it outlines scenarios where the compiler may not perform inlining.

Uploaded by

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

Basics of C++

Revision Series -4
Educator: Twinkle S. Panchal
Assistant Professor
Sutex Bank College of Computer
Applications and science
Previous Lecture Highlights
1. C ++ was developed by BJarne Stroustrup at AT & T Bell lab, USA in
early 1980s.

2. cout is an output operator also called insertion operator


3. Example: cout<<”Hello World”;
4. cin is an input operator also called Extraction Operator
5. Example: cin>>number1;
6. In c++ int main() is used where we return 0 and 0 means “successful
execution of the program”.
Let us understand
FUNCTIONS IN C++
Functions
1. Using functions, programs can be divided into small units(functions)
which can be reused in future.

1. Functions helps to reduce the size of a program by calling and using


them at different places in the program.
Function Prototype/syntax

Return_type function_name (argument_list);

For Example:

1. Float volume (int x, int y, float z);


2. Float volume (int, int, float);
3. Float volume (int x, int y, z); \\ invalid
INLINE FUNCTION
C++ inline function is powerful concept that is commonly used with classes.

If a function is inline, the compiler places a copy of the code of that


function at each point where the function is called at compile time.

To inline a function, place the keyword inline before the function name and
define the function before any calls are made to the function.

The compiler can ignore the inline keyword in case defined function is more
than a line.
The inline function syntax as follows:-

inline return_type function-name (argument list)

function body;

Example:

inline double cube (double a)

{ return(a*a*a); }

The above inline function can be invoked by statements like

c=cube(3.0); d=cube(2.5);
Example for Inline Function
inline int Max(int x, int y) {

return (x > y)? x : y;

int main() { // Main function for the program

cout << "Max (20,10): " << Max(20,10) << endl;

cout << "Max (0,200): " << Max(0,200) << endl;

cout << "Max (100,1010): " << Max(100,1010) << endl;

return 0; }
Advantages of Inline Function
1. It speeds up your program by avoiding function calling.

2. It save time to return call from a function.


Disadvantages of Inline Function
1. It increases the executable size due to code expansion.
2. C++ inlining is resolved at compile time. Which means if you change the
code of the inlined function, you would need to recompile all the code
using it to make sure it will be updated
Compiler may not perform inlining in such circumstances like:
1) If a function contains a loop. (for, while, do-while)

2) If a function contains static variables.

3) If a function is recursive.(where function call itself)

4) If a function return type is not void, and the return statement doesn’t
exist in function body.

5) If a function contains switch or goto statement.


FUNCTIONS WITH DEFAULT ARGUMENT

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.

Example: float amount (float principle, int year,float rate=0.15);

Call: cout<<”Amount ”<<amount(1000,4);


LET’S REVISE IT AGAIN !!
_____ Keyword is use to declare inline
function
In Inline Function _____ is replaced with
_____
List out any two cases where compiler ignore
inline function.
In which type of function, default arguments are
provided in argument list ?
THANK YOU

You might also like