Page 87 - Modular_V2.0_C++_Flikpbook
P. 87

8

                                                                             FUNCTIONS IN C++










                                     Your Aim


                                     to learn about:
                                            Function
                                             Types of Parameters and Arguments
                                              Different Methods to Call a Function
                                              Scope of Variables
                                               More Solved Programs



                 Sometimes, you need to reuse the code written previously. In that case, either you copy and
                 paste the code or rewrite the same code again. But, that is possible only a few number of times.
                 If you need the same code a hundred times, then it will be a tedious task to copy paste the same
                 code again and again. It also increases the length of the code and decreases the efficiency of the
                 code. To avoid such situations, C++ provides the concept of functions.

                    FUNCTION


                 A function is a  block of organised  and  reusable code  used to  perform  a  particular  task.
                 It allows you to reuse a code many times and reduces duplicity in a program. It saves time
                 as we need not to write the same code again and again. It simplifies the code by breaking
                 it into smaller units. C++ provides two types of functions which are: built-in functions and
                 user-defined functions. Let us discuss them in detail. In this chapter, we will learn about user-
                 defined functions.

                 Built-in Functions

                 Built-in functions are predefined functions provided by C++. The functions are written in the
                 header files or library files. Hence, these are also called library functions. You can directly use
                 these functions in your code like getch( ). The getch() function is defined in the conio.h header
                 file. We will learn more about built-in functions in the next chapter.
                 User-Defined Functions


                 C++ allows you to create your own functions. These are known as user-defined functions. You
                 can use any logic in the function according to your requirement. Creation of a user-defined
                 function consists of three parts: declaration, definition and call.
                                                                                                                  85
                                                                                                Functions in C++
   82   83   84   85   86   87   88   89   90   91   92