Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My First Project
#5
(11-10-2009, 11:16 PM)nevets04 Wrote:
Code:
int addition (int a,int b)
{
    int r;
    r=a+b;
    return(r);
}

Just a few points I will make about this which are good habits to get into.

1) Pass parameters that aren't going to be changed in the function as "const", pass complex data types (such as classes, structs, etc) as a const reference (or const pointer if you really must). The "const" part of this will allow the compiler to optimise better since you are telling it that this parameter won't be changed, the reference/pointer part will save copying the complex type (which pass by value would have done).

Example:
Code:
int addition(const int a, const int b)
{
    int r;
    r=a+b;
    return(r);
}

And for a complex data type:
Code:
int printLine(const std::string &str)
{
    std::cout << str << std::endl;
}

2) You creating a temporary variable (which is using it's default constructor), you are then assigning to that variable (using the assignment operator), before returning by value (which will take yet another copy). All in all that int is having it's value set twice, and copied once; this can be done cheaper.

Firstly, you could always use the copy-constructor of the int. (Be careful of this when using complex data types as the copy-constructor might not have been defined properly). This will save the initial defaulting of the value:
Code:
int addition(const int a, const int b)
{
    int r(a + b);
    return(r);
}

However you are still creating a temporary instance of the int to just return it by value, so the most optimal way would be to just return the sum of the two numbers:
Code:
int addition(const int a, const int b)
{
    return a + b;
}

These are trivial points for such a small application, but for larger applications which are performance critical they all start to add up. You might want to read GotW.
[Image: sig.php]
The little boat gently drifted across the pond exactly the way a bowling ball wouldn't.
Reply


Messages In This Thread
My First Project - by nevets04 - 11-10-2009, 11:16 PM
RE: My First Project - by Codine - 11-11-2009, 01:21 AM
RE: My First Project - by Extasey - 11-11-2009, 02:02 AM
RE: My First Project - by se7en - 11-11-2009, 04:49 AM
RE: My First Project - by MrD. - 11-11-2009, 07:28 AM
RE: My First Project - by uber1337 - 03-30-2010, 04:54 PM
RE: My First Project - by nevets04 - 03-31-2010, 11:45 PM
RE: My First Project - by uber1337 - 04-02-2010, 03:23 PM
RE: My First Project - by nevets04 - 04-02-2010, 11:14 PM
RE: My First Project - by Fallen - 04-05-2010, 06:21 PM
RE: My First Project - by Nyx- - 04-05-2010, 02:47 PM
RE: My First Project - by Drakeā„¢ - 04-05-2010, 02:52 PM
RE: My First Project - by Deities - 04-05-2010, 06:30 PM
RE: My First Project - by uber1337 - 04-06-2010, 05:42 PM
RE: My First Project - by GameOver* - 04-07-2010, 03:25 AM
RE: My First Project - by Navineous - 04-07-2010, 08:10 AM

Forum Jump:


Users browsing this thread: 2 Guest(s)