Thread: Pass function to function

  1. #1
    Registered User
    Join Date
    Sep 2020
    Posts
    31

    Pass function to function

    Compile hangs up at calc_run_time1 call for calc_vap_abv.
    Error message is: 'argument' undeclared (first use in this function).

    Full code is lengthy so these are pertinent snippets.
    The pointer types are arrays, except for the calc_vap_abv function. The calc_vap_abv function solves the array elements that the calc_runtime1 function then uses.

    Can you tell from snippets what I am doing wrong?
    Thx



    Definitions
    Code:
    double calc_vap_abv(
        int index, 
        double *pot_l_abv, 
        double *pot_v_abv, 
        double *pot_r_abv, 
        double c_riser1, 
        double c_riser2, 
        double init_reflux)
    Code:
    double calc_run_time1(
        int index, 
        double *pot_l_vol, 
        double *pot_l_abv, 
        double *thp_l_vol, 
        double *thp_l_abv, 
        double *pot_r_abv,                   
        double p_kw1, 
        double riser_time, 
        double c_riser1,
        double *runtime, 
        double pot_abv_dec,
        double (*calc_vap_abv)(int index, double *pot_l_abv, double *pot_v_abv, 
                      double *pot_r_abv, double c_riser1, double c_riser2, double init_reflux))
    runtime call
    Code:
     calc_run_time1(
        index, 
        pot_l_vol, 
        pot_l_abv, 
        thp_l_vol, 
        thp_l_abv, 
        pot_r_abv, 
        p_kw1, 
        riser_time, 
        c_riser1, 
        run_time, 
        pot_abv_dec, 
        calc_vap_abv);
    calc vap abv call inside runtime function
    Code:
    calc_vap_abv(
        index, 
        pot_l_abv, 
        pot_v_abv, 
        pot_r_abv, 
        c_riser1, 
        c_riser2, 
        init_reflux);
    Last edited by Salem; 03-19-2022 at 08:47 AM. Reason: Wrapped for clarity

  2. #2
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    Quote Originally Posted by Buckeye Bing View Post

    Can you tell from snippets what I am doing wrong?
    Thx
    No... You are welcome.

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    31
    haha... OK.

    Still kinda new at this. To me, compiler is saying I haven't declared the parameters to calc vap abv. So perhaps it was a syntax error because the program executes fine with the functions being separate. Also thought posting few hundred lines would obfuscate. Any help to troubleshoot really appreciated.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Compile hangs up at calc_run_time1 call for calc_vap_abv.
    > Error message is: 'argument' undeclared (first use in this function).
    There are no parameters or variables called argument in what you posted.

    > double calc_vap_abv
    Is this in scope, or it's prototype already exists, before you try to pass it as a parameter in calc_run_time1 ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2020
    Posts
    31
    Sorry... guess I'm not being clear.

    The function calc vap abv and 2 of its parameters are errored as being undeclared. Oddly, 5 other parameters not mentioned.(???)

    The functions are prototyped above main(). I understand scope as local to the function. I'd thought that using a pointer to the function calc vap abv (as a parameter to calc runtime1) gives it scope outside calc runtime1 function. If I said that properly. Correct?

    Maybe best to post the whole program?
    At 600 lines I didn't want to breach any etiquette.

    Not sure if I have a simple mistake or there is something fundamental I don't understand.

    Thanks.

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Upload a zip somewhere and post the link here.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  7. #7
    Registered User
    Join Date
    Feb 2022
    Posts
    45
    Quote Originally Posted by john.c View Post
    Upload a zip somewhere and post the link here.
    Or better still, put the code in on offsite repo and post the link to the repo. Having your code under version control and stored in an offsite repo is a critical practice for modern programming, even if it is only a practice program or course assignment.

  8. #8
    Registered User
    Join Date
    Sep 2020
    Posts
    31
    Text for main.c uploaded to Google drive. Thank you.

    main.c - Google Drive

    wanted to add the program is a work in progress from a spreadsheet that models a two stage distillation.
    Last edited by Buckeye Bing; 03-19-2022 at 05:34 PM.

  9. #9
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    I assume you know about the many many unused variables and will fix that later.
    About this function:
    Code:
    double calc_run_time1(
        int index,
        double *pot_l_vol,
        double *pot_l_abv,
        double *thp_l_vol,
        double *thp_l_abv,
        double *pot_r_abv,
        double p_kw1,
        double riser_time,
        double c_riser1,
        double *runtime,
        double pot_abv_dec,
        double (*calc_vap_abv)(int, double*, double*, double*, double, double, double))
    Note that pot_v_abv (an array declared in main) is not passed into this function at all.
    It is only mentioned as the name of a parameter to a function that is being passed in.
    I removed those names above as they are not needed.
    Those names are only for programmer information and are totally ignored by the compiler.
    You need to pass pot_v_abv in as a separate parameter if you want to pass it to that function.
    I'm assuming all of your troubles are similar to this.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  10. #10
    Registered User
    Join Date
    Sep 2020
    Posts
    31
    I'm assuming all of your troubles are similar to this.
    They were. Plus a typo.
    Compiles and works now. Thanks for the insight. I get it now.

    I know I'm an undisciplined programmer. Thanks again for taking the time to review my efforts.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-30-2012, 07:18 AM
  2. how to pass a set to function
    By iHateSicsic in forum C++ Programming
    Replies: 1
    Last Post: 01-02-2011, 10:24 AM
  3. Replies: 3
    Last Post: 11-22-2007, 12:58 AM
  4. how to pass a function as a parmeter to a function?
    By billy_other in forum C Programming
    Replies: 2
    Last Post: 04-25-2005, 12:17 AM

Tags for this Thread