dividedByTwo

Hi. can someone tell me how i can write a statement that calls the dividedByTwo function? advance thanks.! :)
Have you created such a function?
Yes, i used a parameter named wholenumber. but this time, it should pass the function that contents of a total variable. :)
I don't know what you are saying.You should be able to call the function like any other function. dividedByTwo(5);
@Peter87 oh, sorry i can't make it any clearer but the function should pass the total variable. that's the only thing i remembered from what my instructor told me. :)
Last edited on
Let's see your code.
dividedByTwo(total)
*facepalm*
@Volatile Pulse oh, so sorry for the late reply. here's the other code that i made naming my parameter a wholenumber .

#include <iostream>
using namespace std;


void dividedByTwo(double wholeNumber);

int main()
{
double wholeNumber;


cout<<"Enter a whole number: ";
cin>>wholeNumber;


dividedByTwo(wholeNumber);

}

void dividedByTwo(double wholeNumber)
{
if(wholeNumber > 0)
{
cout<<"The result is : " << wholeNumber / 2;
}


}


what im trying to figure out this time is that how can the dividedByTwo function can pass a " total variable " . thanks again. :)
I believe you're referring to a return value. This is pretty simple. In your function prototype (check your other post) you need to change void to the data type of your choice (most likely double). You also need to have a way to assign the value of the "wholeNumber" divided by 2. Or, since it's a simple function, you can just return wholeNumber / 2.

An example of a return value is also located in your prototype thread. But to elaborate on a return statement it follows this syntax:
return value;

So in this case, your code would be simply:
return wholeNumber / 2;

Note: You declared main as an int and you must also return a value since main is a function as well. Typically, main returns 0 to notify the OS that everything went ok. There is exceptions, but most, if not all, of your programs should return 0.

Note 2: Please use the code tags when posting code. You have two options, you can highlight your code when pasting it and press the < > button to the right, or you can add the tags manually by typing [code] before your code and [/code] after your code.
Last edited on
ok, thanks, and ill sure do the code tag things. :)
@Volatile Pulse im still confused, what you were trying to say before is that the function should looks like dividedByTwo(total) right? but what about the variable? cause my instructor said that the dividedByTwo function should pass the total variable. :)
Well it's better if you don't use the same names in your functions as the name of your local functions, specifically wholeNumber. It's a habit that beginners get stuck in so let's break that here and now.

When programming a function, you should pick a variable name for that function. To give you an example using your code, you can do something like this:
1
2
3
4
5
// DividedByTwo Function
void DividedByTwo(double dividend) {
   double quotient = dividend / 2;
   cout << "The result is: " << quotient;
}


And how to use it:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main() {
   int total;
   cout << "Please enter a number: ";
   cin >> total;

   // Call DividedByTwo and pass the value of total
   DividedByTwo(total);

   return 0;
}


It might not make sense just reading it, but the value of total is passed to DividedByTwo. When the computer looks at DividedByTwo's definition (the first code snippet) it assigns the value from total to the variable dividend. Dividend in a sense is another name for total. There are some things that don't work in the function like you'd expect (mainly any assignments), but it is like doing this dividend = total; so you can use dividend within that function just like you would have used total.

You have the option to return a value, like I explained earlier, but I don't believe you need that. I'm sorry if I misunderstood, but I think that's what you were asking for.
Topic archived. No new replies allowed.