SlideShare a Scribd company logo
1 of 18
FUNCTION IN C++



                     GUIDED BY
PREPARED BY
                 MR.MITESH JAISWAL
AKSHAY JANI
(116380307521)       LCTURER IN
KAUSHAL SONI      PIET(DS) 2ND SHIFT
(116380307509)
   WE ARE IMMENSLY GREATFUL TO OUR LECTURER AND PROJECT
    GUIDE, MR.MITESH JAISWAL, FOR HER INVALUABLE GUIDANCE
    WHICH GAVE US A DEEP INSIGHT ON THE SUBJECT. WITH HER
    KEEN INTEREST AND CONSTANT MORAL BOOSTING, WE ARE
    ABLE TO IMPLEMENT THE PROJECT SATISFACTORILY.
   WE EXPRESS OUR VERY SINCERE THANKS TO COMPUTER
    ENGINEERING DEPT. FOR PROVIDING ADEQUATE FACILITIES TO
    COMPLETE OUR PROJECT.
   WE ARE AGAIN CORDIALLY THANKFUL TO OUR C&E
    DEPARTMENT STAFF, FRIENDS AND OTHER PEOPLE WHO HAVE
    DIRECTLY OR INDIRECTLY HELPED US IN COMPLETION OF THIS
    PROJECT.
   LAST BUT NOT THE LEAST, WE ARE ALSO THANKFUL TO OUR
    FAMILY MEMBERS WHO ENGOURAGED AND SUPPORTED US
    ROUND THE CLOCK FOR THIS PROJECT.
                                       Akshay Jani
                                       Kaushal Soni
CERTIFICATE
This is to certify that AKSHAY JANI Student of computer
Engineering, bearing Enrollment No: 116380307521 have satisfactorily
completed his/her Seminar work as a part of course curriculum in
Diploma Engineering semester III having a report title “ FUNCTIONS IN
C++ ”.




                MR.MITESH JAISWAL
                     Lecturer, computer Dept.
                     PIETDS-2nd Shift, Limda.

         PARUL INSTITUTE OF ENGINEERING & TECHNOLOGY
                 COMPUTER ENGG. DEPARTMENT
                  LIMDA, VAGHODIA, VADODARA
This is to certify that KAUSHAL SONI Student of computer
Engineering, bearing Enrollment No: 116380307509 have satisfactorily
completed his/her Seminar work as a part of course curriculum in
Diploma Engineering semester III having a report title “ FUNCTIONS IN
C++ ”.




                MR.MITESH JAISWAL
                     Lecturer, computer Dept.
                     PIETDS-2nd Shift, Limda.

         PARUL INSTITUTE OF ENGINEERING & TECHNOLOGY
                 COMPUTER ENGG. DEPARTMENT
                  LIMDA, VAGHODIA, VADODARA
FUNCTIONS IN C++
   Experience has shown that the best way to develop and maintain large
    programs is to construct it from smaller pieces(Modules)
   This technique Called “Divide and Conquer”
    Bad Development Approach                             Wise Development Approach

                                                               main()
    main()                        •Easer To                    {
    {                                                            -----
       -----                      Design                         ----
       -----                      Build                       }
       -----                      Debug
       -----                      Extend                      function f1()
       .                          Modify                      {
       .                          Understand                     ---
       .                          Reuse                          ---
       ----                       Better Organization         }
       -----
       -----                                                   function f2()
    Return 0;                                                  {
    }                                                             ---
                                                                  ---
                                                               }
FUNCTIONS IN C++(CONT.)

   In FORTRAN Modules Known as Subprograms
   In Pascal Modules known as Procedures &
    Functions
   In C++ Modules Known as Functions & Classes
   Programs use new and “prepackaged” modules
       New: programmer-defined functions and classes
       Prepackaged: from the standard library
ABOUT FUNCTIONS IN C++
        Functions invoked by a function–call-statement which consist of
         it’s name and information it needs (arguments)
        Boss To Worker Analogy
           A Boss (the calling/caller function) asks a worker (the called
         function) to perform a task and return result when it is done.
                                       Boss
                                      Main

                                                                           Worker
Worker              Worker
                                                                    Function Z
Function A           Function B

          Worker             Worker
                                              Note: usual main( ) Calls other
            Function B1        Function B2    functions, but other functions
                                                    can call each other
ADVANTAGES OF USING FUNCTIONS

1. To help make the program more
   understandable
2. To modularize the tasks of the program
     building blocks of the program
3. Write a module once
     those lines of source code are called multiple
      times in the program


                                                       8
