On C++ we have two different types of function which are non-returning value or void function and returning value function and what I would like to know under which condition you use either of those functions, and when do you know that you have to pass by reference(ByRef) or by value(ByVal) when you are passing parameters to those functions

Recommended Answers

All 4 Replies

There are a lot of considerations but to start with you can use one rule.

-> when you dont want to change the actual variable, you pass by value. in this case in a copy of the variable will be made in the called function and hence the original value will remain same.
-> when you want the value of the actual variable to change then you pass by reference. whatever changes you make to the value in the called function will change the value of the original variable in the calling function.
-> whenever the data passed is large, for examples structures or objects we prefer passbyref as a new copy will not be made.

you can explore more now...

Thanx a lot hey,

but now...... when do u know that u have to use void function or returning-value function is there any condition where u just know that u need to use one of the functions

well there again a lot depends on what you are trying to achieve. but i can give you some examples.

->setmethods a usually void. all that this method does is set the value of a member variable. hence you dont need to return anything.
->as i had written earlier functions where the parameters are huge we prefer to pass them by reference and make the function as void. In that case we will avoid copying of the variables.
->A return value function example would be the 'getmethods' which you would use to retrive the value of some member variable that you would have set earlier.
->and then most of the time when you want some value from the function to be used in your function you will need a return-value.

Thanx man, that means a lot, problem solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.