Jump to content
  • Advertisement
Sign in to follow this  
Avont29

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.

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


Link to 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


Link to 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


Link to 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


Link to 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;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
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;
}



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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!