Thread: Function call

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    199
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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, 03: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, 07: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