Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Would someone please explain how to place a function in my main.ccp file in the c++ language?

+2 votes
asked Mar 6, 2020 by WillPope (190 points)
So far I've written this,

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
    double pi;
    pi = ( asin(1)*2 );
    cout << " The value of pi is: " << pi << setprecision(pi) << endl;
}

But I need to place this function in my main.ccp file, place code in my main function to call my pi() function and store the returned value in a variable.  Then using cout, display the value, as well as the value of M_PI to the console for comparison purposes.

I was forced to miss lecture this day and now as a result I'm now completely confused regarding what this is even asking for, so any help would be greatly appreciated!

2 Answers

0 votes
answered Mar 7, 2020 by wyattbiker (240 points)

An example function is at this link http://www.cplusplus.com/doc/tutorial/functions/

You add the function to the main.cpp file.

E.g.


...


int main(){

double my_pi;
my_pi=pi();
cout goes here...
}

// This is the function
double pi(){
     return asin(1)*2;
}

Also read the example http://www.cplusplus.com/reference/iomanip/setprecision/ how setprecision is used.

0 votes
answered Mar 9, 2020 by Vedant (150 points)
Treat main as a function:
- You have no parameters in the () after main.
- You have to return an integer at the end of the function, though most compilers will automatically  return a value for main so you dont need to.

Essentially, just write the function like this:

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int setprecision(int pi);

int main()
{
    double pi;
    pi = ( asin(1)*2 );
    cout << " The value of pi is: " << pi << setprecision(pi) << endl;
}

int setprecision()
{
    return pi;
}

--
Of course, you'll need to change the code according  to want you want. For more information, check out: http://learn.onlinegdb.com/c%2B%2B_functions
commented Mar 9, 2020 by Fatai Sule (100 points)
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int setprecision(int pi);

int main()
{
    double pi;
    pi = ( asin(1)*2 );
    cout << " The value of pi is: " << pi << endl;
}

int setprecision(int pi)
{
    return pi;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...