Function call

This is a discussion on Function call within the C++ Programming forums, part of the General Programming Boards category; Hi guys, would appreciate your help since Im beginner everything in this fantastic language seems wired for me. I got ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    7

    Function call

    Hi guys, would appreciate your help since Im beginner everything in this fantastic language seems wired for me. I got the follwing codes:

    1.

    //The function SumTo() takes an integer and prints out the sum of all integers upto that int.
    Code:
    int SumTo(int nValue)
    {
    	using namespace std;
    	cout<<"enter the number: ";
    	
    	cin>>nValue;
    	int nsum=0;
    	for(int iii=0;iii<=nValue;iii++)
    		nsum+=iii;
    	return nsum;
    }
    
    int main()
    {
    	using namespace std;
    	cout<<SumTo(int nValue)<<endl; //This way of calling the function SumTo doesnt 
    //work,why?
    	return 0;
    }

    2. If I on the otherhand declare my integer inside the SumTo() function then it works, as the code bellow.


    Code:
    int SumTo()
    {
    	using namespace std;
    	cout<<"enter the number: ";
    	int nValue;
    	cin>>nValue;
    	int nsum=0;
    	for(int iii=0;iii<=nValue;iii++)
    		nsum+=iii;
    	return nsum;
    }
    
    int main()
    {
    	using namespace std;
    	cout<<SumTo()<<endl;
    	return 0;
    }
    Would be thankful for some help, how can I modify the first one to work?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    The answer to your first question is that it is simply syntactically incorrect.

    As for your second question, perhaps this example would demonstrate the syntax:
    Code:
    #include <iostream>
    
    int SumTo(int value)
    {
        int sum = 0;
        for (int i = 0; i <= value; ++i)
            sum += i;
        return sum;
    }
    
    int main()
    {
        using namespace std;
        cout << "enter the number: ";
        int value;
        cin >> value;
        cout << SumTo(value) << endl;
        return 0;
    }
    Basically, I copied over most of your code (renaming the variables and formatting). What is different is that I declared a variable in the main function, and moved over the user input/output code to the main function. The SumTo function thus deals entirely with summation.
    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
    Sep 2010
    Posts
    7
    Hi, Thanks alot for the help, but Im still curious: would it still work if I leave also the reading part inside the function SumTo()?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    Yes, if you change
    int SumTo(int nValue)
    to
    int SumTo(int& nValue)
    The later basically tells the compiler to make "nValue" an alias of another variable. The problem is that all arguments gets copied, so your nValue will be local to the function. But if you make it an alias of another variable which you pass in, it will write to that variable instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    7

    Thumbs up

    Thank you very much both of you for the help!

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    87
    Laserlight you are very helpful.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    7
    Still curious! Does the character & in front of int, i.e. int& makes the variable availabe everywhere in the file its defined/declared?

    And what if I in the main function make following declarations:
    Code:
    int main()
    {
    ...
    ...
    int x;
    int y;
    int nValue;
    ...
    ....
    ....
    return 0;
    }
    Will x=y=nValue? becouse of the declaration form (int& nValue) ?

    Best regards!

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    No. All the & does is tell the compiler that this is real variable; it is an alias for another variable. And the variable it is an alias for is whatever you initialize it to. It behaves just the same as other variables. It can go out of scope, and will do so, just as a normal variable would and just when a normal variable would. Of course, the original variable is not destroyed when its alias is destroyed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM

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