Home > front end >  C Function to be called later
C Function to be called later

Time:04-03

I am a complete beginner just started to learning C . I want to make function say a formula which would take square of a variable. Check the code below to understand better

 double raiseToPow(double x, int power)
{
    double result;
    int i;
    result = 1.0;

    for (i = 1; i <= power; i  )
    {
        result *= x;
    }

    return (result);
}

The above code would be used to square a variable. I know you can save functions and then recall them to be used in the program. But I do not know how? Do I save it in a separate file and then recall it or do I write above every code. I do have searched Stackoverflow with the same query but most of the questions are way too advance for me to understand them and replicate them. Is there simple and beginner friendly explanation. Or some video that would be great.

Best Regards

CodePudding user response:

For now, add a main function and call that function. Add this to your code:

#include <iostream>

int main() {
    double value = 2.0;
    double result = raiseToPow(value, 2);
    std::cout << result << 'n';
    return 0;
}

Compile the file into an executable and run the executable.

  • Related