please help me i have to send this to my C++ prof today

Write your question here.
Task#1: Practice the concept of Void Functions by showing the output of the
following C++ Programs including the function Modify to discover how the Value
and Reference parameters work, then write a short sentence to explain the difference
between them in the achieved results:
Note: put the screen shot of the output under each program below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 
a) //Function Modify with (Value parameters)
#include<iostream>
using namespace std;
void modify(int m); // function prototype
int main()
{
int x;
x=7;
cout<<"x befor calling modify function = "<<x<<endl;
modify( x ) ; //calling statement x is actual parameter
cout<<"x After calling modify function = "<<x<<endl;
cout << endl;
return 0;
}
// Function definition
void modify( int y ) // Heading y is Formal Parameter
{
y=y+10;
cout<<"y inside modify function = "<<y<<endl;
}


 


 b) // Function Modify with (Reference parameters)
#include<iostream>
using namespace std;
void modify( int &m ); // function prototype
int main()
{
int x;
x=7;
cout<<"x befor calling modify function = "<<x<<endl;
modify( x ) ; //calling statement x is actual parameter
cout<<"x After calling modify function = "<<x<<endl;
cout << endl;
return 0;
}
// Function definition
void modify( int &y ) // Heading y is Formal Parameter
{
y=y+10;
cout<<"y inside modify function = "<<y<<endl;
}
Hello mick777,

And your question is??

First quit writing your code to benefit the compiler. You have failed in this attempt. To much white space.

Write your code so that you can read and follow it.

Give you variables a good name. Not a single letter that has no meaning.

Prefer to use the new line, (\n), over the function "endl".

It may be legal to change a variable name in a function parameter, but it does not mean that you have to. Most times using the same variable name that is sent to the function is best for readability and folling the variable.

As an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// a) //Function Modify with (Value parameters)

#include<iostream>

using namespace std;

void modify(int m); // function prototype

int main()
{
    //int number;
    //number = 7;
    int number{ 7 };  // <--- You can define the variable and initialize at the same time.

    cout << "\n Number before calling modify function = " << number << '\n';

    modify(number); //calling statement number is actual parameter

    cout << "\n Number After calling modify function = " << number << '\n';
    cout << '\n';

    return 0;  // <--- Not required, but makes a good break point for testing.
}

// Function definition
void modify(int number) // Heading number is Formal Parameter
{
    number = number + 10;

    cout << "\n Number inside modify function = " << number << '\n';
}


Both pieces of code work, what do you not understand?

Andy

Edit: Sorry hit the wrong button.
Last edited on
thank you for the example

but i have a question

what is the difference between using
void modify( int m )
or
void modify( int &m )

because in the example
the output


Number before calling modify function =7
Number After calling modify function = 17
Number inside modify function = 7

and
Number before calling modify function =7
Number After calling modify function = 17
Number inside modify function = 17
Hello mick777,

I think you need to reread your book.

The first is pass by value and the second is pass by reference.

By value is only a copy of the variable. You can use it change it, but when the function ends so does the variable because it is local to the function. So any changes made are lost when the function ends.

Pass by reference is not a copy, but used the variable that was sent to the function, so any changes are reflected back where the variable was defined and passed to the function.

By reference still works when a function passes a passed variable to another function.

Andy

P.S. if you understand the difference of pass by value and pass by reference the answer is right there.
Last edited on
thank you ANDY
i know that the first is pass by value and the second is pass by reference.
but i didn't know how it work for the compiler

thank you that's very helpful
Topic archived. No new replies allowed.