Windows 7 Forums
Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: C++ Question


23 Mar 2012   #1

Window 7 Ultimate x64
 
 
C++ Question

I am working on my homework and need help figuring out a tough question.

2) When you call a function, as in the area and perimeter of a past assignment, you include the values to be sent to the function (the arguments) in parentheses
and the function must have been defined with parameters of an equivalent type, that will be used to receive those values

If the function is called using variables, as in cout << area(length, width) for instance, what is passed to the parameters is a copy of the values in those variables.

This is called call-by-value. In that case, the receiving parameters in the function are initialized to the contents of the two argument variables (length and width, in our example).

Based on this, given the following function:

void f (int i )

{

i = 57;

}

assuming it is called like this:

f(value)

where value is a variable of type int, defined as int value = 9; what would be the result of printing variable value after the call? Explain



My System SpecsSystem Spec
.

23 Mar 2012   #2

XP / Win7 x64 Pro
 
 

I don't believe this is the right section for this type of question, nor am I certain you should be getting help on forums for your homework, but I feel comfortable enough asking the following questions in helping you figure this out:

- What is actually being passed to the f() function?
- Is it the variable itself that is being passed or just the value it contains?
- If it's just the value it contains, how does the f() function impact the actual value of the variable value?

I would hope there is some sort of help system set up at your school or in your class for when you need help like this. I would suggest you look into that instead.
My System SpecsSystem Spec
23 Mar 2012   #3

Window 7 Ultimate x64
 
 

I dont think there is a section on here for programming or other random computer related topics. I am taking an online class with a professor who doesn't reply very fast to my emails, and there is a small discussion board for the class, but its pretty much tumble weeds, no help there.

#include <iostream>
using namespace std;
void f (int);
int main()
{
int value = 9;
cout << "Value in Main: " << value << endl;
f(value);
cout << "Value in Main: " << value << endl;
return 0;
}
void f (int i)
{
i = 57;
cout << "Value in Function: " << i << endl;
}
Returns
Value in Main: 9
Value in Function: 57
Value in Main: 9

Answer 2) There are basically two ways to pass a value to a function pass by value and pass by reference.
When you pass by value, the compiler creates a temporary variable to hold the passed value within the scope of the function.
Any changes made to the value by the function are temporary to the scope of the function;
the changes are not seen by the scope of the caller when the function returns.
When you pass by reference, the compiler passes the memory location of the variable rather than a temporary copy.
This means that any changes made to the variable by the function are also seen in the scope of the caller when the function returns.
In this case, changes made to the parameter have no effect on the argument.
I think if you printed the value of value after the call (and not in the function itself) it would still be 9,
because the value only changes in the scope of the function. The scope of variables declared within a function
or any other inner block is only their own function or their own block and cannot be used outside of them.
My System SpecsSystem Spec
.


23 Mar 2012   #4

XP / Win7 x64 Pro
 
 

Right. You're only passing the value of the variable value, not the reference to it. Doing anything to that value within the function doesn't change the value variable at all.
My System SpecsSystem Spec
Reply

 C++ Question




Thread Tools



Similar help and support threads for2: C++ Question
Thread Forum
Solved Thermal Paste Question..(Noobish Question) PC Custom Builds and Overclocking
Answer Question, Ask Question. Chillout Room
Solved Logitech 5.1 surround question and soundcard question IDT and Realtek Sound & Audio
Question BSOD Help and Support
ram question General Discussion
Question about being a IT Chillout Room

Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 06:40 PM.
Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App
  

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