New To Programming

This is a discussion on New To Programming within the C++ Programming forums, part of the General Programming Boards category; Hello my name is Javier. Ive been going at this topic and my teacher always said to call her but ...

  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
    22,303
    Show the code that is your best attempt.
    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

  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, 04: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, 11:32 AM

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