Example of Program using passing by value and use of arguments in a function

note that the numbers are not part of the program, they are used for you to tract the flow 
 
11 // function example, passing by values and void functions
22 #include <iostream>
33 #include<string>
44 using namespace std;
55 // function prototypes inserted here because
 //they are used before they are defined
66
 void PrintFunction(void);
77
  double DFunction();
88
  int IntFunction(int,int);
99
   void myName(string);
1010  // main program...
1111
        int main()
1212 {
1313
        cout<<"Function PrintFunction () displays.."<<endl;
1414 // call, to the void PrintFunction ()
1515
        PrintFunction ();
1616
        cout<<" \nWorking with function DFunction..."<<endl;
1717 // calling function DFunction of type double but without any argument...
1818
        double  d = DFunction();
1919 // display the value returned by DFunction.
2020
        cout<<"The value returned by DFunction=  "<<d<<endl;
2121
        int y = 100;
2222
        int  x=2;
2323
2424 //Calling function IntFunction of type int and with an argument...
2525
        int z = IntFunction(x, y);
2626
        cout<<"Function IntFunction calculates the recieved and
 returns the result main() "<<endl;
2727 // display the returned value...
2828
        cout<<"The value returned  by IntFunction is = "<<z<<endl;
2929
        string Name="Selina";
3030
        myName(Name);
3131 // return 0 if everything was successful
3232
        return 0;//end main
3333 }
3434
        void PrintFunction ()
3535 {
3636 // Simply display the following text when called. //return no value to the caller
3737
        cout<<"\nWhen PrintFunction is called "<<endl;
3838 cout<<"It returns nothing because it of a void function.
 However this message is displayed once it is called"<<endl;
3939 }
4040 // When called function DFunction() receives nothing .
 // It will however return a value it will calculate basing on its local variables
4141
        double DFunction()
4242 {
4343
        double p = 12.122;
4444
        double q=2.0;
4545 cout<<"\nAlthough function DFunction does not receive data when called "
4646 <<"\nIt returns a value"
4747 <<"\nto the calling function..."<<endl;
4848 // return the result to the caller...
4949 return p/p;
5050 }
5151
        int IntFunction(int a,int b)
5252 {
5353 // receive data, works on it and returns the result to the caller
5454
        a=2;
5555
        b = a * 100;
5656
        int c=b/4;
5757
        cout<<"On completing the calculation function it.. "
5858
         <<"\nreturns result to the calling function 
which in is this case is main()\n"<<endl;
5959 // now return the data stored in c to main ()
6060
        return c;
6161 }
6262 // Function myName receives the names and displays on the screen instead of returning a value
6363
        void myName(string fname)
6464 {
6565 // receive something but return nothing...
6666
         string firstName = fname;
6767
        cout<<"\nFunction myName receives the name from the caller."<<endl;
6868
        cout<<"but does not return any value to the caller, rather..."<<endl;
6969
        cout<<"Then it processes and displays name as "<<firstName<<endl;
7070 }


Output: Function PrintFunction () displays.. When PrintFunction is called It returns nothing because it of a void function. However this message is displayed once it is called Working with function DFunction... Although function DFunction does not receive data when called It returns a value to the calling function... The value returned by DFunction= 1 On completing the calculation function it.. returns result to the calling function which in is this case is main() Function IntFunction calculates the recieved and returns the result main() The value returned by IntFunction is = 50 Function myName receives the name from the caller. but does not return any value to the caller, rather... Then it processes and displays name as Selina
Back to introduction to functions

No comments:

Post a Comment