Thread: Semi-noob Q: calling functions

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    13

    Semi-noob Q: calling functions

    What is the point of calling a function? I'm currently reading Sam's teach yourself C++ and its discussing functions. In a sample program, the function is first declared using prototypes. Then it is called using main and then the function is executed. Does calling a function activate it? Thanks for help.

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Yes, calling a function executes the code contained within it. Functions are used to seperate code into more efficient blocks. If you had, say, a block of code which is executed many times during a program, instead of writing the same code in many times, you just place it in a function, and then call that function whenever you need to execute its code. Here is a trivial example:
    Code:
    int MultiplyTwoNumbers(int FirstNumber,int SecondNumber)
    {
        return (FirstNumber * SecondNumber);
    }
    
    int main()
    {
        int Answer=MultiplyTwoNumbers(5,7);
    
        return Answer;
    }
    Here, a function, 'MultiplyTwoNumbers', is defined, and it takes two ints as parameters and returns an int. This means that the int 'Answer' will be assigned the value 5*7 =35.

    Make sure to only use functions to avoid heavy repetitions of code, or to encapsulate a certain activity.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Actually, given the question "What is the point of calling a function", I don't think that was a good example But consider functions like std::cin.getline(). It would be a big pain in the rear if every time you wanted to get input you had to type out the code for getline(). In short, functions are used to simplify a program, so you don't have to do this:
    Code:
    //Read a character from the user
    int a = 239as7f@#ajflksjfa;
    faobiu(213.z;aWEr0as
    fjskldafl213#3(f23");
    fsdafjlke
    3289a7sdeFSJALK
    fsjlakwejf
     
    //Read another character from the user
    int a = 239as7f@#ajflksjfa;
    faobiu(213.z;aWEr0as
    fjskldafl213#3(f23");
    fsdafjlke
    3289a7sdeFSJALK
    fsjlakwejf
    **EDIT** Hmm, actually I guess "What's the point of" could also be read as "What does _x_ mean?"... If that's the case, read benny's post and ignore mine
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I Need Help Defining and Calling Functions
    By jonbuckets in forum C++ Programming
    Replies: 6
    Last Post: 10-25-2007, 09:46 AM
  2. Noob + Functions == Problems
    By Inao in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2007, 04:02 PM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Functions calling themselves...?
    By mikebrewsj in forum C++ Programming
    Replies: 10
    Last Post: 01-18-2002, 12:09 AM
  5. Calling functions written in assembly?
    By The V. in forum C Programming
    Replies: 5
    Last Post: 10-24-2001, 08:11 PM