Jump to content
• ### What is your GameDev Story?

• Advertisement

Public Group

# help with functions

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

If you intended to correct an error in the post then please contact us.

## 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 this post

##### Share on other sites
Advertisement
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 this post

##### 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 this post

##### 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 this post

##### 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 this post

##### 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

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement
• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• ### Popular Now

• 15
• 9
• 11
• 9
• 9
• Advertisement
• ### Forum Statistics

• Total Topics
634135
• Total Posts
3015754
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!