SlideShare a Scribd company logo
Lecture 05



       Overloaded Functions
               and
                Inline Functions
Learn about:
Some functions that are not supported in C:
- Overloaded
- inline functions
- default arguments.

Ref.: OOP using C++, Joyce Farrel, Thopson Learning
                                                      1
Overloaded Functions (page 146-148)


• Overloaded functions are functions that have
  similar name but each performs different
  activities depending on the kind of data sent to
  the function.

• The compiler uses the number of arguments,
  and their data types, to distinguish one
  function from another.

• Function overloading is not allowed in C.
                                        2/15
Function Overloading
                        Example 1
#include <iostream.h>       void repchar(char ch)
void repchar();             { for(int j=0; j<20; j++)
void repchar(char);             cout << ch;
void repchar(char, int);      cout << endl;
                            }
int main()
{                           void repchar(char ch, int n)
  repchar();                { for(int j=0; j<n; j++)
  repchar('=');                   cout << ch;
  repchar('+', 30);           cout << endl;
  return 0;                 }
}
void repchar()              Output:
{ for(int j=0; j<20; j++)      ********************
    cout << '*';               ====================
  cout << endl;                +++++++++++++++++++++++++
                                             3/15
}
Function Overloading
                         Example 2
#include <iostream.h>      void show(double val)
                           { cout << "Double: " << val
void show(int);            << endl;
void show(double);         }
void show(char*);
                           void show(char *val)
int main()                 { cout << "String: " << val
{ show(12);                << endl;
  show(3.1415);            }
  show("Hello World");
  return 0;                Output:
}
void show(int val)            Integer: 12
{ cout<<"Integer: "<<val      Double: 3.1415
<< endl;                      String: Hello World
}                                             4/15
Inline Functions (page 143-144)


• Functions are good for structured programming
  but incur runtime overhead.

• An inline function is written like a normal
  function but compiles into inline code which is
  inserted into the program wherever a function
  call occurs.

• Inline functions are not allowed in C.
                                           5/15
Inline Functions : Overhead

:                             memory
:                             a     10
int main() {
     func1(10, 20);           b     20
     :                        x     30
     :
     func1(40,50);           • passing control to the function.
     :
     :                       • placing argument values in a memory.
}                            • returning to proper point in the
                             calling program.
void func1(int a, int b) {
     :                       • providing a memory for any returned
     x = a + b;              value
     :
}                                                        6/15
Inline Functions: Example 1

#include <iostream.h>
inline void lbstokg(float pounds)
{
  0.453592 * pounds;
}

void main()
{ float lbs;
  cout<<"Enter weight in pounds: ";
  cin>>lbs;
  cout<<"Your weight in kg is "
      << lbstokg(lbs) << endl;
}

                               0.453592 * lbs
                                                7/15
Inline Functions: Example 2

#include <iostream.h>
inline double computeGross(double hours, double rate)
{
  return (hours * rate);
}

void main()
{
  double hrsWorked = 37.5, rateOfPay = 12.45, gross;
  gross = computeGross(hrsWorked, rateOfPay);
  cout<<endl<<“Gross pay is “<<gross;
}


                          hrsWorked * rateOfPay
                                                  8/15
Inline Functions Vs Macros

• Inline functions are similar to macros declared
  with the #define directive. The major
  differences are:

  – Inline functions are recognized by the compiler.
    Macros are implemented by a simple text
    substitution.

  – Compiler performs type checking on the parameters
    of an inline function. Macros have unwanted side
    effects.
                                             9/15
Inline Functions Vs Macros
                          Example

#include <iostream.h>
#define MAX(A,B) ( (A) > (B) ? (A) : (B))
inline int max(int a, int b) void main()
{ if (a > b)                 { int result,x,y;
     return a;                  x = 23; y = 45;
                                result = MAX(x++,   y++);
   else                         cout << x << y <<   "n";
     return b;
}                               x = 23; y = 45;
                                result = max(x++,   y++);
                                cout << x << y <<   "n";
                             }

