Thread: Newbie needs help

1. Newbie needs help

Is there a way to keep an output in fraction form in stead of decimal form? and What does pass by reference mean? Also when you are using functions, when do you put void in front of the function and when do you not do that?

Also is there a way to let someone cin>> a formula?

2. 1) Yes. If I understand correctly you want something like
Code:
```struct fraction
{
int big;    //dunno the actual name
int small; //dunno the actual name
}```
So instead of having 15.23 you will store 15 to fraction.big and 23 to fraction small. That is all. Now, I don't know if there is a standard method for getting the "small" from a number. You get the "gib" by fraction.big = 15.23, of course. To get the small you will have probably to specify how many digits you want.

2) Pass by reference means that you pass the actual object/variable to the function, not a copy. So whatever changes you make to the object/variable are reflected to it. Google it for more information. You use the & symbol to do so

3) If you put void in front of a function it means that it returns nothing. If you want a function lets say to print "hello" on the screen you would do this:
Code:
```void fun()
{
std::cout << "Hello";
}```
If you want to add two number and return the result:
Code:
```int add(int a, int b)
{
return a + b;
}```
You can also use return; with a void function but it just terminates the function without returning anything.

These are kind of basic stuff. I recommend studying a book to learn C++

3. Is there a way to keep an output in fraction form in stead of decimal form?
Not in a trivial way when the result is a floating point (float, double or long double).

and What does pass by reference mean?
That you are passing a value as a reference - you are basically passing the ACTUAL location of the variable, rather than passing a by value, which means that a copy of the original variable is made. A reference can be modfiied, but a value copy is just that - a copy of the original variable, so you can modify it to your hearts content, but it will not change the original variable - just like scribbling on a copy of a page of a book will not change the original page in the book.

The fact that we are not making a copy comes in handy when the object (variable) is large - in this case, copying the location of the variable is a small effort, whilst copying the content (say of a vector with 10000 elements) can be a substantial effort for the computer.

Also when you are using functions, when do you put void in front of the function and when do you not do that?
void is used to indicate that a function doesn't return a value (void means "of no value"). All functions have a return type - if the function doesn't have a natural return value (it's not calculating something and there is no other form of "result" to return - e.g. no "success or fail" to return).

--
Mats

4. What does pass by reference mean?
Maybe it will help if I give you an example of why you would use references...

Usually references are introduced with the Swap(x, y) function. The purpose of that function is to swap the x & y values. So, if x=4 and y=5 before you run the function, you will have x=5 and y=4 after you run the function (example*).

If you simply pass x and y into a function "normally", the actual variables (x & y) don't really get passed-in. Only the values (4 & 5) get passed in. You can re-use the variable names x & y inside the function (with the values 4 & 5 assigned), but these will be different x & y variables with the same names (but different scope). So, you can go-ahead and swap the x & y values inside the function, and it will work fine inside the function (this requres a temporary variable). But, when you exit the function, your original x & y values will not be changed!

Your function could return one swapped value, but a function can only return one value. So, you could return the new x-value, or the new y-value, but not both!

References (and pointers) allow your function to "get to" the original x & y variables, and get-around these limitations.

Since functions very-frequently need to modify more than one variable, references (and pointers) are very common.

* The example shows 4 different version of swap(), using references, pointers, templates, and the STL. (Don't worry about the other methods/concepts 'till you study them.)

Popular pages Recent additions