Interchanging variables between functions

R

Roberto Dias

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
 
T

Thomas Matthews

Roberto said:
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
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(unsigned int& num)
{
num += 5;
return;
}

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

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

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

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

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

return EXIT_SUCCESS;
}


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top