Output from the program:
2447
                                              10/15
2446
Default Arguments (page 144 - 146)

#include <iostream.h>

void compute (int length, int width, int height)
{
  ………..
}

void main()
{ ……
  compute (12, 7);        // cause syntax error
}
                                                   11/15
Default Arguments (page 144 - 146)


• A function can be called without specifying all
  its arguments.

• The function declaration must provide default
  values for those arguments that are not
  specified.

• Default arguments are not allowed in C.

                                         12/15
Default Arguments: Example 1

#include <iostream.h>

void repchar(char='*', int=20);

void main()
{ repchar();        // print 20 '*'
  repchar('=');     // print 20 '='
  repchar('+', 25); // print 25 '+'
}
                             Output:
void repchar(char ch, int n)    ********************
{                               ====================
  for(int j=0; j<n; j++)        +++++++++++++++++++++++++
    cout << ch;
  cout << endl;
}
                                              13/15
Default Arguments: Example 2

#include <iostream.h>

void showstring(char *str = "Hello World!")
{ cout << str << endl;
}

void main()
{   showstring("Here's an explicit argument.");
    showstring(); // in fact this says:
                   // showstring("Hello World!");
}
 Output:
    Here's an explicit argument.
    Hello World!
                                              14/15
Default Arguments: Example 3

#include <iostream.h>
void example(int, int = 5, float = 6.78);

void main()
{   example(7, 2, 9.3);
    example(7, 2);
    example(7);
}

void example(int x, int y, float z);
{   cout << x << y << z << endl;
}
    Output:
                7 2 9.3
                7 2 6.78
                7 5 6.78                    15/15

More Related Content

PPTX
Learning C++ - Functions in C++ 3
PPTX
Lambda Expressions in C++
PPT
C++ functions presentation by DHEERAJ KATARIA
PPT
Functions in C++
PDF
03 function overloading
PPTX
C++ programming function
PDF
C++ Programming - 1st Study
PPT
Function overloading(C++)
Learning C++ - Functions in C++ 3
Lambda Expressions in C++
C++ functions presentation by DHEERAJ KATARIA
Functions in C++
03 function overloading
C++ programming function
C++ Programming - 1st Study
Function overloading(C++)

What's hot (20)

PDF
C++ Programming - 11th Study
PPTX
C++20 features
PDF
Monads in Swift
PPT
Lecture#6 functions in c++
PPTX
Operator overloading2
PDF
Fun with Lambdas: C++14 Style (part 1)
PDF
Advanced functional programing in Swift
PPT
Functions in c++
PPTX
Is your C# optimized
PPTX
Basic c++ programs
DOC
All VLSI programs
PPTX
Functions in C++ (OOP)
PPTX
functions of C++
PPTX
Mca 2nd sem u-4 operator overloading
PPTX
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
PDF
Cocoa heads 09112017
PPT
C++ functions
PPT
C++ Functions
PPT
C++ functions
PPTX
Operator Overloading & Type Conversions
C++ Programming - 11th Study
C++20 features
Monads in Swift
Lecture#6 functions in c++
Operator overloading2
Fun with Lambdas: C++14 Style (part 1)
Advanced functional programing in Swift
Functions in c++
Is your C# optimized
Basic c++ programs
All VLSI programs
Functions in C++ (OOP)
functions of C++
Mca 2nd sem u-4 operator overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
Cocoa heads 09112017
C++ functions
C++ Functions
C++ functions
Operator Overloading & Type Conversions
Ad

Viewers also liked (20)

