Hi Eddie,
I have a serious problem, PLEASE HELP! I have a test on the 29th, and I cant for the life of me ind the answers to this question. If you could help me so I can study I would REALLY appreciate it.

What determines when you should define a function to return a value?

When you define a function, what determines whether you should define the
function with parameters or not?

Thanks so Much,
Happy Thanksgiving,

Hello Eric, thanks for the question.

Defining your function is dependant on a number of different things, but, most importantly, what you are trying to do with the function. Let's take a look at a couple of different ways to write an example function that simply needs to multiply a number by 10. Here is the first.

int Calc(int num)
return num * 10;

Now when we call it, we can do the following:

int num = 2;

num = Calc(num);

To get the proper answer. How else would num retain the proper value we need? Here's another way. By passing the parameter by pointer:

void Calc(int *pNum)
(*pNum) *= 10;

Now we have retrieved the same result without returning a value. Here is another way to do the same thing, by passing by reference:

void Calc(int &num)
num *= 10;

So to answer your question, it all depends on the purpose of the function. Where any of these example functions would suffice because it is a simple operation, more complicated functions would be more particular. If you wanted to draw a rectangle on your screen, your function would more than likely have to take in an x coord, y coord, width, and height so we could properly draw a rectangle of any size. The parameters should be used to give the function what it needs to complete the task. If you wanted to write a function to return a random number between 0 and another number, you would need to return the number so we can store it, and take in a number so we can determine the range of values:

int Random(int highNum)
return (rand() % highNum);

I hope that these examples have helped you out some.

- Eddie


All Answers

Answers by Expert:

Ask Experts




I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.


I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.