This article was recovered from an archive and needs to be reviewed

The formatting may be incorrect as it was automatically converted to WikiCode from HTML, it needs to be revised and reformatted

Some information may be out of date as it was written before Half-Life was available on Steam

After the article is re-formatted and updated for Steam HL, remove this notice

Please do not remove the archive notice from the bottom of the article.

Some archive articles are no longer useful, or they duplicate information from other tutorials and entity guides. In this case, delete the page after merging any relevant information into other pages. Contact an admin to delete a page.

Function Overloading is useful when you have a function that you want to do several different things with different results depending on what inputs it is given.

Example :

// Example 1
int Double( int value )
{
return 2 * value;
}

That would return an int, but what if you wanted to double a float? MSVC++6 would give you a warning saying that converting this could result in data loss.

Example :

float value = Double( 2.3 );

This would give you 2 warnings. 1 in Double and 1 because of the = operator.Remember, Double was declared as an int, and float = int results in a warning.What could you do? You could cast it, but the 2.3 would still be lost, turned into 2.

As you can see both functions have the same name, but their type is different and the value you pass is off a different type as well. Yet your compiler doesn't give you an error of duplicate declaration. (at least mine didn't :D)

Function Overloading can only work if the first parameter is different.

You can have ...

int double( int , int );

... and ...

int double( float , int );

... but NOT ...

int double( int , float );

... because the last one would have the first parameter the same as the first one, an int.

With this you can use the same function name, but give out different results.

But even this way you'd already save a line, as all you have to do then is SetHealth( pEntity, 100 ); and it would check for you if the entity you send is both existing AND alive, saving you the if line and the risk of crashes if you don't do that.

Function Overloading is there to make your code smaller and easier to read. Overuse, and especially bad overuse can make your code harder to read, so be careful how you use it.

This article was originally published on the Valve Editing Resource Collective (VERC).TWHL only archives articles from defunct websites. For more information on TWHL's archiving efforts, please visit the TWHL Archiving Project page.