C++ 2

251 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
251
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

C++ 2

  1. 1. FUNCTION IN C++ GUIDED BYPREPARED BY MR.MITESH JAISWALAKSHAY JANI(116380307521) LCTURER INKAUSHAL SONI PIET(DS) 2ND SHIFT(116380307509)
  2. 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. 3. CERTIFICATEThis is to certify that AKSHAY JANI Student of computerEngineering, bearing Enrollment No: 116380307521 have satisfactorilycompleted his/her Seminar work as a part of course curriculum inDiploma Engineering semester III having a report title “ FUNCTIONS INC++ ”. MR.MITESH JAISWAL Lecturer, computer Dept. PIETDS-2nd Shift, Limda. PARUL INSTITUTE OF ENGINEERING & TECHNOLOGY COMPUTER ENGG. DEPARTMENT LIMDA, VAGHODIA, VADODARA
  4. 4. This is to certify that KAUSHAL SONI Student of computerEngineering, bearing Enrollment No: 116380307509 have satisfactorilycompleted his/her Seminar work as a part of course curriculum inDiploma Engineering semester III having a report title “ FUNCTIONS INC++ ”. MR.MITESH JAISWAL Lecturer, computer Dept. PIETDS-2nd Shift, Limda. PARUL INSTITUTE OF ENGINEERING & TECHNOLOGY COMPUTER ENGG. DEPARTMENT LIMDA, VAGHODIA, VADODARA
  5. 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. 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. 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 WorkerWorker Worker Function ZFunction A Function B Worker Worker Note: usual main( ) Calls other Function B1 Function B2 functions, but other functions can call each other
  8. 8. ADVANTAGES OF USING FUNCTIONS1. To help make the program more understandable2. To modularize the tasks of the program  building blocks of the program3. Write a module once  those lines of source code are called multiple times in the program 8
  9. 9. ADVANTAGES OF USING FUNCTIONS4. 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. 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. 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. 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. 13. EXAMPLES OF LEGAL AND ILLEGALUSE OF FUNCTIONS WITH DEFAULTPARAMETERS 13
  14. 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. 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. 16. THREE OVERLOADED FUNCTIONSTHAT PERFORM SIMILAR TASKS 16
  17. 17. POSTSCRIPT "The important thing is not to stop questioning; never lose a holy curiosity." -- Albert Einstein

×