CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Hybrid View

  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();".

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