Thread: functions question.

  1. #1
    Registered User
    Join Date
    Feb 2008

    functions question.

    I am a beginner in C, and am currently learning about using multiple functions.

    A piece of my current assignment is to get 3 values from a function I call "Menu();", but later, another two from another function I call "total();" and use them in function "show();".

    This is all well and good, but I have to get the values from "total();" into "show();" in two seperate calls after already assigning some variables to "show();" in a seperate function.

    How can I go about doing this? Keep in mind, I am brand new to multi-functions, so please be kind.

    Sorry if this is hard to understand, I haven't picked up much Programming Lingo.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    I'm certain that this part of your assignment can be done using the values that functions can return, and functions' parameter lists. I'll explain this to you in about as much detail as you need.

    If you take any old function:
    long int calc ( int v )
      /** some calculation **/
      /** return the result as a value **/
      return v * 2;
    /** ... **/
    int x = getint( );
    long int total = calc( x );
    A function's body, contained in curly braces, are the statements the function will execute.
    A function's parameters are values it operates on.
    An optional return value stores the result of the process.

    To explain this briefly: say you get a value for x, and in this case it's 30. If we examine the parameter list for calc(), we discover that it takes an int as a parameter. As we know, thirty is an int so this is perfect.

    I can easily demonstrate how a function in general works by calling calc() with x. calc() operates it's value v, which is now a copy of x's value. It doubles v, storing the result in total. So a function can use values going in as parameters, do something with them, and return the result to where it was called.

    Knowing this, you may want to review functions in your textbook, and then start your homework.

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, 07:00 AM
  2. Functions Question
    By audinue in forum C Programming
    Replies: 2
    Last Post: 01-09-2009, 09:39 AM
  3. Question concerning functions
    By Warrax in forum C++ Programming
    Replies: 5
    Last Post: 04-04-2007, 11:00 AM
  4. Question about functions
    By richdb in forum C Programming
    Replies: 2
    Last Post: 01-22-2006, 01:18 AM
  5. Question about creating flash functions
    By jbh in forum C++ Programming
    Replies: 8
    Last Post: 11-21-2005, 09:39 AM