I know this is a kind of broad question, but I have been having a hard time in general knowing when to put what call parameters into what functions. Does anybody have any good tips on when to know when to use a void function verses a bool or int or other type of function. Also how to know what to call into the Function?

I know this is a kind of broad question, but I have been having a hard time in general knowing when to put what call parameters into what functions. Does anybody have any good tips on when to know when to use a void function verses a bool or int or other type of function. Also how to know what to call into the Function?

Well, you bring up to different topics really when mention both parameters and the "void function verses....", which would indicate return type.

Return type really depends on what you want. If it makes sense that your function should return an integer, then you would make it an int myFunc() declaration.

Parameters also depend on type, but if you pass by address/reference, then they can also return a changed value via the parameter...hmmmm...

So, when should I use a parameter verses a return value...hmmm...

Well, when you pass in a parameter, you might expect the contents of a parameter to be modified, or even allocated and then assigned. However, you can do the same thing with the return value of a function....hmmmm...

I think it just boils down to user preference and adhering to coding practices already in place for consistency--for the most part. But, you really can achieve similar/same results doing it either way except that you can only return one item, but you can pass several as parameters....

At this point, I will let someone else take over the conversation because someone will certainly have something more to add...so, stay tuned...

Iwhen to use a void function verses a bool or int or other type of function.

Whenever you create a function, you need to know what it does
first. Does it sort an array, does it return the square of a number,
does it print a triangle? Whatever, it does, you need to know its
objective. After knowing its objective, you can easily figure out
what type function you need. If it calculate the square of a number
then make it return int, since it make sense to return the square
of number. If it sorts an array, then it should be void function
because array are pointers. If it just prints stuff then make it
void because there is no need to return anything. So analyze what
a function does and then look at the different possibilities, and
pick one that fits the situation.

Also how to know what to call into the Function?

Again this is when you know what you want case. When
you know what a function does and you know what you need
it to do, then passing the required parameters isn't hard.
Also you should have functions that you don't use. The
reason you should create functions is for clarity, mostly.
So if you decide to create a function make sure you use it,
its not too big, and you know what its supposed to do.
After that experience will guide you through.

Edited 7 Years Ago by firstPerson: n/a

This question has already been answered. Start a new discussion instead.