Page 284 - Computer_Science_F5
P. 284

to compute the interest of another person  (a) User-defined function
              who also deposited money in the bank.    A  user-defined  function  is  the  type  of
              Alternatively, we would rather move to   function designed by a programmer
              a ‘code section’ that calculates interest   according  to  the  user’s requirement.
              and then return to where you left off.   Programmers use the function to break       Chapter Four: Object oriented programming with C++
          FOR ONLINE READING ONLY
                                                       down large programs to simplify
              Advantages of functions                  debugging, testing, and maintenance of
               (a) Using functions facilitates the reuse   the  program.  A  user-defined  function
                   of the codes in writing computer    that  returns value  is called ‘returning
                   programs                            function’.  The  function  that  does not

               (b) The use of functions help to improve  return value is called ‘void function’.
                   the program’s readability because it
                   reduces the complexity of the main  Elements of user-defined function
                   function                            User-defined  function  contains  three

               (c) Functions help you to focus on just  elements:  function  definition,  function
                   that part of the program and construct  call and function prototype.
                   it, debug it, and perfect it.
               (d) Different people can work on  (i) Function definition
                   different functions concurrently.   Function    definition   specifies   all

              The following is the syntax of typing a  characteristics  and  elements  required
              function:                                in  a  user-defined  function.  Function
                 returnValueType functionName (list    definition includes function header and
                 of parameters)                        function body. Function header includes
                 {                                     data type or returns type, function name,
                 //Function body;                      and a list of parameters. On the contrary,
                 }
              Types of functions                       function  body involves  all  executable
                                                       statements within it. Figure 4.55 shows
              There are two types of functions applied  the components of function definition.
              in  C++  programming,  namely  user-
              defined function and built-in/pre-defined   int sum (int x, int y)
              function as shown in Figure 4.54.                                 Function header

                             Functions
                                                         sum = x + y;
                                                         cout<< “The total is”<<sum>>endl;


                 Built-in                User-defined
                Functions                 Functions                             Function body


                     Figure 4.54: Types of functions    Figure 4.55: Components of function definition

                                                    275
               Student’s Book  Form Five



     Computer Science Form 5.indd   275                                                     23/07/2024   12:34
   279   280   281   282   283   284   285   286   287   288   289