Page 46 - Touchcode_C7_Flipbook
P. 46

So, the main idea of using  a function  is to keep the code DRY (Don’t Repeat Yourself).
                  Function mainly removes the repeated commands and minimize errors, keeps the code
                  short, and saves programming time.


                  ADVANTAGES OF USING FUNCTIONS
                      Use of functions enhances the readability of a program, thus making code organized
                      and easy to understand.

                      Function reduces the complexity of a program and gives it a modular structure.
                      Functions can be individually tested.

                      Reduces code length.
                      Code reusability increases.


                  WHAT ARE DIFFERENT FUNCTION PARAMETERS?
                  Parameters are variables local to function, and are part of the definition. Arguments are the

                  values that are passed on to the function. A function can have more than one parameter,
                  and in that case order in which arguments are passed matters.

                  So, Function parameters are the names listed in the function  definition  and Function
                  arguments are the real values passed to (and received by) the function.



                     Example:
                     1.  To calculate the area of a circle; you need the radius of the circle whose area you
                         want to calculate.

                     2.  To calculate the area of a rectangle; you need to know the length and breadth of the
                         rectangle.
                     3.  To calculate the area of a square; you need to know the side of the square.



                  The formula for calculating the area of a particular shape whether it is circle, rectangle or
                  square will always remain the same.


                  So, in the above-mentioned scenarios, to calculate the area of circle, rectangle and square,
                  the formula which is used to calculate the area is the body of the function.

                  You only need to provide the value of:
                      radius to calculate the area of a circle
                      length and breadth to calculate the area of the rectangle

                      side to calculate the area of the square

                  So, the variables used by a function to perform the set of tasks defined in the body of the
                  function is known as function parameters.


                   44     Touchcode-VII
   41   42   43   44   45   46   47   48   49   50   51