Page 286 - Computer_Science_F5
P. 286

Output:






          FOR ONLINE READING ONLY

                                        Figure 4.56: Output of the program                         Chapter Four: Object oriented programming with C++


              Function that does not return value (Void function)
              A void function is a function that does not return any value. To call such a function,
              you will define function name without data type or variable. Program Example 4.28
              demonstrates a C++ program that changes marks to grade using void function. The
              output is shown in Figure 4.57.


                    Program Example 4.28:
                Changing marks grade using void function


                                     #include<iostream>
                                     using namespace std;
                                     void grade(double marks)
                                     {
                                            if (marks>80)
                                            cout<<’A’;
                                            else if (marks>60)
                                            cout<<’B’;
                                            else if (marks>40)
                                            cout<<’C’;
                                            else if (marks>20)
                                            cout<<’D’;
                                            else
                                            cout<<’F’;
                                     }
                                     int main()
                                     {
                                            cout<<”Enter marks: “;
                                            double marks;
                                            cin>>marks;
                                            cout<<”The grade is “;
                                            grade(marks);   // function call
                                            return 0;
                                     }

                Output:





                                        Figure 4.57: Output of the program


                                                    277
               Student’s Book  Form Five



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