ADVANTAGES OF USING FUNCTIONS
4. While working on one function, you can focus on
   just that part of the program
     construct it,
     debug it,
     perfect it.
5. Different people can work on different functions
    simultaneously.
6. If a function is needed in more than one place in a
    program, or in different programs, you can write it
    once and use it many times


                                                      9
FUNCTION OVERLOADING
   Function overloading
     Functions with same name and different
      parameters
     Should perform similar tasks
         I.e.,   function to square ints and function to square floats
          int square( int x) {return x * x;}
          float square(float x) { return x * x; }
   A call-time c++ complier selects the proper function by
    examining the number, type and order of the parameters
USING DEFAULT ARGUMENTS

   When you don’t provide enough arguments in a function
    call, you usually want the compiler to issue a warning
    message for this error
   Sometimes it is useful to create a function that supplies a
    default value for any missing parameters




                                                                  11
USING DEFAULT ARGUMENTS




   Two rules apply to default parameters:
       If you assign a default value to any variable in a function
        prototype’s parameter list, then all parameters to the right of that
        variable also must have default values
       If you omit any argument when you call a function that has default
        parameters, then you also must leave out all arguments to the right
        of that argument


                                                                               12
EXAMPLES OF LEGAL AND ILLEGAL
USE OF FUNCTIONS WITH DEFAULT
PARAMETERS




                                13
OVERLOADING FUNCTIONS

   In most computer programming languages, each
    variable used in a function must have only one
    name, but C++ allows you to employ an alias

   Similarly, in most computer programming
    languages, each function used in a program must have
    a unique name

   You don’t have to use three names for functions that
    perform basically the same task, C++ allows you to
    reuse, or overload, function names


                                                           14
OVERLOADING FUNCTIONS




   When you overload a function, you must ensure that the
    compiler can tell which function to call
   When the compiler cannot tell which version of a function to
    use, you have created ambiguity




                                                                   15
THREE OVERLOADED FUNCTIONS
THAT PERFORM SIMILAR TASKS




                             16
POSTSCRIPT

   "The important thing is not to stop
    questioning; never lose a holy curiosity."



                                     -- Albert
    Einstein
C++ 2

More Related Content

What's hot

Mainframe jcl exec and dd statements part - 3
Mainframe jcl exec and dd statements part - 3Mainframe jcl exec and dd statements part - 3
Mainframe jcl exec and dd statements part - 3janaki ram
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directivesVikash Dhal
 
Modules in Python Programming
Modules in Python ProgrammingModules in Python Programming
Modules in Python Programmingsambitmandal
 
Functions assignment
Functions assignmentFunctions assignment
Functions assignmentAhmad Kamal
 
Functions in c
Functions in cFunctions in c
Functions in creshmy12
 
C programming language working with functions 1
C programming language working with functions 1C programming language working with functions 1
C programming language working with functions 1Jeevan Raj
 
Modular programming
Modular programmingModular programming
Modular programmingbhuwanbist1
 
Shell Scripting With Arguments
Shell Scripting With ArgumentsShell Scripting With Arguments
Shell Scripting With ArgumentsTechronology Inc.
 

What's hot (19)

Mainframe jcl exec and dd statements part - 3
Mainframe jcl exec and dd statements part - 3Mainframe jcl exec and dd statements part - 3
Mainframe jcl exec and dd statements part - 3
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
 
Modules in Python Programming
Modules in Python ProgrammingModules in Python Programming
Modules in Python Programming
 
Functions assignment
Functions assignmentFunctions assignment
Functions assignment
 
Functions in c
Functions in cFunctions in c
Functions in c
 
C# programs
C# programsC# programs
C# programs
 
Savitch ch 11
Savitch ch 11Savitch ch 11
Savitch ch 11
 
C language 3
C language 3C language 3
C language 3
 
Preprocessors
Preprocessors Preprocessors
Preprocessors
 
ANSI C Macros
ANSI C MacrosANSI C Macros
ANSI C Macros
 
C programming language working with functions 1
C programming language working with functions 1C programming language working with functions 1
C programming language working with functions 1
 
Modular programming
Modular programmingModular programming
Modular programming
 
Savitch Ch 11
Savitch Ch 11Savitch Ch 11
Savitch Ch 11
 
Preprocessors
PreprocessorsPreprocessors
Preprocessors
 
pre processor directives in C
pre processor directives in Cpre processor directives in C
pre processor directives in C
 
Preprocessor in C
Preprocessor in CPreprocessor in C
Preprocessor in C
 
Shell Scripting With Arguments
Shell Scripting With ArgumentsShell Scripting With Arguments
Shell Scripting With Arguments
 
