Thread: Newbie Here - Return? Parameters?

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    1

    Newbie Here - Return? Parameters?

    Hello,
    I am very new to programming and am currently taking a c++ class.
    I am a little confused as to what the parameters and return are.
    I am currently working on how to use functions outside of "main" and as well as outside of the file.

    So here it goes..

    Code:
    int myFunction(int whoAmI_1, int whoAmI_2)    //Am I declaring these
                                                                     //now? So they didnt  
                                                                     //exist till now?      
    {
        int whoAmI_1 = 2;
        int whoAmI_2 = 3;
        int sendBack;
        inMain += wh0AmI_1;   //how do I use this "inMain" from another
                                         //function
        sendBack = whoAmI_1 + whoAmI_2;
        return sendBack;
    
        //I know I am sending "sendBack" to the function it was called 
        //from right? If this is so than what are the parameters for at the
        //top? Are those vars I am getting from another function?
        // Also what if I want to send all of the variables back to main for 
        //further computation? can I return a few vars?
    }
    
    int main()
    {
    int inMain=4;
    cout << myFunction();    //This should print out 5 because      
                                      //"myFunction" returned "sendBack" which
                                      //is 4 right?
                                      //What about the parameters I put in when I 
                                      //created the function? do i need those here?
    return 0;
    }
    Does anyone know what I am doing wrong?

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    the parameters to your function are like local variables, except their values are set by the calling routine. so in your 'myFunction' you shouldn't redeclare the variables whoAmiI_1 and whoAmiI_2. they are declared in the parameter list.
    next, your call to myFunction should have values for those 2 parameters. so you would call myFunction like this:
    Code:
    cout << myFunction(2,3);
    and in myFunction, this call will set the parameters whoAmiI_1 and whoAmi2 to 2 and 3.
    but, you should not have even been able to compile this program without errors due to the several syntax problems. so how did you run the program? and 'cout' is a c++ thing, not C.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by JamesFlanigan View Post
    Hello,
    I am very new to programming and am currently taking a c++ class.
    Hello.Mind that there is a topic for C and one for C++ on this forum.So because your c++ class,you could at the future -for convenience-post there if you write C++ and here if you write C.

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I think that this should be moved to the C++ board.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Thread moved to C++ Programming forum.
    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

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Inline commentary:
    Code:
    int myFunction(int whoAmI_1, int whoAmI_2)    //Am I declaring these
                                                                     //now? So they didnt  
                                                                     //exist till now?
    You're saying that myFunction must be passed two integers. It doesn't matter what the variables are called in the original function; they can be constants like "5", or "x" or "someLongName" or even "whoAmI_1". The names are solely for the convenience of this function. Their values must be supplied by the calling function.
    Code:
    {
        int whoAmI_1 = 2;
        int whoAmI_2 = 3;
    Really bad idea to redeclare these variables again. Now you have two copies: the ones which were passed in as arguments, and the ones declared locally on the stack. The local variables take precedence, "shadowing" the other ones, because they're declared later. Still, a very bad idea. You might be interested in reading Functions and Character Arrays HELP!
    Code:
        int sendBack;
        inMain += wh0AmI_1;   //how do I use this "inMain" from another
                                         //function
    Okay, so you're setting up a variable to use as a return value. Keep in mind that variable names are mostly only relevant to the function they're inside. You can return the value of this variable inMain with "return inMain;" or you can return a value directly as in "return 2*5;". The calling function doesn't know, or care, how the return value was computed. It certainly doesn't know or care the name of the variable you used, if any.
    Code:
        sendBack = whoAmI_1 + whoAmI_2;
        return sendBack;
     
        //I know I am sending "sendBack" to the function it was called 
        //from right? If this is so than what are the parameters for at the
        //top? Are those vars I am getting from another function?
        // Also what if I want to send all of the variables back to main for 
        //further computation? can I return a few vars?
    You can only return one value. If you want to return multiple values you have to "pass by reference", essentially make one or more variables references or pointers. That's a bit more advanced but you can look it up if you're interested. https://www.google.com/search?q=pass...nt=iceweasel-a
    Code:
    }
     
    int main()
    {
    int inMain=4;
    Okay, now you're confusing yourself... the variable "inMain" that you've declared here will not be the same variable that occurs with the same name in other functions. You're just allocating space for an integer, and you happened to call it "inMain". The name doesn't matter to any other functions. Different functions in different scopes can name variables whatever they like. How else would programmers be able to write for loops and always use "i" or "x" as a name? Can you imagine making up a new, unique name every time you needed a loop?
    Code:
    cout << myFunction();    //This should print out 5 because      
                                      //"myFunction" returned "sendBack" which
                                      //is 4 right?
                                      //What about the parameters I put in when I 
                                      //created the function? do i need those here?
    return 0;
    }
    Yes. You need to pass parameters to functions that are expecting parameters.

    A few other quick examples to get you going.
    Code:
    void function(int oneArg) {}
    
    function(5);  // pass in a constant
    int x = 4;
    function(x);  // pass in a variable -- note that the name "x" doesn't mean anything to the called function
    Code:
    int add(int a, int b) {
        // this function takes two parameters. We're going to add them up and return them.
        // We could use an extra variable here, but it's not necessary.
        return a + b;
    }
    
    int x = add(1, 2);  // pass in constants
    int y = add(x, x);  // pass in variables, even the same variable. The value is just copied.
    int a = add(3, 4);  // I can call the variable storing the return value whatever I want. It won't interfere with parameters.
    Code:
    // (this function is called myswap because there's already a std::swap built-in to the standard library!)
    void myswap(int &x, int &y) {
        // this function is supposed to swap two variables, in a sense return two values.
        // Because the parameters are passed by reference, any changes you do here will be reflected in the calling function.
        int temp = x;
        x = y;
        y = temp;
        // now x and y have been changed, we can just let the function exit.
    }
    
    int x=1, y=2;
    myswap(x, y);
    // now x = 2 and y = 1
    
    // myswap(4, 5);  // can't pass constants where references are expected
    Hope you find some of that useful.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function with two parameters, not sure what to return
    By new2cstudent in forum C Programming
    Replies: 10
    Last Post: 10-03-2010, 07:54 AM
  2. Newbie function return question
    By faifas in forum C Programming
    Replies: 2
    Last Post: 06-29-2009, 10:19 AM
  3. Newbie question about pointers in function parameters.
    By sojurn in forum C++ Programming
    Replies: 14
    Last Post: 01-20-2007, 09:21 PM
  4. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  5. Episode II Return Of the newbie : Website IO Question
    By MagiZedd in forum Windows Programming
    Replies: 1
    Last Post: 10-18-2001, 08:58 PM

Tags for this Thread