Thread: Confused about writing functions

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    45

    Confused about writing functions

    Here is my code:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    string Print_Out();
    
    string Check();
    
    string secret;
    
    int main()
    {
    	Print_Out();
    	Check(int b);
    
    	return 0;
    }
    
    string Print_Out()
    {
    	int b;
    cout << "Test" << endl;
    b = 2;
    	return secret;
    }
    
    string Check(int b)
    {
    
    if (5>b)
    {
    	cout << "5 Is greater than 2" << endl;
    }
    else
    {
    	cout <<"5 is not greater than 2" << endl;
    }
    
    return 0;
    
    }
    What I want it to do is do the Print_Out function, thus printing out the Test on the screen as well as assigning b to 2. Then how would I call up the Check(int b) function to test if b is less than or greater than 5?

    I am very confused as to how to put these functions into the int main();

    When I run the program, it seems as though the Print_Out() works fine printing out Test on the screen but it does not output whether 5 is greater than or less than 2. Please help me with this. Thank you.
    Last edited by chickenlittle; 11-27-2010 at 06:50 PM.

  2. #2
    Registered User Fractur0x65's Avatar
    Join Date
    Nov 2010
    Location
    Eastern U.S.
    Posts
    10
    First off, the prototype of your Check function at the top, string Check(); should match the actual function below, string Check(int b). So that means you need to include the int b argument there too or that could cause problems.

    Second, the int b you're passing to you Check function isn't same as the one you are assigning to the number 2.

    Code:
    string Print_Out()
    {
    	int b;
    
    cout << "Test" << endl;
    
    b = 2;
    
    	return secret;
    }
    Because you declared int b inside the function, b exists only within the scope of that function, it cannot be used out side of it. So when the program goes back to main, then runs the check program, you're basically making a new int b with no assigned value and giving that to your Check function. If you want the variable to be available to all the functions in the program, you can instead put it up with your function prototypes before main, making it a global variable. Then you can just call Check(b) - check with b passed to it.

    One more thing, the return 0; line at the end of check is incorrect. The return type for check is set as string [ string Check(int b) ], that means the function isn't meant to return an int. What you return needs to match the return type you declared it with. I'm not sure if it came up for you, but in my compiler at least thing returned errors.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    Thank you so much for your helpful guidelines! Here is my working code now:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    string Print_Out();
    
    string Check(int b);
    
    string secret;
    
    int a, b;
    
    int main()
    {
    	Print_Out();
    	Check(b);
    
    	return 0;
    }
    
    string Print_Out()
    {
    cout << "Test" << endl;
    b = 2;
    	return secret;
    }
    
    string Check(int b)
    {
    
    if (5>b)
    {
    	cout << "5 Is greater than 2" << endl;
    }
    else
    {
    	cout <<"5 is not greater than 2" << endl;
    }
    
    return secret;
    
    }

    However I have another question. Is it really necessary to do Check(b); inside the function main?

    If I change that to say, Check(a); with the variable declared globally the answer would still come out the same. The same result comes out if I do Check(5); as well.

    Is it suppose to be like that or is there something I am doing wrong? I am using Microsoft Visual Studio 2010 by the way.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by chickenlittle View Post
    However I have another question. Is it really necessary to do Check(b); inside the function main?

    If I change that to say, Check(a); with the variable declared globally the answer would still come out the same. The same result comes out if I do Check(5); as well.

    Is it suppose to be like that or is there something I am doing wrong? I am using Microsoft Visual Studio 2010 by the way.
    Yes it is really necessary to pass an argument, because of the way that you wrote string Check(int b); if you didn't pass an integer, you would get an error saying something like "too few arguments for function Check". Making sure calls match their respective declarations is one of the important things a compiler does to make sure your syntax is valid and that in the actual program the correct function is executed.

    Your issue is made complicated by the use of global variables. You see, globals can be edited anywhere in the program. So even though you pass something that isn't b into Check, b can still be changed. If you used locals, b would look like it stays the same, due to scoping rules.

  5. #5
    Registered User Fractur0x65's Avatar
    Join Date
    Nov 2010
    Location
    Eastern U.S.
    Posts
    10
    Sorry about that, should have pointed out that the only reason you need to pass b, or anything, to Check when -b is global- is because Check is wrote as talking an argument. Having check still take an argument when the only variable it's using is global won't really do any good in this case.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    Thank you so much for explaining this to me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Optimize file writing
    By Opariti in forum Windows Programming
    Replies: 7
    Last Post: 10-23-2008, 01:32 PM
  2. Static functions.... why?
    By patricio2626 in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2007, 08:06 PM
  3. Static member functions more efficient?
    By drrngrvy in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2006, 07:07 AM
  4. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM
  5. Variables do not equal functions!!!
    By me@burk. in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 06:24 AM