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 
 
1 // function example, passing by values and void functions
2 #include <iostream>
3 #include<string>
4 using namespace std;
5 // function prototypes inserted here because
 //they are used before they are defined
6
 void PrintFunction(void);
7
  double DFunction();
8
  int IntFunction(int,int);
9
   void myName(string);
10  // main program...
11
        int main()
12 {
13
        cout<<"Function PrintFunction () displays.."<<endl;
14 // call, to the void PrintFunction ()
15
        PrintFunction ();
16
        cout<<" \nWorking with function DFunction..."<<endl;
17 // calling function DFunction of type double but without any argument...
18
        double  d = DFunction();
19 // display the value returned by DFunction.
20
        cout<<"The value returned by DFunction=  "<<d<<endl;
21
        int y = 100;
22
        int  x=2;
23
24 //Calling function IntFunction of type int and with an argument...
25
        int z = IntFunction(x, y);
26
        cout<<"Function IntFunction calculates the recieved and
 returns the result main() "<<endl;
27 // display the returned value...
28
        cout<<"The value returned  by IntFunction is = "<<z<<endl;
29
        string Name="Selina";
30
        myName(Name);
31 // return 0 if everything was successful
32
        return 0;//end main
33 }
34
        void PrintFunction ()
35 {
36 // Simply display the following text when called. //return no value to the caller
37
        cout<<"\nWhen PrintFunction is called "<<endl;
38 cout<<"It returns nothing because it of a void function.
 However this message is displayed once it is called"<<endl;
39 }
40 // When called function DFunction() receives nothing .
 // It will however return a value it will calculate basing on its local variables
41
        double DFunction()
42 {
43
        double p = 12.122;
44
        double q=2.0;
45 cout<<"\nAlthough function DFunction does not receive data when called "
46 <<"\nIt returns a value"
47 <<"\nto the calling function..."<<endl;
48 // return the result to the caller...
49 return p/p;
50 }
51
        int IntFunction(int a,int b)
52 {
53 // receive data, works on it and returns the result to the caller
54
        a=2;
55
        b = a * 100;
56
        int c=b/4;
57
        cout<<"On completing the calculation function it.. "
58
         <<"\nreturns result to the calling function 
which in is this case is main()\n"<<endl;
59 // now return the data stored in c to main ()
60
        return c;
61 }
62 // Function myName receives the names and displays on the screen instead of returning a value
63
        void myName(string fname)
64 {
65 // receive something but return nothing...
66
         string firstName = fname;
67
        cout<<"\nFunction myName receives the name from the caller."<<endl;
68
        cout<<"but does not return any value to the caller, rather..."<<endl;
69
        cout<<"Then it processes and displays name as "<<firstName<<endl;
70 }


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