Thread: Returning a value from a function

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    18

    Returning a value from a function

    hi there, i tried to return a value from a function and the value returned will be used another function. here is my code
    Code:
    void processL()
    {
    	.....
    	.....
    	if(nameLine==Name && passLine==Password)
    	{
    		valid = false;
    		return valid
    	}
    }
    Code:
    void processA()
    {
    	.....
    	.....
    	check = customerLogin();
            if(check == true)
    	  {
    	      cout<<"success";
    	  }
    	else
    	   cout<<"Failed";
              
    }
    i meant to use the value returned on processA(), but i'm unable to do it. can u give some help or advice ??

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    err... what value are you returning, and why do you declare the return value as void if you want to return something?
    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
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Code:
    void processL()
    {
    	.....
    	.....
    	if(nameLine==Name && passLine==Password)
    	{
    		valid = false;
    		return valid
    	}
    // and what value are you returning if you get here?
    }
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    A void function cannot return a value...it has to be an int function or float etc. Do you not also need to pass parameters?

    Edited out: Why, if you are using the value returned from processA in processL is there no value returned in A?

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Code:
    int processL(int valid)
    {
    	int completeandutterloser = -1;
            .....
    	.....
    	if(nameLine==Name && passLine==Password)
    	{
    		valid = false;
    		return valid
    	}
            return completeandutterloser; 
    }
    Code:
    void processA(int valid)
    {
    	.....
    	.....
    	check = customerLogin();
            if(check == true)
    	  {
    	      cout<<"success";
    	  }
    	else
    	   cout<<"Failed";
              
    }

    I could be wrong as that might not do what you want it to (feedback if I am right please) If valid is not a parameter of processL then you have to define it in processL.....I think!

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You would want to return a bool, not int, since the code uses true and false. And there is no need for a parameter here.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Woops

    Apart from that was I right?


    Edit: I had a dumb and dumberndumberer moment there for some reason I was thinking false was a variable and valid was now equal to that!

    /me shoots homself in the foot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in returning value from the dll exported function
    By dattaforit in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2006, 04:30 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. returning pointers from a function
    By curlious in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2003, 11:37 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM