Thread: functions

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    2

    functions

    i am a newbie to programming and having a hard time understanding C++ functions. may someone please explain to me in layman's terms ,with examples, what a function is and how it is used. i would greatly appreciate any help.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    Ok First of all a function is just a bit of code that could in theory be used without a function. Functions are generally used to make things easier on both the writer and the reader. Here's an example:
    Code:
    #include <iostream.h>
    float examplefunction( float number, float number2); /*declares function just like you declare a variable.*/
    
    float examplefunction( float number, float number2)/*Defines the function so that it returns the quotient of 2 arguments it receives*/
    {
    return number/number2;
    }
    
    int main()
    {
    float number, number2;/*Declares the variables*/
    cout<<"Type in first number: ";
    cin>>number;
    cout<<"Type in second number: ";
    cin>>number2;
    cout<<"The quotient is "<<examplefunction(number, number2);/*Takes the numbers inputted and inserts them into the function to be diveded*/
    return 0;
    }
    Ask me if you have any questions. Try to show me a function of your own and I'll tell you if it's good. You don't HAVE to have arguments. For example you could create a function like this:
    Code:
    int examplefunction2(void);
    That tells it that it has no arguments and then when you call the function it will just execute whatever's inside.

    Keep in mind the program I showed you uses floating point values...


    Oh yeah and by the way, just to give you a further explanation, this could all be done WITHOUT a function like this:
    Code:
    #include <iostream.h>
    int main()
    {
    float number, number2;/*Declares the variables*/
    cout<<"Type in first number: ";
    cin>>number;
    cout<<"Type in second number: ";
    cin>>number2;
    cout<<"The quotient is "<<number/number2;/*Takes the numbers inputted and divides them*/
    return 0;
    }
    Last edited by Padawan; 11-19-2003 at 12:22 AM.

  4. #4
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    functions consist of a
    (1)prototype
    (2)call
    (3)definition
    functions are used to cut the program into smaller more manageable pieces.


    Code:
     
    //prototype
    //data type (e.g. void), name (e.g. functionname), parameters inserted in the parentheses ()
    //usually located in your header file
    //see below
    
    void functionname();
    
    //call
    //name of the function with the parameters you are passing
    //this is the code in main that executes the code
    //used in your driver file or implimentation file
    
    funtionname();
    
    //definition
    //the code that the function executes
    //usually located in your implimentation file
    
    void functionname()
    {
       cout<<"hello world";
    }
    This is an example of a simple function.
    You can also make morecomplex functions by
    passing parameters and returning data.
    Last edited by xviddivxoggmp3; 11-19-2003 at 12:57 AM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM