Thread: Functions

  1. #1
    Registered User mike's Avatar
    Join Date
    Aug 2001


    Could someone please try and explain funtions clearly to me. I understand the syntax of them but I get confused on returning values and why the prototype statement has different parameter names than the funtion itself. thanks for your help!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    When you define a function, you give the names of its arguments something that makes sence to the function:
    void drawSquare( int upperLeftX, int upperLeftY, int lowerRightX, int lowerRightY )
        do some stuff here
    Now when we call this function, we pass it variables that we've already been working with, or have defined someplace else, that already have names:


    drawSquare( coordinate1.x, coordinate1.y, coordinate2.x, coordinate2.y );

    The above would call the function using two "coordinate" structure's variables. We could have just as easily used any 4 integers.

    drawSquare( 0, 0, 10, 10 );

    But when we're inside the function, we want our own names for them so we know how they relate to the function.

    You 'return' a value to wherever you call it, if you need to make use of something that the function was using, or what not.

    int myAdd( int n, int o ) { return n+o; }

    Then we could do:

    int y;

    y = myAdd( 3, 4 );

    y will now equal 7.

    Hope is the first step on the road to disappointment.

  3. #3
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001


    first, the function prototype does not have different param names than the function. ex.:

    int func1(int x, int y); //prototype

    blah blah

    int func1(int x, int y) //definition, or "function itself"
    return x+y;
    //Notice that x and y in the main program are not changed, they retain the same value. Scroll down to see how to change the values.

    This will add the values of x any y, and return them. First I want to say that you could also write the prototype like int func1(int, int); You only have to put the corresponding variable types, and leave out the name. Ok, that function adds the value of x and y and returns it to whatever you're doing. Ex.:


    This will print out the sum of a and b. You could also do:

    int sum=func1(a,b);

    This will assign the sum of a and b to sum. Now, if you want to just want change some variables values, and return nothing, then you must use pointers, and replace int with void. Ex.:

    #include "iostream.h"

    void func1(int* num);

    int main()
    int num=1;
    cout<<"num="<<num<<" before function is called."<<endl;
    int* pnum=&num;


    cout<<"num="<<num<<" after function is called."<<endl<<endl;

    return 0;

    void func1(int* num)

    This will change num permanentlly because pointers hold the address of a variable and when passed to a function, it still has the address. Notice when you use void, you do not return a value.
    You can do the same with references. Need any more help just ask. You can also return a pointer, not just the value inside of it. Ask if you want to know how.

  4. #4
    Registered User
    Join Date
    Aug 2001
    mike I am a bit of a newbie myself but feel it is time to try and be of some assistance to others .

    Functions as you probably know are designed to split your code up into smaller manageable pieces . In a program you may want a certain block of code to be repeated numerous times during its execution. This is the obvious advantage of functions so that you dont have to type out the same section of code more than once in a program.

    In a prototype for a function here are certain options

    void addnum (void)

    this means no arguements are sent or received from this function .

    This kind of function would probably be useful if you know the exact format of your program but you want to have less code in your



    if you had this code
    int addnum (num1 , num2);
    int main(void)
       int a=3;
       int b=4;
       cout << addnum(a,b);
       return 0;
    int addnum (num1, num2)
       return num1+num2 
    would take the arguments a and b and use them as if they were num1 and num2 in the function then the int would be returned to the main function.

    Hope this was of some help . I havent checked for errors in my code but i am sure that someone will point em out .

    Hope i was of some help to you .

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Are you Mike Herin? It's a long explanation why i think you are, but are you? And The Gweech - once you start helping other's, you'll be amazed how much you learn yourself!

  6. #6
    Registered User mike's Avatar
    Join Date
    Aug 2001


    Thank You all for your help

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