Creating a Function Prototype in C++ to Calculate a Value Using f(x)

  • Context: C/C++ 
  • Thread starter Thread starter Freyster98
  • Start date Start date
  • Tags Tags
    Function Prototype
Click For Summary

Discussion Overview

The discussion centers on creating a function prototype in C++ to calculate a value using a defined mathematical function. Participants explore the correct syntax and structure for function declaration and definition, as well as performance considerations in implementation.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • A beginner participant shares their initial attempt at defining a function in C++ and seeks guidance on its correctness.
  • One participant confirms that the initial code is correct and provides a complete example including the main function.
  • Another participant clarifies that a function does not need to be defined before it is called, but a prototype is necessary.
  • A suggestion is made that using the multiplication operator instead of the pow function for integer powers could improve performance.
  • A more optimized expression for the function is proposed, emphasizing efficiency in execution.

Areas of Agreement / Disagreement

Participants generally agree on the correctness of the initial function definition and the necessity of a prototype. However, there is a debate regarding the efficiency of using the pow function versus direct multiplication.

Contextual Notes

Some participants mention performance considerations related to using the pow function for integer exponents, suggesting that alternative implementations may yield faster execution times.

Who May Find This Useful

Beginners in C++ programming, particularly those interested in function definitions and performance optimization techniques.

Freyster98
Messages
49
Reaction score
0
I am a beginner trying to enter a function prototype into C++. This is what I have entered...where am I going wrong?

double f(double x)
{
return 5*pow(x,3)-3*pow(x,2)+2*x+1;
}

I would like to be able to enter something like f(2.4) later in the code, and have it calculate the value using this function. How would I do this?

f(x)=5x3-3x2+2x+1
 
Technology news on Phys.org
There's nothing wrong with what you have.

Code:
#include <iostream>
#include <math>

double f(double x)
{
    return 5*std::pow(x,3) - 3*std::pow(x,2) + 2*x + 1;
}

int main()
{
    std::cout << f(2.4) << std::endl;
}
 
OK, thanks. I didn't know I had to declare it before main().
 
You don't have to define a function before it's called, just declare a prototype. Same code with an actual prototype:
Code:
#include <iostream>
#include <math>

double f(double x);    /* this is the prototype */

int main()
{
    std::cout << f(2.4) << std::endl;
}

double f(double x)
{
    return 5*std::pow(x,3) - 3*std::pow(x,2) + 2*x + 1;
}
 
Side note: while it is not generally an error, using pow function to raise float number to an integer power is an overkill. It is absolutely enough to use multiplication:

Code:
5*x*x*x-3*x*x+2*x+1

It will execute faster then your code. But it can be very simply modified to be executed even faster:

Code:
x*(x*(5*x-3)+2)+1
 
Thank you all for your help.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 39 ·
2
Replies
39
Views
5K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 23 ·
Replies
23
Views
3K