Passing data by value between C and C++

In general, value parameters are passed and received by C++ in the same manner as under C; a non-pointer or reference variable is passed in the parameter list. Any change that happens to a value parameter in the called function does not affect the variable in the caller, as in the following example, where an integer is passed by value from C++ to C:

Sample C++ usage C subroutine
#include <stdio.h>
extern "C" int cfunc(int);

main() {
  int result, y;

  y=5;
  result=cfunc(y);  /* by value */

  if (y==5 && result==6)
    printf("It worked!\n");
}
#include <stdio.h>

cfunc(int newval)
{
  ++newval;
  return newval;
}

Similarly, to pass an int by value from C to C++:

Sample C usage C++ subroutine
#include <stdio.h>

int cppfunc(int);
main() {
int result, y;

y=5;
result=cppfunc(y);  /* by value */

if (y==5 && result==6)
printf("It worked!\n");
}
#include <stdio.h>

extern "C" {
int cppfunc(int);
}

int cppfunc(int newval)
{
++newval;
return newval;
}