AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Coding issue. (https://forums.alliedmods.net/showthread.php?t=131395)

fezh 07-04-2010 16:17

Coding issue.
 
I'm wondering why I usually see this coding style instead of the one I used my whole life, example:
What I do:
PHP Code:

public MySuperFuncid 

But instead, I see:
PHP Code:

public MySuperFunc( const id 

Is there any efficiency issue or it's just a readability thing?

joaquimandrade 07-04-2010 16:22

Re: Coding issue.
 
Consistency :twisted:

But is not really worth it because single cell values are passed by value so, even if you change id by mistake, it won't be a big problem because you are changing it only in the function scope.

Alucard^ 07-04-2010 17:59

Re: Coding issue.
 
I thinked that are used only for strings, i am wrong? sry for doing this question but i didn't understand at all, what joaquim said.

fysiks 07-04-2010 18:02

Re: Coding issue.
 
If you use "const id" then you can't do "id = 2" and then use it as 2. Strings are passed by reference.

Bugsy 07-04-2010 18:37

Re: Coding issue.
 
When a single cell is passed to a function it is passed by-value (by default) meaning only the value stored by the variable is sent to the function; the actual memory/variable passed will remain unchanged if the function happens to alter the value. Arrays/strings on the other hand are always passed by-reference meaning the memory address is passed to the function so if the function alters the value, the original array or string that was passed will be changed.

If you understand that clearly you will realize const is only necessary when a var is passed by reference which will prevent the function from altering the actual variable/memory that was passed. As quim said, it does not hurt to use const for the sake of consistency and readability.


All times are GMT -4. The time now is 07:11.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.