Function parameters?

This is a discussion on Function parameters? within the C++ Programming forums, part of the General Programming Boards category; In my book it talks about using function parameters. My question is what is the main purpose for a function ...

  1. #1
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280

    Question Function parameters?

    In my book it talks about using function parameters. My question is what is the main purpose for a function parameter? Does it just make it where the current function can use a local variable that is located in a different function?

  2. #2
    Shadow12345
    Guest
    a function parameter is a variable passed to a function so it can do what it needs to do. if you want to calculate the area of a square in math class, you must provide the width and height, the same is true for a function that calculates the area, you must pass the width and height parameters

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    It's purpose is to be able to pass variables to the function
    from wich it was called

    Code:
     
    void function1()
    {
    
    int a=5; //function 1 has a var noone else can acces
    function2(a); // However whe can pass it to function 2
    
    }
    
    void function2(int anumber)
    {
    cout << anumber;
    }

  4. #4
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    So I could do something like-

    Code:
    int main()
    {
        long p
        cout << "Enter the number: ";
        cin >> p;
        function(p);
        return 0;
    }
    
    int function(p)
    {
        cout << "The value of p is" << p;
        return 0;
    }
    ?

  5. #5
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    yes, but you should put a semicolon after 'long p' and change 'function's return type to 'void' (because it doesn't return anything).
    If I had a world of my own, everything would be nonsense. Nothing would be what it is, because everything would be what it isn't. And contrariwise, what it is, it wouldn't be, and what it wouldn't be, it would. You see?

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>change 'function's return type to 'void' (because it doesn't return anything).
    Wrong, it returns 0.

    >>int function(p)
    This is incorrect, you need to specify the variable type, like this:
    >>int function(long p)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    I saw the semicolon I missed, just a quick example. But why change it to void? It has a return statement at the end of it.

    EDIT: Thanks for clearing that up hammer.

  8. #8
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    >>change 'function's return type to 'void' (because it doesn't return anything).
    >Wrong, it returns 0.

    sorry.. i missed that. it should be changed to void and the 'return 0' taken away, since it serves no purpose.. shouldn't it?
    If I had a world of my own, everything would be nonsense. Nothing would be what it is, because everything would be what it isn't. And contrariwise, what it is, it wouldn't be, and what it wouldn't be, it would. You see?

  9. #9
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    I thought every function should return a value based on ANSI compliancy. Isn't this true?

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Munkey01
    I thought every function should return a value based on ANSI compliancy. Isn't this true?
    no. You can do as MadHatter suggests if you want.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Is void main() ANSI compliant?

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    No, main is a special case. Do a board seach if you want to know more, it's been discussed far too many times already
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Thank you. I think I will do that search.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. function with variable number of parameters
    By mikahell in forum C++ Programming
    Replies: 3
    Last Post: 07-23-2006, 03:35 PM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Replies: 5
    Last Post: 02-08-2003, 06:42 PM

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