By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,352 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

Interchanging variables between functions

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Roberto Dias wrote:
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

Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.