Page 285 - Computer_Science_F5
P. 285

Computer Science  Note:  Parameters are values which need  a function. Formal parameters are variables
                  to be passed into the function. They  in the function heading.
                  create a link or communication
                                                     (ii) Function call
                  between the defined function and
                                                     If you want to use a defined function within
                  the main function. The following
          FOR ONLINE READING ONLY
                  is the structure of the program to
                                                     function. During calling the function, you
                  represent function parameters.     the main function, you have to call that
                                                     have to specify the name of the function,
                   funct1(x,y)                       return value and parameters if they are
                   {                                 included  within  the  function.  There
                   // function body                  are different ways of calling a function
                   ……………                             depending on whether the function returns
                   }                                 a value or not.
                   main()
                   {                                 Returning value function
                   funct1(a,b); //function calling   For the function which returns a value,
                   }                                 the function will be treated as a value.
                                                     To call it, you have to define its data type
           Where a, b are the actual parameters and x,  and variable. An example of the calling
           y are formal parameters. Actual parameters  function with the integer data type is int
           are variables or expressions listed in call to  total = sum(10,20);


           The following Program Example 4.27 demonstrates a C++ program that uses function
           call to find maximum number of the two numbers. The output is shown in Figure 4.56.

                   Program Example 4.27:
               Using function to find a maximum number

                     #include<iostream>
                     using namespace std;
                     int maximum(int x, int y)
                     {
                            int answer;
                            if (x>y)
                            answer=x;
                            else
                            answer = y;
                            return answer;
                     }
                     int main()
                     {
                            int a, b;
                            cout<<”Enter two number to find which is maximum”<<endl;
                            cin>>a>>b;
                            int t = maximum(a,b);         //function call
                            cout<<”The maximum between “<<a<<” and “<<b<<” is “<<t;
                            return 0;
                     }


                                                 276
                                                                for Advanced Secondary Schools



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