PPT
Lecture5
PDF
Best javascript course
PPTX
Lecture 5, c++(complete reference,herbet sheidt)chapter-15
PPTX
Inline function in C++
PDF
02. functions & introduction to class
PDF
C++ Object oriented concepts & programming
PPTX
Operator overloadng
PDF
Operator overloading in C++
PPTX
Inline function
PPTX
Friend functions
PPTX
OPERATOR OVERLOADING IN C++
PDF
A COMPLETE FILE FOR C++
PPTX
Inline function in C++
PPTX
[OOP - Lec 19] Static Member Functions
PPTX
Friend function & friend class
PPT
08 c++ Operator Overloading.ppt
PPTX
Object Oriented Programming Using C++
PPT
Operator Overloading
PDF
Classes and objects
PPT
friend function(c++)
Lecture5
Best javascript course
Lecture 5, c++(complete reference,herbet sheidt)chapter-15
Inline function in C++
02. functions & introduction to class
C++ Object oriented concepts & programming
Operator overloadng
Operator overloading in C++
Inline function
Friend functions
OPERATOR OVERLOADING IN C++
A COMPLETE FILE FOR C++
Inline function in C++
[OOP - Lec 19] Static Member Functions
Friend function & friend class
08 c++ Operator Overloading.ppt
Object Oriented Programming Using C++
Operator Overloading
Classes and objects
friend function(c++)
Ad

Similar to Lecture05 (20)

PPTX
Function in C++, Methods in C++ coding programming
PPT
PPTX
6. Functions in C ++ programming object oriented programming
PDF
PPTX
Silde of the cse fundamentals a deep analysis
PPT
PPT
16717 functions in C++
 
PPTX
Inline Functions and Default arguments
PDF
Functions
PPTX
FUNCTIONS, CLASSES AND OBJECTS.pptx
PPTX
Fundamental of programming Fundamental of programming
PPTX
C++_Functions_Detailed_Presentation.pptx
PPT
Inbuilt Functions in C++ computer language.ppt
PDF
how to reuse code
PPTX
CPP Homework Help
PPTX
C++ Functions.pptx
PPTX
Functions in C++ programming language.pptx
PPTX
Function C++
PPTX
Cs1123 8 functions
PDF
Function overloading ppt
Function in C++, Methods in C++ coding programming
6. Functions in C ++ programming object oriented programming
Silde of the cse fundamentals a deep analysis
16717 functions in C++
 
Inline Functions and Default arguments
Functions
FUNCTIONS, CLASSES AND OBJECTS.pptx
Fundamental of programming Fundamental of programming
C++_Functions_Detailed_Presentation.pptx
Inbuilt Functions in C++ computer language.ppt
how to reuse code
CPP Homework Help
C++ Functions.pptx
Functions in C++ programming language.pptx
Function C++
Cs1123 8 functions
Function overloading ppt

More from elearning_portal (14)

PPT
Lecture21
PPT
Lecture19
PPT
Lecture18
PPT
Lecture17
PPT
Lecture16
PPT
Lecture10
PPT
Lecture09
PPT
Lecture07
PPT
Lecture06
PPT
Lecture20
PPT
Lecture03
PPT
Lecture02
PPT
Lecture01
PPT
Lecture04
Lecture21
Lecture19
Lecture18
Lecture17
Lecture16
Lecture10
Lecture09
Lecture07
Lecture06
Lecture20
Lecture03
Lecture02
Lecture01
Lecture04

