CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2007
    Posts
    37

    Sub-Functions in C

    I've been instructed to create a program that will print a table of efficiencies of a four-stroke engine for different values of compression ratios depending on the specific heat that the user enters.

    The only thing is that we've been told that we need to use sub-functions to do different things, and then execute the program through a main function. I need four sub-functions, one to calculate the efficiency, one to print the welcome/starting message with options for the user and one to print a table of the results. It says that "The main program consists of a loop that controls the overall process"

    I think I can write up the sub-functions by themselves, (I may need a little help putting in the function for the equation, but I'll come back later if I do...). I just don't know how to relate sub-functions to the main function.

    For example, I have a function that printed out a bunch of introduction messages, and told the user to enter a value for the specific heat, and then store that number in the integer k. The function is called double get_k(). How do I write in the function int main() that I want to run get_k()?

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Sub-Functions in C

    get_k() returns double, presumably the entered value.

    So it main, just use:

    double k = get_k();

    You'll either need to define get_k() above main(), or put a function declaration up there, so main knows what get_k() is. (A function declaration is the first line of the function, but you end with ; instead of starting in on {.)

  3. #3
    Join Date
    Oct 2007
    Posts
    37

    Re: Sub-Functions in C

    So you are saying that in main(), assign k to get_k(). right?

    Will that print all of the messages in the get_k() function aswell?

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Sub-Functions in C

    From the program's point of view, it sees that statement "double k = get_k()" as something of the form "type variable = expression".

    In trying to do this, the first thing it does is see if it can figure out what the value of "expression" is. In this case, in order to determine that value, it has to run get_k().

    Everything in get_k() will then occur; the program doesn't know the difference between print statements and computations.

    When a return statement is reached, the program will jump back to main knowing the value of "expression", and assign it to "variable".

    The same thing would happen if you wrote "double a = b + c;", except in that case it the program would jump to operator+(b,c), which is a built-in function, to determine the value of "expression".
    Last edited by Lindley; October 26th, 2007 at 12:45 PM.

  5. #5
    Join Date
    Oct 2007
    Posts
    37

    Re: Sub-Functions in C

    Oh okay, that makes sense, and yes, that one works. I do however, have another function that calculates eta, which is the efficiency. That one involves using the k value obtained in get_k(). This second function is called double calculate_eta (double k). If I try to bring it into main, I get an error message that says that there is a syntax error before double in the line double eta = calculate_eta (double k);. I had different error messages as well, but this is the current one.

    In both of the functions, the only type used it a double though.

    Here is what I have for main()

    Code:
    /* Create variable that will allow computer to terminate program if changed. */
    int z = 1;
    int main()
    {
        double k = get_k();
        double eta = calculate_eta (double k);
        while (z = 1);
        {
              double get_k();
              double calculate_eta(double k);
        }
        
        system("PAUSE");
        return 0;
    }

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Sub-Functions in C

    You shouldn't have "double" in the function call. You should only specify types the first time a variable occurs in a given function; after that, the compiler knows what it is and you just use the variable name.

    Most programs, in fact, will simply declare all the variables (in the manner "type name;") at the top of a function and then never mention the types again. That's a holdover from pure C, and no longer required in C++, but it does sometimes make things cleaner.

    Also, "double get_k()" doesn't make any sense right in that if statement. What you meant was "k = get_k();".

  7. #7
    Join Date
    Oct 2007
    Posts
    37

    Re: Sub-Functions in C

    okay, so what I have now is working as it should. One thing that Im not sure how to do is the calculation. I know how to write up the calculation itself, but I'm not sure if I can do what I'm thinking.

    The calculation is: eta = 1/r^(k-1)

    I have k from a different sub program, but for r, I need to have different values (1, 2, 3, 5, 10, 100 and 1000 to be exact). I need to print a table with the answers to the equation with all of these r values. I was thinking that if I set up an array, with those numbers in the array, that I could pass that array through the equation and somehow keep the answer for each r value, but I don't know if thats possible, or whether or not it makes any sense.

    Otherwise, my thought would be to have 7 different equations, with the r value already plugged into it, that produce 7 different results, that I can call eta1, eta2, eta3, eta5... and then put them in the table. But I think I read somewhere that you can only return one value from a sub-function...

    Would either of those methods work? Or is there a way that I am not thinking about?

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Sub-Functions in C

    You're clearly not yet at a level where you want to start thinking about pointers or passing arrays, so here's my suggestion.

    Stick those R values in an array in main(). Write a function to do the calculation which takes as arguments r and k, and returns eta.

    Then just put a for loop in main which does something like:

    for (i = 0; i < 7; i++)
    {
    eta = calc(r[i],k);
    print info for r[i], k, eta;
    }

    I don't know whether you're learning printf/scanf or cin/cout, so I left that part vague.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured