Public Group

# help with functions

This topic is 4846 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

float square_number(float num) { return num*num; } hey, that function, it doesn't ask the user for a num to mulitply, howdoes it return num * num, when there are no numbers to mult

##### Share on other sites
The num variable is an argument to the function, as you can see from the function argument list (float num). The calling code would provide the value of num.

##### Share on other sites
You would call that function as such:

float square_48 = square_number(48.0f);

To get user input you would have to take their input and call the function with that, such as...

float squared_number;
float number = squared_number = 0.0f;
cin >> number;
squared_number = square_number(number);

Sorry if that doesn't make sense, I'm in a bit of a rush.

##### Share on other sites
it is called from another function, possibly main(), like this:
float sixteen=square_number(4);

When the function is called, num becomes 4, as that is what was specified in the call.

##### Share on other sites
The number needs not be actually entered by the user, it could be a by-product of the rest of the program. At any rate, notice how the function is declared to take one float argument named num. This is how it receives the number it will multiply: you have to pass it to the function.

#include <iostream>float square_number(float num){   return num*num;}int main(){   float five = 5;   float twenty_five = square_number(five);   float hundred = square_number(10);   std::cout << five << ' ' << twenty_five << ' ' << hundred << std::endl;}

hehe too slow

##### Share on other sites
Quote:
 Original post by FrunyThe number needs not be actually entered by the user, it could be a by-product of the rest of the program. At any rate, notice how the function is declared to take one float argument named num. This is how it receives the number it will multiply: you have to pass it to the function.#include float square_number(float num){ return num*num;}int main(){ float five = 5; float twenty_five = square_number(five); float hundred = square_number(10); std::cout << five << ' ' << twenty_five << ' ' << hundred << std::endl;}

oh, ok, thanks, that one helped me the best

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633300
• Total Posts
3011266
• ### Who's Online (See full list)

There are no registered users currently online

×