Page 81 - Modular_V1.1_Flipbook
P. 81

For example:

                 #include<iostream.h>
                 #include<conio.h>
                 void fun1();
                 void fun2();
                 char grade;
                 void main(){

                      fun1();
                      fun2();
                      getch();
                 }

                 void fun1(){
                      grade = ‘B’;
                      cout<<”Your grade is: “<< grade <<”\n”;
                                                                                          Output:
                 }
                                                                                          Your grade is: B
                 void fun2(){
                                                                                          Your grade is: A
                      grade = ‘A’;
                      cout<<”Your grade is: “<< grade;
                 }
                 When you run the preceding program, you will get two different outputs from both the functions,
                 which means both the functions are able to access the global variable grade.

                 You can also use the scope resolution operator (::) two consecutive colons as follows:
                 #include <iostream.h>
                 #include <conio.h>
                 int num = 50;

                 void main(){
                 int num = 100;
                 cout << “Local variable num = “ << num;
                                                                                          Output:
                 cout << “Global variable num = “ << ::num;
                                                                                          Local variable num = 100
                 getch();
                                                                                          Global variable num = 50
                 }

                     MORE SOLVED PROGRAMS

                 Program 5: To print the square of the number if the number is greater than or equal to 10.
                 Otherwise, print the number by multiplying it with 2.
                 #include <iostream.h>
                 #include <conio.h>
                 int add(int);

                 void main(){


                                                                                               Functions in C++   79
   76   77   78   79   80   81   82   83   84   85   86