Question about sub functions

This is a discussion on Question about sub functions within the C Programming forums, part of the General Programming Boards category; Im pretty new at this, so i need some help For a project we needed to create a "main.c" with ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    Question about sub functions

    Im pretty new at this, so i need some help

    For a project we needed to create a "main.c" with three functions

    -Function 1 gets the data

    -Function 2 calculates the data

    -Function 3 prints the data

    The problem im having is with Function 2....

    In function 2, i had to do calculations with the info, then i had to call 2 other functions from Function 2 to do more calculations.

    Ive returned the the calculations from Function 2 to main.c, to be printed in function 3, but i dont know how to return the other two to main.c

    my teacher is horrible, im basically trying to learn this on my own, so any help would be appreciated.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Post your code. Read the forum guidelines for how to post code, and other useful information.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by warthog89
    Im pretty new at this, so i need some help

    For a project we needed to create a "main.c" with three functions

    -Function 1 gets the data

    -Function 2 calculates the data

    -Function 3 prints the data

    The problem im having is with Function 2....

    In function 2, i had to do calculations with the info, then i had to call 2 other functions from Function 2 to do more calculations.

    Ive returned the the calculations from Function 2 to main.c, to be printed in function 3, but i dont know how to return the other two to main.c

    my teacher is horrible, im basically trying to learn this on my own, so any help would be appreciated.
    /* add prototypes here */


    int main() {
    GetData();
    CalcData();
    PrintIt();

    while((c = getchar()) != '\n');
    return 0;
    }

    CalcData() can call it's own sub-functions, as you've stated. Those sub functions can ALSO be called in a loop of main's, right after the call to CalData, and thus return their data that way.

    How you return the data depends on what the data is, and the overall design of the problem, and your program.

    Post up the relevant parts of your code, and explain anything not obvious OK?

    Adak

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    There are no sub functions in C.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    4
    Quote Originally Posted by warthog89
    Im pretty new at this, so i need some help

    For a project we needed to create a "main.c" with three functions

    -Function 1 gets the data

    -Function 2 calculates the data

    -Function 3 prints the data

    The problem im having is with Function 2....

    In function 2, i had to do calculations with the info, then i had to call 2 other functions from Function 2 to do more calculations.

    Ive returned the the calculations from Function 2 to main.c, to be printed in function 3, but i dont know how to return the other two to main.c

    my teacher is horrible, im basically trying to learn this on my own, so any help would be appreciated.

    Hi,

    U have the following options to send back the values calcualted in subfunctions of func2 to main.c
    1. Use global variables
    2. Pass addresses from main.c to subfunctions thru function2 and the use them in main.c after they have been updated in subfunctions..which u can confirm after the call returns from function2.


    And who said there are no subfunctions in C???

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    And who said there are no subfunctions in C???
    I did. We call them functions. BASIC calls a function that does not return a value a SUB rather than a FUNCTION. In C we call them functions and in C++ we often call them methods....although that's more of a MS habit that I'm not too keen on. Sub function is a loaded word since sub comes from the days of sub routines and when you move to C++ if you are still calling them sub functions meaning sub routine functions, you'll find that terminology just does not fit into object oriented programming. Sub routines come from the days of unstructured code which is exactly what BASIC is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner's question about functions.
    By Crocodile23 in forum C Programming
    Replies: 4
    Last Post: 01-13-2009, 06:00 AM
  2. Functions Question
    By audinue in forum C Programming
    Replies: 2
    Last Post: 01-09-2009, 08:39 AM
  3. functions question.
    By Boozel in forum C Programming
    Replies: 1
    Last Post: 02-22-2008, 11:38 PM
  4. Question concerning functions
    By Warrax in forum C++ Programming
    Replies: 5
    Last Post: 04-04-2007, 11:00 AM
  5. Question about creating flash functions
    By jbh in forum C++ Programming
    Replies: 8
    Last Post: 11-21-2005, 08:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21