Page 76 - Modular_V1.1_Flipbook
P. 76

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, return_type is the data type of the value returned by a function. If your function does
                  not return any value then the function should be declared with void data type. The data type is

                  followed by function_name to give a name to the function. This name is used to define and use
                  the function later. The function name is followed by parenthesis. Inside the parenthesis, you can
                  give one or more parameters. A parameter is like a placeholder. At the end, semicolon should be
                  written to complete the declaration. Following is an example to declare a function named display:
                  int display(int);
                  In the preceding 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.




                  74      Touchpad MODULAR (Version 1.1)-X
   71   72   73   74   75   76   77   78   79   80   81