Lecture05

  • 1. Lecture 05 Overloaded Functions and Inline Functions Learn about: Some functions that are not supported in C: - Overloaded - inline functions - default arguments. Ref.: OOP using C++, Joyce Farrel, Thopson Learning 1
  • 2. Overloaded Functions (page 146-148) • Overloaded functions are functions that have similar name but each performs different activities depending on the kind of data sent to the function. • The compiler uses the number of arguments, and their data types, to distinguish one function from another. • Function overloading is not allowed in C. 2/15
  • 3. Function Overloading Example 1 #include <iostream.h> void repchar(char ch) void repchar(); { for(int j=0; j<20; j++) void repchar(char); cout << ch; void repchar(char, int); cout << endl; } int main() { void repchar(char ch, int n) repchar(); { for(int j=0; j<n; j++) repchar('='); cout << ch; repchar('+', 30); cout << endl; return 0; } } void repchar() Output: { for(int j=0; j<20; j++) ******************** cout << '*'; ==================== cout << endl; +++++++++++++++++++++++++ 3/15 }
  • 4. Function Overloading Example 2 #include <iostream.h> void show(double val) { cout << "Double: " << val void show(int); << endl; void show(double); } void show(char*); void show(char *val) int main() { cout << "String: " << val { show(12); << endl; show(3.1415); } show("Hello World"); return 0; Output: } void show(int val) Integer: 12 { cout<<"Integer: "<<val Double: 3.1415 << endl; String: Hello World } 4/15
  • 5. Inline Functions (page 143-144) • Functions are good for structured programming but incur runtime overhead. • An inline function is written like a normal function but compiles into inline code which is inserted into the program wherever a function call occurs. • Inline functions are not allowed in C. 5/15
  • 6. Inline Functions : Overhead : memory : a 10 int main() { func1(10, 20); b 20 : x 30 : func1(40,50); • passing control to the function. : : • placing argument values in a memory. } • returning to proper point in the calling program. void func1(int a, int b) { : • providing a memory for any returned x = a + b; value : } 6/15
  • 7. Inline Functions: Example 1 #include <iostream.h> inline void lbstokg(float pounds) { 0.453592 * pounds; } void main() { float lbs; cout<<"Enter weight in pounds: "; cin>>lbs; cout<<"Your weight in kg is " << lbstokg(lbs) << endl; } 0.453592 * lbs 7/15
  • 8. Inline Functions: Example 2 #include <iostream.h> inline double computeGross(double hours, double rate) { return (hours * rate); } void main() { double hrsWorked = 37.5, rateOfPay = 12.45, gross; gross = computeGross(hrsWorked, rateOfPay); cout<<endl<<“Gross pay is “<<gross; } hrsWorked * rateOfPay 8/15
  • 9. Inline Functions Vs Macros • Inline functions are similar to macros declared with the #define directive. The major differences are: – Inline functions are recognized by the compiler. Macros are implemented by a simple text substitution. – Compiler performs type checking on the parameters of an inline function. Macros have unwanted side effects. 9/15
  • 10. Inline Functions Vs Macros Example #include <iostream.h> #define MAX(A,B) ( (A) > (B) ? (A) : (B)) inline int max(int a, int b) void main() { if (a > b) { int result,x,y; return a; x = 23; y = 45; result = MAX(x++, y++); else cout << x << y << "n"; return b; } x = 23; y = 45; result = max(x++, y++); cout << x << y << "n"; } Output from the program: 2447 10/15 2446
  • 11. Default Arguments (page 144 - 146) #include <iostream.h> void compute (int length, int width, int height) { ……….. } void main() { …… compute (12, 7); // cause syntax error } 11/15
  • 12. Default Arguments (page 144 - 146) • A function can be called without specifying all its arguments. • The function declaration must provide default values for those arguments that are not specified. • Default arguments are not allowed in C. 12/15
  • 13. Default Arguments: Example 1 #include <iostream.h> void repchar(char='*', int=20); void main() { repchar(); // print 20 '*' repchar('='); // print 20 '=' repchar('+', 25); // print 25 '+' } Output: void repchar(char ch, int n) ******************** { ==================== for(int j=0; j<n; j++) +++++++++++++++++++++++++ cout << ch; cout << endl; } 13/15
  • 14. Default Arguments: Example 2 #include <iostream.h> void showstring(char *str = "Hello World!") { cout << str << endl; } void main() { showstring("Here's an explicit argument."); showstring(); // in fact this says: // showstring("Hello World!"); } Output: Here's an explicit argument. Hello World! 14/15
  • 15. Default Arguments: Example 3 #include <iostream.h> void example(int, int = 5, float = 6.78); void main() { example(7, 2, 9.3); example(7, 2); example(7); } void example(int x, int y, float z); { cout << x << y << z << endl; } Output: 7 2 9.3 7 2 6.78 7 5 6.78 15/15