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

Declaration

                  Before using a function, you need to declare it. Declaring a function is called function prototyping.
                  A function prototype tells the compiler about the data type and parameters of the function.  The
                  syntax to declare a function in C++ is:

                       return_type function_name([parameter 1, parameter 2, …]);
                  Where, the return_type specifies the data type of the value returned by the function. If no value
                  is returned, the function should be declared with void. The function_name is the assigned name

                  used to define and call the function later. Parameters are placed inside parentheses () and act as
                  placeholders for input values. The declaration must end with a semicolon (;).
                       int display(int);

                  In the above example, the display function is declared with int data type and and an int parameter.
                  Definition

                  After declaring a function, you need to define it. Defining a function means to give the body of
                  the function. The definition of a function contains the functionality of the function in within curly
                  braces{ }. Let us consider the preceding example of display function:

                       int display(int age)
                       {

                           cout<<"Your age is: " << age <<" years.";
                           return 0;
                       }
                  In the preceding code, you can see that the return statement is used before the ending curly
                  brace of the function to return an integer value. Because, the return type of the function is int.
                  The return is jump statement in C++ that transfers the control to the calling function. If you do

                  not want to return any value from the function, then you need to declare a function with the void
                  data type. In this case, the function returns 0 (zero).

                                 Clickipedia




                        The default return type of every function in C++ is int.



                  Calling a Function

                  Calling a function means to use the function for performing the desired task. To call a function,
                  you just need to write the name of the function with arguments, if any, in the parenthesis. The
                  arguments are the actual parameter’s values. Generally, a function call occurred inside the main()
                  function. However, you can call a function anywhere in the program.






                   86
                          Touchpad MODULAR (Ver. 2.0)
   83   84   85   86   87   88   89   90   91   92   93