Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 230629

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:51:55+00:00 2026-05-11T19:51:55+00:00

I’m making a code that is similar to this: #include <stdio.h> double some_function( double

  • 0

I’m making a code that is similar to this:

#include <stdio.h>

double some_function( double x, double y)
{
  double inner_function(double x)
  { 
    // some code
    return x*x;
  }

  double z;
  z = inner_function(x);
  return z+y;

}

int main(void)
{
  printf("%f\n", some_function(2.0, 4.0));
  return 0;
}

This compiles perfectly in GCC (with no warnings) but fails to compile in ICC.

ICC gives:

main.c(16): error: expected a ";"
    { 
    ^

main.c(21): warning #12: parsing restarts here after previous syntax error
    double z;
            ^

main.c(22): error: identifier "z" is undefined
    z = inner_function(x);
    ^

compilation aborted for main.c (code 2)

What am I doing wrong?

Thanks.

(edit) Sorry for the poor example. In my original code I kinda need to do this stuff. I’m using a GSL numerical integrator and have something like:

double stuff(double a, double b)
{
  struct parameters
  {
    double a, b;
  };

  double f(double x, void * params)
  {
    struct parameters p = (struct parameters *) params;
    double a = p->a, b = b->b;
    return some_expression_involving(a,b,x);
  }
  struct parameters par = {a,b};

  integrate(&f, &par);

}

And I have lots of functions with this kind of structure: they are the result of an integration of a functions with lots of external parameters. And the functions that implements numerical integration must receive a pointer to a function of type:

double f(double x, void * par)

I would really like functions to be nested this way so my code doesn’t bloat with lots and lots of functions. And I hope I could compile it with ICC to speed things a bit.

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:51:55+00:00Added an answer on May 11, 2026 at 7:51 pm

    Everybody else has given you the canonical answer “Nested functions are not allowed in standard C” (so any use of them depends on your compiler).

    Your revised example is:

    double stuff(double a, double b)
    {
      struct parameters
      {
        double a, b;
      };
    
      double f(double x, void * params)
      {
        struct parameters p = (struct parameters *) params;
        double a = p->a, b = b->b;
        return some_expression_involving(a,b,x);
      }
      struct parameters par = {a,b};
    
      return integrate(&f, &par);     // return added!
    }
    

    Since you say that the functions such as the integrator need

      double (*f)(double x, void * par);
    

    I don’t see why you really need nested functions at all. I would expect to write:

    struct parameters
    {
        double a, b;
    };
    
    static double f(double x, void *params)
    {
        struct parameters p = (struct parameters *) params;
        double a = p->a, b = b->b;
        return some_expression_involving(a,b,x);
    }
    
    double stuff(double a, double b)
    {
        struct parameters par = { a, b };
        return integrate(f, &par);
    }
    

    The code above should work in C89 (unless there’s an issue with initializing ‘par’ like that) or C99; this version of the code is for C99 only, using a compound literal for the parameter (section 6.5.2.5 Compound literals):

    double stuff(double a, double b)
    {
        return integrate(f, &(struct parameters){a, b});
    }
    

    The chances are excellent that you have only a few variations on the ‘struct parameters’ type. You do need to provide separate (sufficiently) meaningful names for the various functions – you can only have one function called ‘f‘ per source file.

    The only marginal, tiny benefit of the nested function version is that you can be sure that no other function than stuff calls f. But given the example code, that is not a major benefit; the static definition of f means that no function outside this file can call it unless passed a pointer to the function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What do you mean when you say "appears to"? A… May 11, 2026 at 11:49 pm
  • Editorial Team
    Editorial Team added an answer If you go to the Properties of the CheckEdit and… May 11, 2026 at 11:49 pm
  • Editorial Team
    Editorial Team added an answer It looks like the only way to make the events… May 11, 2026 at 11:49 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.