Interchanging variables between functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roberto Dias

    Interchanging variables between functions

    What about to interchange variables between functions? Which are the
    best way to do this? I know that this is a fundamental question, but I
    C++ fundamental student yet. I just started few day ago on studying
    about functions and I'm so excited to implement some ideias about text
    files processing.

    thanks,

    Roberto Dias
  • Thomas Matthews

    #2
    Re: Interchanging variables between functions

    Roberto Dias wrote:
    [color=blue]
    > What about to interchange variables between functions? Which are the
    > best way to do this? I know that this is a fundamental question, but I
    > C++ fundamental student yet. I just started few day ago on studying
    > about functions and I'm so excited to implement some ideias about text
    > files processing.
    >
    > thanks,
    >
    > Roberto Dias[/color]
    There are two methods: Pass pointers to the variables or
    have the function require references.

    #include <iostream>
    #include <cstdlib>
    using namespace std;

    void Add_Five(unsign ed int& num)
    {
    num += 5;
    return;
    }

    void Add_Ten(unsigne d int * p_num)
    {
    *p_num += 10;
    return;
    }

    int main(void)
    {
    unsigned int orig_num = 2;

    cout << "Original number: " << orig_num << endl;

    Add_Five(orig_n um);
    cout << "After call to Add_Five: "
    << orig_num << endl;

    Add_Ten(&orig_n um);
    cout << "After call to Add_Ten: "
    << orig_num << endl;

    return EXIT_SUCCESS;
    }


    --
    Thomas Matthews

    C++ newsgroup welcome message:

    C++ Faq: http://www.parashift.com/c++-faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.l earn.c-c++ faq:

    Other sites:
    http://www.josuttis.com -- C++ STL Library book

    Comment

    Working...