Using Pointers to Modify a Variable Passed into a Function - C++ Function

C++ examples for Function:Function Parameter

Description

Using Pointers to Modify a Variable Passed into a Function

Demo Code

#include <iostream> 

using namespace std; 

void f(int *myparam) 
{ 
    (*myparam) += 10; /*w ww. ja v a  2 s  . c  o  m*/
    cout << "Inside the function:" << endl; 
    cout << (*myparam) << endl; 
} 

int main() 
{ 
  int mynumber = 30; 
  cout << "Before the function:" << endl; 
  cout << mynumber << endl; 

  f(&mynumber); 
  cout << "After the function:" << endl; 
  cout << mynumber << endl; 

  return 0; 
}

Result


Related Tutorials