My average function for c++ keeps getting errors

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 5416 Views - Last Post: 30 June 2012 - 02:15 PM Rate Topic: -----

#16 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,037
  • Joined: 21-June 11

Re: My average function for c++ keeps getting errors

Posted 27 June 2012 - 10:01 AM

View Postaxnjxn, on 27 June 2012 - 06:33 PM, said:

First, a question: Don't you have to explicitly define a function as being INLINE unless it is declared and defined within the confines of a class definition?


Only if you want to put it in a header file. It is perfectly possible for the compiler to inline a call to a function that's defined as a non-inline function in the same file in which it is called and in fact compiler do regularly do that.

Quote

But in this case, in comparison to the total activity of the program, I'd say the overhead is pretty steep. So far, we're copying 4 variables in via pass-by-value parameters and if the function isn't inline, we also have function call overhead.


First of all if the function is inlined (which it will be in gcc with optimizations on), you get the same code in both cases.

If it is not inlined, the only overhead is the cost of CALL (which is really not that big). You're talking about the cost of passing by-values, but we're talking about integers here. The cost of passing an integer by value is the cost of putting that integer into a register. And since we want to add the numbers, we need to store them in registers anyway, so there's no added cost when using a non-inlined function other than that of the CALL.

Note that even if the arguments were passed on the stack instead of as registers, you're making the cost of copying an int sound a lot more significant than it is.
Was This Post Helpful? 1
  • +
  • -

#17 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 144
  • Joined: 04-February 12

Re: My average function for c++ keeps getting errors

Posted 27 June 2012 - 10:02 AM

Ah, thank you for answering my question. So, in this case, the function IS inlined (I presume because it is defined immediately following the function prototype declaration).

As for the overhead, I still find the use of a function in this case to be wildly unnecessary whether or not registers or the stack is used. The only reason a function is used here is to teach new students of C++ about function calling and parameter passing. If there was no lesson to be learned, would you really suggest he separate the calculation of average into a function (in this example)?

This post has been edited by axnjxn: 27 June 2012 - 10:07 AM

Was This Post Helpful? 0
  • +
  • -

#18 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: My average function for c++ keeps getting errors

Posted 27 June 2012 - 10:14 AM

Quote

If there was no lesson to be learned, would you really suggest he separate the calculation of average into a function (in this example)?

Usually yes, because normally you would be using a vector/array of items when computing an average.

Jim
Was This Post Helpful? 2
  • +
  • -

#19 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,037
  • Joined: 21-June 11

Re: My average function for c++ keeps getting errors

Posted 27 June 2012 - 10:34 AM

View Postaxnjxn, on 27 June 2012 - 07:02 PM, said:

If there was no lesson to be learned, would you really suggest he separate the calculation of average into a function (in this example)?


Yes, absolutely. If I were writing any program that at some point has to calculate the average of something, I'd put the calculation of the average in its own function. Same for other small calculations that can easily be moved into their own functions.

Having a program that consists of many small, clearly named and easily understandable functions is the ideal I strive for when writing code.
Was This Post Helpful? 2
  • +
  • -

#20 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 144
  • Joined: 04-February 12

Re: My average function for c++ keeps getting errors

Posted 27 June 2012 - 10:43 AM

I understand. If I was writing a program that was going to have a number of different calculations (small or large) that would be called more than once or averaging values stored in a vector or array I would also use a function. I just wouldn't think to use one in a situation like this with a single call to do a single calculation on four values that are set in main().
Was This Post Helpful? 0
  • +
  • -

#21 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: My average function for c++ keeps getting errors

Posted 27 June 2012 - 02:52 PM

Note that the compiler is not obligated to respect your wishes when you request a function be inline. Requested or not a function may or may not be inline:

Quote

C++, C99, and GNU C each have support for inline functions. Different compilers vary in how complex a function they can manage to inline. Mainstream C++ compilers like Microsoft Visual C++ and GCC support an option that lets the compilers automatically inline any suitable function, even those not marked as inline functions.

Was This Post Helpful? 0
  • +
  • -

#22 busyme12srv  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 44
  • Joined: 18-June 12

Re: My average function for c++ keeps getting errors

Posted 28 June 2012 - 03:05 AM

You are not storing the value of the result of the computation anywhere.
It should be:

float Average(int v1,int v2,int v3, int v4)
{
.
.
.

ave=(v1+v2+v3+v4)/4
return ave

}
in your function.
Also , declare the function as

float Average(int v1,int v2,int v3, int v4)
Was This Post Helpful? 0
  • +
  • -

#23 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 630
  • View blog
  • Posts: 2,107
  • Joined: 31-December 10

Re: My average function for c++ keeps getting errors

Posted 30 June 2012 - 09:24 AM

View Postaxnjxn, on 27 June 2012 - 11:33 AM, said:

First, a question: Don't you have to explicitly define a function as being INLINE unless it is declared and defined within the confines of a class definition?

But in this case, in comparison to the total activity of the program, I'd say the overhead is pretty steep. So far, we're copying 4 variables in via pass-by-value parameters and if the function isn't inline, we also have function call overhead. Since the program itself only declares 4 variables, I'd say it would be much more efficient to simply put the average calculation right in the cout statement. Of course there's nothing wrong with a single call function to organize code, but in this case I'd say your stretching it.

There's a quote by Donald Knuth, "premature optimization is the root of all evil". So first off, the "overhead" of calling the average function is actually pretty small so this is something a beginner shouldn't have to worry about. Instead, you're probably making things more confusing by making this suggestion.

You say the overhead is pretty steep, can you explain what the overhead actually is? If you can't, then how do you know it's "steep"?
Was This Post Helpful? 1
  • +
  • -

#24 starrim  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 21
  • Joined: 08-May 12

Re: My average function for c++ keeps getting errors

Posted 30 June 2012 - 02:11 PM

Your function was void type, it won't return a value.
And you didn't make it return a value.
void Average(int value1, int value2, int value3, int value4)     
 {  

  ((value1 + value2 + value3 + value4)/4);  

 }  



try to make it
double Average(int value1, int value2, int value3, int value4)     
 {  

  return((value1 + value2 + value3 + value4)/4);  

 }  


The function won't have a value unless you returned one.And void type won't return anything unless you make it to do so.
Was This Post Helpful? 0
  • +
  • -

#25 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,037
  • Joined: 21-June 11

Re: My average function for c++ keeps getting errors

Posted 30 June 2012 - 02:15 PM

View Poststarrim, on 30 June 2012 - 11:11 PM, said:

Your function was void type, it won't return a value.


It's generally considered polite to read the entire thread before answering. People already pointed this out days ago (and the OP already said that he's not allowed to change the return type, so recommending the same thing again really serves no purpose).
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2