Thread: New To Programming

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

    New To Programming

    Hello my name is Javier. Ive been going at this topic and my teacher always said to call her but she isn't responding so im asking you all. Im a beginner on programming. As of now I have an assignment that im not asking to help me cheat on or anything. Just a simple question, how do I make a function return integer values back to the main() function. I have tried much and it isn't working. I called the function and I thought the problem was the fact that I first called it void so nothing would return. I changed it to int and still nothing works in this situation. Can anyone help me. First post so any critisizm would benefit as well.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Show the code that is your best attempt.
    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
    Feb 2012
    Location
    Ottawa
    Posts
    6
    Perhaps you are trying to pass the value back through the parameter list. By default, C and C++ always pass the function parameters by value, meaning that if they are modified within the function, those modifications are local. For instance, if you have the function foo:
    Code:
    void foo(int arg1, int arg2) {
      arg1=2;
      ...
    }
    And you call it from the main routine like so:
    Code:
    int a=3;
    foo(a, 5);
    printf("%d\n", a);
    you will get 3, not 2, as the result. There are two ways to pass by reference, one of which is a C++ extension. The old, C way of doing things, is:
    Code:
    void foo(int *arg1, int arg2) ...
    Now you have to call it like this:
    Code:
    int a=3;
    foo (&a, 5);
    The newer, C++ way is:
    Code:
    void foo(int &arg1, int arg2) ...
    in which case you can continue to call it in the usual way as in the very first example.

    Hope this helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-11-2011, 04:25 PM
  2. Replies: 1
    Last Post: 08-19-2007, 03:55 AM
  3. small programming job VCPP / Object Oriented Programming
    By calgonite in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 01-04-2006, 11:48 PM
  4. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM