Like Tree2Likes
  • 1 Post By spark*
  • 1 Post By MK27

Noob question about functions

This is a discussion on Noob question about functions within the C++ Programming forums, part of the General Programming Boards category; Hello all! I am having trouble with the following code: Code: #include <iostream> using namespace std; void changeArgument (int x) ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    9

    Noob question about functions

    Hello all!

    I am having trouble with the following code:

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    void changeArgument (int x)
    
    
    {
    x = x + 5;
    }
    
    int main()
    
    {
               int y = 4;
               changeArgument( y ); // y will be unharmed 
               cout << y; // still prints 4
    }
    I started playing around with the code to get an idea of how it worked only to find the c++ rug underneath my feet slip away.

    I omitted the variable declaration (y) passing an integer 4 as the argument, but the compiler is complaining that there were "too few arguments to function void changeArgument(int)."

    I also changed "cout <<y;" to "cout << changeArguement ( 4 );"
    Same error....

    Does anyone know why this is happening? I was under the impression that I could pass arguments of type integer to this function for it to work.

    Why is it required that in main() one needs to declare an additional variable, and assign it a value? As the x variable is already declared locally inside the function, I thought that one would only need to pass an argument of its type for it to return a value. Despite the fact that changeArguments() return type is void, running the above code Does return a value.

    Any help would be immensely appreciated.

    Thank you so much for your time!

    spark*

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,433
    The function argument is passed by value, so the change to the parameter x changes a local variable in changeArgument, not the variable from main. If you have learnt about reference parameters, use that instead.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    9
    Thank you for replying again laserlight!

    My question is, why do I need to change the variable to Y and assign a new value for it to work at all? I thought it was possible to simply run the function as "changeArgument (4);" in main(), without having to change the variable from x to y.

    In fact, I should be able to run changeArgument (); without any arguments as an expression has supposedly already been declared in the function as x = x + 5;

    Shouldn't running changeArgument () without arguments equal 5?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by spark* View Post

    I omitted the variable declaration (y) passing an integer 4 as the argument, but the compiler is complaining that there were "too few arguments to function void changeArgument(int)."
    If you mean you tried this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    void changeArgument (int x)
    {
    	x = x + 5;
    }
     
    int main()
    {
    	changeArgument( 4 ); // pass int literal
    	return 0;	// main() should return an int!
    }
    Then try again -- there is nothing wrong with that.

    I also changed "cout <<y;" to "cout << changeArguement ( 4 );"
    Same error....
    That would be an error, but not the error you refer to (you should post the code that actually produced that -- you have made a mistake or misinterpreted something).

    The reason changeArgument() does not work with cout's << is because the return type is void. Try this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int changeArgument (int x)
    {
    	x = x + 5;
    	return x;
    }
     
    int main()
    {
    	cout << changeArgument( 4 ); 
    	return 0;	
    }
    WRT post #3, you need to keep posting code to demonstrate what you are talking about, because you sound confused.

    In fact, I should be able to run changeArgument (); without any arguments as an expression has supposedly already been declared in the function as x = x + 5;
    No, if the prototype for changeArgument() requires an int you must submit one. It doesn't matter whether it is used, how it is used, what other ints exist, etc.
    Last edited by MK27; 03-18-2012 at 08:50 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    9
    Quote Originally Posted by MK27 View Post
    If you mean you tried this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    void changeArgument (int x)
    {
        x = x + 5;
    }
     
    int main()
    {
        changeArgument( 4 ); // pass int literal
        return 0;    // main() should return an int!
    }
    Then try again -- there is nothing wrong with that.



    That would be an error, but not the error you refer to (you should post the code that actually produced that -- you have made a mistake or misinterpreted something).

    The reason changeArgument() does not work with cout's << is because the return type is void. Try this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int changeArgument (int x)
    {
        x = x + 5;
        return x;
    }
     
    int main()
    {
        cout << changeArgument( 4 ); 
        return 0;    
    }
    WRT post #3, you need to keep posting code to demonstrate what you are talking about, because you sound confused.



    No, if the prototype for changeArgument() requires an int you must submit one. It doesn't matter whether it is used, how it is used, what other ints exist, etc.
    Hello Mk27,

    Well thank you very much. I understand now what I was doing wrong from this code that you posted:

    Code:
    {
        x = x + 5;
        return x;
    }
    
    
    int main()
    {
        cout << changeArgument( 4 ); // still prints 4
        return 0;
    }
    And Yes, I was confused. I hope that is not a problem. If so, then that's disappointing. As I mentioned in my post, I am a noob and apologize if I annoyed in anyway. I will post code as frequent as possible in the future.

    Turns out not include return statements and not changing the return type for changeArgument was the problem. Go figure, a beginner forgetting important keywords.

    Thank you anyway for the solution as the issue makes more sense now.

    Thank you laserLight for all your help as well!!
    Last edited by spark*; 03-18-2012 at 09:12 PM.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by spark* View Post
    And Yes, I was confused. I hope that is not a problem.
    Of course not. That's what the forum is for.

    As I mentioned in my post, I am a noob and apologize if I annoyed in anyway. I will post code as frequent as possible in the future.
    It's not that it's annoying, it's that it tends to lead to confusion and frustration (mostly for you), because of the potential discrepancy between a description of code and code itself. This doesn't just apply to noobs -- everyone makes mistakes by overlooking something they though was insignificant. Description usually doesn't include stuff the describer thinks doesn't matter. Hence, it is always important to see the actual code .
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    9
    Hey Mk27!

    Awesome! Glad I misread you there!

    Thank you SO MUCH again for your time and effort! It really cleared up the confusion for me ( and sorry for the luke warm thank you). Again, I will heed your advice on posting code in the future!

    take care,

    spark*
    MK27 likes this.

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    Code:
    #include <iostream>
    
    //Stops conflicting namespaces in the future. 
    using std::cin;
    using std::cout; 
    using std::endl;
     
    void changeArgument (int *x) //This had to be a pointer
    {
    *x = *x + 5;
    }
     
    int main()
     
    {
               int y = 4;
               changeArgument( &y ); // pass by reference, not by value.
               cout << y; 
               cout << endl;
               cin.get();
    }
    I changed 'using namepace std;' for good practice. This prevents future conflicting namespaces. Google 'Why using namespace std is bad' for more info. The way you had your function, you needed to use pointers to get your desired result. PM me if you need any further information.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by inu11byte View Post
    Code:
    void changeArgument (int *x) //This had to be a pointer
    {
    *x = *x + 5;
    }
     
    int main()
     
    {
               int y = 4;
               changeArgument( &y ); // pass by reference, not by value.
    Don't call this pass by reference, since pass by reference means something different in C++. What you are doing is passing a pointer to an int. A reference is not a pointer, a reference is a reference; a pointer is not a reference, a pointer is a pointer. They can be used in very similar ways, but they are not the same thing.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int changeArgument (int &x) // this parameter is a reference
    {
    	x = x + 5;
    	return x;
    }
     
    int main()
    {
    	int x = 4;
    	changeArgument(x); // x will be passed by reference
    	cout << x;
    	return 0;	
    }
    Notice that this accomplishes the same result as using a pointer. Do not confuse &, the reference modifier (above), with &, the address of operator (used to create a pointer, as on line 17 of post #8). They can be differentiated by the context.

    I changed 'using namepace std;' for good practice. This prevents future conflicting namespaces. Google 'Why using namespace std is bad' for more info.
    You're right, so please excuse my living dangerously in the example .
    Last edited by MK27; 03-19-2012 at 03:00 AM.
    inu11byte likes this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,433
    Quote Originally Posted by inu11byte
    I changed 'using namepace std;' for good practice. This prevents future conflicting namespaces.
    It reduces the chance of a name collision. If your namespaces conflict, not using any using declarations or using directives won't save you

    I don't really see the point of the using declarations though: std::cin is not used at all, std::endl is just used once, and is it so hard to write and read std::cout when it is used twice? You might as well have a using directive local to main.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    Quote Originally Posted by laserlight View Post
    It reduces the chance of a name collision. If your namespaces conflict, not using any using declarations or using directives won't save you

    I don't really see the point of the using declarations though: std::cin is not used at all, std::endl is just used once, and is it so hard to write and read std::cout when it is used twice? You might as well have a using directive local to main.
    Yeah, well I am a C programmer, not C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob + Functions == Problems
    By Inao in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2007, 04:02 PM
  2. Noob question?
    By wart101 in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 11-21-2006, 08:41 PM
  3. noob question
    By unclebob in forum C++ Programming
    Replies: 9
    Last Post: 09-24-2006, 08:48 AM
  4. Lesson 4: Functions noob question
    By NiVaG in forum C++ Programming
    Replies: 8
    Last Post: 09-24-2004, 05:12 PM
  5. Semi-noob Q: calling functions
    By sean... in forum C++ Programming
    Replies: 2
    Last Post: 07-06-2004, 04:43 PM

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