trying to wrap my head around functions

This is a discussion on trying to wrap my head around functions within the C++ Programming forums, part of the General Programming Boards category; So I know a bit about functions, yet for some reason I can't get my head to wrap around them. ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    35

    trying to wrap my head around functions

    So I know a bit about functions, yet for some reason I can't get my head to wrap around them. I understand they're used to split your program up to make it easier to deal with it, however I'm not completely understanding their workings. I have a runtime error with a certain function, not sure why. I wrote some code to experiment different ways of passing a function and what not.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void function1(void);
    void function2(void);
    int returnfunc(void);
    void str(string prompt);
    string strFunc(string); // Problem function
    
    
    
    int main()
    {
        int num1;
        int num2;
        string strSent;
    
        function1(); // Call function1 in main
    
        function2(); // call function2 in main
    
        num2 = returnfunc(); // num2 will grab the value that returnfunc returns within its function
    
        cout << "The number you passed to main was " << num2 << endl << endl; // shows that nb1 successfuly returns a value and assigns it to the variable 'num2'
    
        str("The prompt is within the () of this function"); // the message determined from prompt is defined here
        cout << endl << endl;
    
        cout << "Enter a sentence.\n";
        cin.get();
        getline(cin, strSent);
    
        // Problem here
        strFunc(strSent);
    
    
    
    
        return 0;
    }
    
    void function1()
    {
        cout << "Calling this function in main() will display this stream." << endl << endl;
    }
    
    void function2()
    {
        int n1;
    
        cout << "Enter a number -> ";
        cin >> n1;
    
        cout << "The number you entered was \"" << n1 << "\"" << endl << endl;
    }
    
    int returnfunc()
    {
        int nb1;
    
        cout << "Enter a number to pass to main ";
        cin >> nb1;
    
        return nb1;
    }
    
    void str(string prompt)
    {
        cout << prompt; // will display whatever prompt you code in the main
    }
    
    string strFunc(string strsnt) // problem here
    {
        cout << strsnt;
    }
    Perhaps someone can elaborate more on them? Maybe some more practical approaches?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your strFunc function is declared as "function returning string", yet you do not. You must do so, and your compiler has been jumping up and down frantically trying to tell you so this whole time (so in future you should listen to it).

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    35
    Yeah I realized that not even 30 seconds ago of making this reply. So this is how I understand functions...

    int functionName(int, double, char, string)

    Where int is gonna be the returned data type, functionName is the name of the function and the types within the () are the variables that will be used within the function, and the arguments needed when calling the function?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That is all correct.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    35
    So if I don't want to use arguments when I call the function, i must leave () for (void) and declare the arguments within the function... all about scoping. the () is the parameter list correct?

    edit: Through further experimenting, I found something else which causes me some confusion.

    Code:
    int numInFunc(int somenum)
    {
        somenum = 1234;
    
        return somenum;
    
    ... in main()
    ...
        numInFunc(num1);
    
        cout << "The number from the function numInFunc is: " << num1 << endl;
    ...
    }
    The main code doesn't give me the correct number. However if I rewrite it like this and put the function in the output stream
    Code:
        cout << "The number from the function numInFunc is: " << numInFunc(num1) << endl;
    It gives me the correct number. The variable num1 does not hold the value when I put it inside the function argument list to take it, then output the value the variable holds later?
    Last edited by Darkroman; 09-03-2011 at 07:01 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Quote Originally Posted by Darkroman
    The variable num1 does not hold the value when I put it inside the function argument list to take it, then output the value the variable holds later?
    When you make that function call, somenum is a copy of num1, so assigning to somenum has no effect on num1. If you had made somenum a reference to int instead, then somenum would be an alias of num1, hence assigning to somenum would have changed num1.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wrap around in array
    By lamko in forum C Programming
    Replies: 22
    Last Post: 08-17-2011, 12:24 PM
  2. Simple error around which I can't wrap my head.
    By KAUFMANN in forum Linux Programming
    Replies: 3
    Last Post: 06-09-2011, 11:00 PM
  3. It's a wrap!
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 06-10-2007, 02:00 PM
  4. Word Wrap
    By Fireblade2006 in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2006, 02:09 PM
  5. Word Wrap
    By sethjackson in forum Windows Programming
    Replies: 4
    Last Post: 09-21-2005, 04:35 PM

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