user3306583
user3306583

Reputation: 119

Calling external functions in C

so I'm trying to make a main function that calculates the area under a curve. I have three separate functions that each calculate the area differently: function 1 calculates using the trapezoid rule, function 2 calculates using Simpsons rule, and function 3 calculates using the Gauss Quadrature. I had each individual program working well and am now trying to change them from main to functions to be called from a separate main called "numericalIntegration".

So far I have for my main:

#include <math.h>
#include <stdlib.h>

#define pi 3.1415927;

void TrapezoidRule(float area);
void SimpsonsRule(float area);
void GaussQuadrature(float area);

int main() {

int userInput, N;
float area, error;

    printf("Choose which method to use to calculate the area of the function sin(x) from 0 to pi:\n");
    printf("Enter 1 to use the Trapezoid Rule, enter 2 to use Simpson's Rule, enter 3 to use Gauss' Quadrature.\n");
        scanf("%d", &userInput);
        printf("\nEnter the number of intervals to use to calculate the area.\n");
        scanf("%d", &N);

        if (userInput == 1) {                //Call Trapezoid rule function
           TrapezoidRule(area);
           }
        if (userInput == 2) {
           SimpsonsRule(area);
           }
        if (userInput == 3) {
           GaussQuadrature(area);
        }

        //Print the area calculated using the chosen method     
return 0;

}

If needed I'll include the three separate functions but to keep this post shorter I'll exclude them for now. They are each called:

void TrapezoidRule(float area) {
void GaussQuadrature(float area) { 
void SimpsonsRule(float area) {

One error I'm getting when I try to compile (using icc -o num numericalIntegration.c GaussQuadrature.c TrapezoidRule.c SimpsonsRule.c) is that the trapezoid rule and simpson's rule both use a small function in them to convert degrees to radians which I guess isn't being called properly in the numericalIntegration.

So here are my specific questions:

  1. is this method correct for calling functions in a main?
  2. should I make the degrees to radians function a header file to include in the main? I'm not sure how to do this...

UPDATE: The error I'm getting says: 1st error: "multiple definition of 'degtorad'" 2nd error: first defined here

I think I understand that I need to make the function degtorad a header file but I don't know how to do this?

Upvotes: 0

Views: 6435

Answers (4)

hyde
hyde

Reputation: 62777

First of all, to call the functions correctly, you need to have them declared, to have their prototypes. Usually they would come from include files, but you can also just declare them before using them. To do this, add these lines before main():

void TrapezoidRule(float area);
void GaussQuadrature(float area); 
void SimpsonsRule(float area);

Normally these would be in include file, but C include is essentially same as copy-pasting contents of include file, so end result is same. You will really want the include files once you have more functions, or when you need to use them from many places.

And if you have some utility function, which is defined in just one of you files, but used in many, then have it in one place, and just have declaration where you use it, similarly to above. Alternatively, if you have utility function in many files with same name, make it static, so linker will not complain about multiple definitions of same symbol (but if they are the same function, this is bad idea, because now you have many copies of same function and you may need to change them all later).

Upvotes: 2

Michael
Michael

Reputation: 968

If you are defining those functions in a new file, you need to write a header file (.h) that tells the compiler what the function looks like, and at the top of the file, have

#include "GaussQuadrature.h"
#include "TrapezoidRule.h"
#include "SimpsonsRule.h"

then in the header file, you'll (probably) just need to put:

void TrapezoidRule(float area);

Upvotes: 0

Filippo Lauria
Filippo Lauria

Reputation: 2064

you must compile the c source containing that "small function ... to convert degrees to radians".

If you type fun() { ... } is in fun.c you must include it into your compile command string:

icc -o num fun.c numericalIntegration.c GaussQuadrature.c TrapezoidRule.c SimpsonsRule.c

Upvotes: 0

Jens
Jens

Reputation: 72619

Chances are you forgot to link with the math library libm.a. Do so by adding -lm to the compiler invocation doing the final link step.

Upvotes: 0

Related Questions