Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best practice for parameters?

Is it generally considered better to pass parameters as pointers rather than as value when you can? Obviously it largely depends on the situation, but when there is a choice, is it better to use pointers?

Is this simply for reasons of memory?

And what is better to pass through if it is true, a pointer or a reference?

like image 535
Dollarslice Avatar asked Oct 13 '11 13:10

Dollarslice


People also ask

What are different types of parameters?

Out Parameters. Default Parameters or Optional Arguments (C# 4.0 and above) Dynamic parameter (dynamic keyword). Value parameter or Passing Value Types by Value (normal C# method param are value parameter)

How many types of parameters are there?

We can use all 4 types of parameters in one function.

What is order of parameters in Java?

In a method or constructor invocation or class instance creation expression, argument expressions may appear within the parentheses, separated by commas. Each argument expression appears to be fully evaluated before any part of any argument expression to its right.


1 Answers

Some general rules of thumb:

If you need to modify it, pass a pointer or a reference. If the value might be null, pass a pointer, otherwise pass a reference.

If it's large, pass a const pointer or const reference, depending on whether null is a legal value.

If using pointers, prefer smart pointers to bare pointers.

Otherwise, pass by value.

like image 152
Andy Thomas Avatar answered Sep 19 '22 12:09

Andy Thomas