FUNCTION CPU
FUNCTION CPUFUNCTION CPU
FUNCTION CPU
 
Modular programming
Modular programmingModular programming
Modular programming
 

Viewers also liked

De presantation
De presantationDe presantation
De presantationjani
 
Swappin Deck
Swappin DeckSwappin Deck
Swappin DeckSwappin
 
Tips to Take Back Your Sold WOW Account
Tips to Take Back Your Sold WOW AccountTips to Take Back Your Sold WOW Account
Tips to Take Back Your Sold WOW Accountclutch132
 
Concepts of oop
Concepts of oopConcepts of oop
Concepts of oopjani
 
Monografija Gimnazije Lebane
Monografija Gimnazije LebaneMonografija Gimnazije Lebane
Monografija Gimnazije LebaneGimnazija Lebane
 

Viewers also liked (7)

De presantation
De presantationDe presantation
De presantation
 
Swappin Deck
Swappin DeckSwappin Deck
Swappin Deck
 
Rest surekha
Rest surekhaRest surekha
Rest surekha
 
Tips to Take Back Your Sold WOW Account
Tips to Take Back Your Sold WOW AccountTips to Take Back Your Sold WOW Account
Tips to Take Back Your Sold WOW Account
 
Concepts of oop
Concepts of oopConcepts of oop
Concepts of oop
 
Monografija Gimnazije Lebane
Monografija Gimnazije LebaneMonografija Gimnazije Lebane
Monografija Gimnazije Lebane
 
Mavenppt
MavenpptMavenppt
Mavenppt
 

Similar to C++ 2

Function Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal ShahzadFunction Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal ShahzadFaisal Shehzad
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
Functions part1
Functions part1Functions part1
Functions part1yndaravind
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2yndaravind
 
cbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptxcbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptxSRamadossbiher
 
cbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptxcbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptxSRamadossbiher
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxKhurramKhan173
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptxmiki304759
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsRuth Marvin
 
Functions in c++
Functions in c++Functions in c++
Functions in c++Maaz Hasan
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
functions in c language_functions in c language.pptx
functions in c language_functions in c language.pptxfunctions in c language_functions in c language.pptx
functions in c language_functions in c language.pptxMehakBhatia38
 
C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1ReKruiTIn.com
 

Similar to C++ 2 (20)

Function Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal ShahzadFunction Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Functions part1
Functions part1Functions part1
Functions part1
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2
 
cbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptxcbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptx
 
cbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptxcbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptx
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_arguments
 
Function
FunctionFunction
Function
 
C++
C++C++
C++
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Inline function
Inline functionInline function
Inline function
 
inline function
inline functioninline function
inline function
 
functions in c language_functions in c language.pptx
functions in c language_functions in c language.pptxfunctions in c language_functions in c language.pptx
functions in c language_functions in c language.pptx
 
C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1
 
Presentation 2.pptx
Presentation 2.pptxPresentation 2.pptx
Presentation 2.pptx
 
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++
 
Ocs752 unit 4
Ocs752   unit 4Ocs752   unit 4
Ocs752 unit 4
 

C++ 2

  • 1. FUNCTION IN C++ GUIDED BY PREPARED BY MR.MITESH JAISWAL AKSHAY JANI (116380307521) LCTURER IN KAUSHAL SONI PIET(DS) 2ND SHIFT (116380307509)
  • 2. WE ARE IMMENSLY GREATFUL TO OUR LECTURER AND PROJECT GUIDE, MR.MITESH JAISWAL, FOR HER INVALUABLE GUIDANCE WHICH GAVE US A DEEP INSIGHT ON THE SUBJECT. WITH HER KEEN INTEREST AND CONSTANT MORAL BOOSTING, WE ARE ABLE TO IMPLEMENT THE PROJECT SATISFACTORILY.  WE EXPRESS OUR VERY SINCERE THANKS TO COMPUTER ENGINEERING DEPT. FOR PROVIDING ADEQUATE FACILITIES TO COMPLETE OUR PROJECT.  WE ARE AGAIN CORDIALLY THANKFUL TO OUR C&E DEPARTMENT STAFF, FRIENDS AND OTHER PEOPLE WHO HAVE DIRECTLY OR INDIRECTLY HELPED US IN COMPLETION OF THIS PROJECT.  LAST BUT NOT THE LEAST, WE ARE ALSO THANKFUL TO OUR FAMILY MEMBERS WHO ENGOURAGED AND SUPPORTED US ROUND THE CLOCK FOR THIS PROJECT. Akshay Jani Kaushal Soni
  • 3. CERTIFICATE This is to certify that AKSHAY JANI Student of computer Engineering, bearing Enrollment No: 116380307521 have satisfactorily completed his/her Seminar work as a part of course curriculum in Diploma Engineering semester III having a report title “ FUNCTIONS IN C++ ”. MR.MITESH JAISWAL Lecturer, computer Dept. PIETDS-2nd Shift, Limda. PARUL INSTITUTE OF ENGINEERING & TECHNOLOGY COMPUTER ENGG. DEPARTMENT LIMDA, VAGHODIA, VADODARA
  • 4. This is to certify that KAUSHAL SONI Student of computer Engineering, bearing Enrollment No: 116380307509 have satisfactorily completed his/her Seminar work as a part of course curriculum in Diploma Engineering semester III having a report title “ FUNCTIONS IN C++ ”. MR.MITESH JAISWAL Lecturer, computer Dept. PIETDS-2nd Shift, Limda. PARUL INSTITUTE OF ENGINEERING & TECHNOLOGY COMPUTER ENGG. DEPARTMENT LIMDA, VAGHODIA, VADODARA
  • 5. FUNCTIONS IN C++  Experience has shown that the best way to develop and maintain large programs is to construct it from smaller pieces(Modules)  This technique Called “Divide and Conquer” Bad Development Approach Wise Development Approach main() main() •Easer To { { ----- ----- Design ---- ----- Build } ----- Debug ----- Extend function f1() . Modify { . Understand --- . Reuse --- ---- Better Organization } ----- ----- function f2() Return 0; { } --- --- }
  • 6. FUNCTIONS IN C++(CONT.)  In FORTRAN Modules Known as Subprograms  In Pascal Modules known as Procedures & Functions  In C++ Modules Known as Functions & Classes  Programs use new and “prepackaged” modules  New: programmer-defined functions and classes  Prepackaged: from the standard library
  • 7. ABOUT FUNCTIONS IN C++  Functions invoked by a function–call-statement which consist of it’s name and information it needs (arguments)  Boss To Worker Analogy  A Boss (the calling/caller function) asks a worker (the called function) to perform a task and return result when it is done. Boss Main Worker Worker Worker Function Z Function A Function B Worker Worker Note: usual main( ) Calls other Function B1 Function B2 functions, but other functions can call each other
  • 8. ADVANTAGES OF USING FUNCTIONS 1. To help make the program more understandable 2. To modularize the tasks of the program  building blocks of the program 3. Write a module once  those lines of source code are called multiple times in the program 8
  • 9. ADVANTAGES OF USING FUNCTIONS 4. While working on one function, you can focus on just that part of the program  construct it,  debug it,  perfect it. 5. Different people can work on different functions simultaneously. 6. If a function is needed in more than one place in a program, or in different programs, you can write it once and use it many times 9
  • 10. FUNCTION OVERLOADING  Function overloading  Functions with same name and different parameters  Should perform similar tasks  I.e., function to square ints and function to square floats int square( int x) {return x * x;} float square(float x) { return x * x; }  A call-time c++ complier selects the proper function by examining the number, type and order of the parameters
  • 11. USING DEFAULT ARGUMENTS  When you don’t provide enough arguments in a function call, you usually want the compiler to issue a warning message for this error  Sometimes it is useful to create a function that supplies a default value for any missing parameters 11
  • 12. USING DEFAULT ARGUMENTS  Two rules apply to default parameters:  If you assign a default value to any variable in a function prototype’s parameter list, then all parameters to the right of that variable also must have default values  If you omit any argument when you call a function that has default parameters, then you also must leave out all arguments to the right of that argument 12
  • 13. EXAMPLES OF LEGAL AND ILLEGAL USE OF FUNCTIONS WITH DEFAULT PARAMETERS 13
  • 14. OVERLOADING FUNCTIONS  In most computer programming languages, each variable used in a function must have only one name, but C++ allows you to employ an alias  Similarly, in most computer programming languages, each function used in a program must have a unique name  You don’t have to use three names for functions that perform basically the same task, C++ allows you to reuse, or overload, function names 14
  • 15. OVERLOADING FUNCTIONS  When you overload a function, you must ensure that the compiler can tell which function to call  When the compiler cannot tell which version of a function to use, you have created ambiguity 15
  • 16. THREE OVERLOADED FUNCTIONS THAT PERFORM SIMILAR TASKS 16
  • 17. POSTSCRIPT  "The important thing is not to stop questioning; never lose a holy curiosity." -- Albert Einstein