0

I have written a function to be called by a main. In the function, I have a nested function. I compile using:

gcc -o numericalIntegration numericalIntegration.c TrapezoidRule.c SimpsonsRule. GaussQuad.c -fnested-functions

Here is my TrapezoidRule:

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

#define pi 3.1415927

//Note: This program was taken from the first practical and adjusted for sin instead of tan(x) and the limits of integration changed to o --> pi/3

double degtorad(double);

float TrapezoidRule(int args) {


int i, j; //Loop index, Counter, Array dimension
float area, rad, Sin[args], coeff; //Return value of the function, Result array, Area, Coefficient
 //TODO: Get table of sin as in Ex. 3

        j=0;

        for (i=0; i<=180; i=i+5) {
                rad = degtorad(i);
                Sin[j] = sin(rad);
                j=j+1;
                }

        area = Sin[0];
        for (i = 1; i < args - 1; i++) {
                area = area + 2.0*Sin[i];
                }
 //Calculating the area using the trapezoid rule and comparing to the real area 
        coeff = degtorad(2.5);
       // area = (area + Sin[dim - 1]) * coeff; 

 //Function to convert degrees to radians
    double degtorad(double arg) {
        return( (pi * arg)/180.0 );
    }

        area = (area + Sin[args - 1]) * coeff;

return area;

}

The error I'm getting is:

Undefined symbols:
  "_degtorad", referenced from:
      _TrapezoidRule in ccdjbt6m.o
      _TrapezoidRule in ccdjbt6m.o

ld: symbol(s) not found
collect2: ld returned 1 exit status

Am I doing the nested function incorrectly?

2
  • 7
    Nested functions are a gcc extension -- and I don't see any reason for your degtorad function to be nested inside TrapezoidRule. Try moving the degtorad function out to file scope. Commented Feb 24, 2014 at 21:45
  • 1
    In the alternative, try moving the nested function to the top of the enclosing function. If I remember correctly, nested functions need to have their definition visible at the point of the call; the file-scope forward declaration you have does not qualify as a forward declaration of the nested function. See gcc.gnu.org/onlinedocs/gcc-4.8.1/gcc/Nested-Functions.html particularly the part at the very end. Commented Feb 24, 2014 at 23:15

1 Answer 1

1
  1. Remove the declaration of degtorad() before the definition of TrapezoidRule().
  2. Move the defintion of degtorad() to the beginning of TrapezoidRule(), or at least before the call to it.

If you want to declare a nested function first, and put its definition at the end of its containing function, you could do something like:

float TrapezoidRule(int args) {
    auto double degtorad(double);

    coeff = degtorad(2.5);

    double degtorad(double) {
        /* .... */
    }
}

See 6.4 Nested Functions for further details.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.