Thread: Passing functions to a function help

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

    Passing functions to a function help

    The goal is to calculate the total mols in a binary mix given the total volume and vol% concentration.

    Trying to pass 3 functions to another function, only two of which take an argument.

    One error suggests incompatible type for argument 1 but I'm not seeing it.

    The other error suggests the calling function is expecting a *(pointer?) Don't understand that one either.

    Thanks for any help
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <math.h>
    
    
    #define W_DENS 0.998       //Water density at 20 degrees C
    #define E_DENS 07892       //Ethanol density at 20 degrees C
    #define MM_W 18.0153       //Molar mass of water - grams per mol. Source NIST
    #define MM_E 46.0684       //Molar mass of ethanot grams per mol. Source NIST
    #define GAL2L 3.785411784  //Gallons to liters factor
    
    
    /*********FUNCTION PROTOYPES*************************/
    double abv_to_abw(double abv);   //convert alcohol by volume to alcohol by weight
    double abw_to_mf_e(double abw);  //convert alcohol by weight to mol fraction of ethanol
    double calc_total_mol(double get_input(), double abv_to_abw(double abv), double abw_to_mf_e(double abw));
    double get_input(void);
    
    
    int main()
    {
        double total_mol = calc_total_mol(get_input(), abv_to_abw(abv), abw_to_mf_e(abw));
        printf("\ntotal mols = %lf", total_mol);
        return 0;
    }
    
    
    /*********FUNCTION DEFINITIONS*************************/
    
    
    double get_input(void)
    {
    double input = 0;
    scanf("%lf", &input);
    return input;
    }
    /**********************************/
    
    
    //alcohol conversion functions abv to abw results are between 0 and 1 and only valid for input between 0 and 1
    //From "On the Conversion of Alcohol by Edwin Croissant -  R1: 2014*02-16
    
    
    double abv_to_abw(double abv)
    {
        const double a = 0.00018684999875047631;
        const double b = 0.77602465132552556;
        const double c = 0.41803095099103116;
        const double d = -2.5221614925275091;
        const double f = 9.5827123045656251;
        const double g = -19.928886159385002;
        const double h = 24.165120890385651;
        const double i = -15.830262207383321;
        const double j = 4.3390473620304988;
    
    
    
    
        double temp = j;
        temp = temp * abv +i;
        temp = temp * abv +h;
        temp = temp * abv +g;
        temp = temp * abv +f;
        temp = temp * abv +d;
        temp = temp * abv +c;
        temp = temp * abv +b;
        double abw = temp * abv +a;
    
    
        return abw;
    }
    /*******************************/
    
    
    double abw_to_mf_e(double abw)
    {
       double mf_e = abw / (abw + MM_E / MM_W * (1-abw));
       return mf_e;
    }
    /*******************************/
    
    
    double calc_total_mol(double get_input(), double abv_to_abw(double abv), double abw_to_mf_e(double abw))
    {
        printf("\nEnter Wash Gallons");
        double wash_l = get_input() * GAL2L;
    
    
        printf("\nEnter ABV (between 0 and 1)");
        double abv = get_input();
    
    
        double water_mol = wash_l *(1-abv) *1000 *W_DENS / MM_W;
        double abw = abv_to_abw(abv);
        double eth_mol = abw_to_mf_e(abw);
    
    
        return water_mol + eth_mol;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,435
    Function pointers are their own special class of weird.
    You were just missing some () around the parameter names.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    #define W_DENS 0.998       //Water density at 20 degrees C
    #define E_DENS 07892       //Ethanol density at 20 degrees C
    #define MM_W 18.0153       //Molar mass of water - grams per mol. Source NIST
    #define MM_E 46.0684       //Molar mass of ethanot grams per mol. Source NIST
    #define GAL2L 3.785411784  //Gallons to liters factor
    
    
    /*********FUNCTION PROTOYPES*************************/
    double abv_to_abw(double abv);   //convert alcohol by volume to alcohol by weight
    double abw_to_mf_e(double abw);  //convert alcohol by weight to mol fraction of ethanol
    double calc_total_mol(double (*get_input)(), double (*abv_to_abw)(double abv), double (*abw_to_mf_e)(double abw));
    double get_input(void);
    
    
    int main()
    {
        double total_mol = calc_total_mol(get_input, abv_to_abw, abw_to_mf_e);
        printf("\ntotal mols = %lf", total_mol);
        return 0;
    }
    
    
    /*********FUNCTION DEFINITIONS*************************/
    
    
    double get_input(void)
    {
    double input = 0;
    scanf("%lf", &input);
    return input;
    }
    /**********************************/
    
    
    //alcohol conversion functions abv to abw results are between 0 and 1 and only valid for input between 0 and 1
    //From "On the Conversion of Alcohol by Edwin Croissant -  R1: 2014*02-16
    
    
    double abv_to_abw(double abv)
    {
        const double a = 0.00018684999875047631;
        const double b = 0.77602465132552556;
        const double c = 0.41803095099103116;
        const double d = -2.5221614925275091;
        const double f = 9.5827123045656251;
        const double g = -19.928886159385002;
        const double h = 24.165120890385651;
        const double i = -15.830262207383321;
        const double j = 4.3390473620304988;
    
    
    
    
        double temp = j;
        temp = temp * abv +i;
        temp = temp * abv +h;
        temp = temp * abv +g;
        temp = temp * abv +f;
        temp = temp * abv +d;
        temp = temp * abv +c;
        temp = temp * abv +b;
        double abw = temp * abv +a;
    
    
        return abw;
    }
    /*******************************/
    
    
    double abw_to_mf_e(double abw)
    {
       double mf_e = abw / (abw + MM_E / MM_W * (1-abw));
       return mf_e;
    }
    /*******************************/
    
    
    double calc_total_mol(double (*get_input)(), double (*abv_to_abw)(double abv), double (*abw_to_mf_e)(double abw))
    {
        printf("\nEnter Wash Gallons");
        double wash_l = get_input() * GAL2L;
    
    
        printf("\nEnter ABV (between 0 and 1)");
        double abv = get_input();
    
    
        double water_mol = wash_l *(1-abv) *1000 *W_DENS / MM_W;
        double abw = abv_to_abw(abv);
        double eth_mol = abw_to_mf_e(abw);
    
    
        return water_mol + eth_mol;
    }
    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.

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    31
    Thank you. Much appreciated.
    Gained a little insight into pointers as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating Functions & passing information to other functions
    By RyanLeonard in forum C Programming
    Replies: 4
    Last Post: 10-28-2010, 12:17 PM
  2. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  3. Passing from functions in classes to another function in another class?
    By Robert_Sitter in forum Windows Programming
    Replies: 1
    Last Post: 12-13-2005, 07:46 AM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM

Tags for this Thread