Function with single parameter to evaluate an expression – CSKC
Sign In

Search

Code Learning #CPP#2179    siteicon   siteicon  

Function with single parameter to evaluate an expression

Writing a function with one parameter to evaluate an expression.

Learning Objectives

  • Learning the concept of function prototype or declaration.
  • Learning the way to call a function.
  • Learning to construct the function definition in its four parts – return type/name/parameters and body.

Source Code

TC++ #2179

 

Source Code

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

Code Understanding

int myexp(int);
This line is called the function prototype or the function declaration. It is put above the main function of any program. This can also be inside an include file if user defined include files are made. It has construct of the <return data type> <function name>(Function parameter(s))

int n; cout<<“Enter an Integer : “; cin>>n;
Here we collect the input from the user which we shall use as the function parameter.

int ex=myexp(n);
This is function calling calling staement. The left hand side is the output received after the function has been run. In this case the datatype of output has also been declared. The right hand side is the function name and inside the function round braces, the actual parameter of the function which is the value of n is given.

cout<<“Value of expression x^3+x^2+x “<<“with given value x=”<<n<<” would be = ” <<ex<<endl;
This is the output display line formed with proper chaining in the cout expression.

int myexp(int x)
This is the beginning of function definition or just the function as we call it. It has three parts – <The return data type> <The function name>(<formal parameters>)
{
After the function definition initial part we write the function body inside the braces { } int y=x*x*x+x*x+x;
The instructions in the function will now use the formal parameter names passed to the function. In this case it is x of int type. In this line we are applying the expression x^3+x^2+x to our input parameter and get it into the variable y which is declared inside the function itself.

return y;
This is the final line of a function which returns the given value (in this example y) to the place from where the function is being called (in this example from main()) . The datatype of returned value should be same as been declared initially and shown as part of the function definition heading also.
}

Notes

  • When function definition itself is written above main() function then the separate prototype line may not be required.

Common Errors

  • Students often forget that the prototype line does not require the formal parameter name. It just needs the datatype of the parameter. For e.g.
    int myexp(int); //is correct
    int myexp(int x); //is not correct
  • Student some times make a mistake of giving semicolon (;) in the function definition part just after the parameters. for e.g.
    int myexp(int x);  //This semicolon is not correct
    {
    … instructions;
    }


Suggested Filename(s): eval-exp.cpp



Share

Created: 4-Jan-2018 23:47 | Updated: 15-Sep-2018 14:02|