functions in c call by value and call by reference

0 votes

The code below shows how to call a function in both methods. 

Please explain the main difference or meaning of call by value and call by reference. 

1. Make a value-based call. 

2. Make a reference call. 

The call by value method is demonstrated in the following code.

In the comments, I expressed my reservations.

#include<iostream>
int main(){
void change(int);//why function prototype is before function definition and what is 
int orig=10;//meaning of argument int, it did not defined any variable of type int
cout<<"The original value is: "<<orig<<"\n";
change(orig);//what is the meaning of this piece of code
cout<<"Value after change() is over:"<<orig<<"\n";
return 0;
};
void change(int orig){
orig=20;
cout<<"Value of orig in function change() is:"<<orig<<"\n";
return;
}
According to the book, function definition should come before function prototype.
Jun 2, 2022 in C++ by Nicholas
• 7,760 points
634 views

1 answer to this question.

0 votes

Calling a function by value copies the argument and stores it in a local variable for use by the function, so the argument is unaffected if the value of the local variable changes. 

The argument is passed to the function as a reference rather than a copy, so if the function changes the value of the argument, the argument is also changed.


 

The void change(int); function prototype informs the compiler that there is a function named change that takes a single int argument and returns void (i.e. nothing). 

Because there is no & with the argument, it is called by value. 

You have the line change(orig); later in your code, which actually calls the function with.

Take a look at the output of this program:

#include<iostream>

using namespace std;

int main(){
  void change(int);
  void change2(int&);
  int x = 10;
  cout<<"The original value of x is: "<< x <<"\n";
  change(x); // call change(), which uses call by value
  cout<<"Value of x after change() is over: "<< x <<"\n";
  change2(x); // call change2(), which uses call by reference
  cout<<"Value of x after change2() is over: "<< x <<"\n";
  return 0;
};

void change(int orig){
    cout<<"Value of orig in function change() at beginning is: "<<orig<<"\n";
    orig=20;
    cout<<"Value of orig in function change() at end is: "<<orig<<"\n";
  return;
}

void change2(int &orig){
    cout<<"Value of orig in function change2() at beginning is: "<<orig<<"\n";
    orig=20;
    cout<<"Value of orig in function change2() at end is: "<<orig<<"\n";
  return;
}

I've changed int orig in main() to int x to hopefully avoid name confusion, and I've added change2() which uses call by reference.

answered Jun 7, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

functions in c++ call by value and call by reference

The code below shows how to call a function in both methods.  Please explain the major differences or meanings of call by value and call by reference to me.  1.Make a value-based call.  2.Call based on a reference.  The call by value method is demonstrated in the following code. In a comment, I expressed my reservations. #include<iostream> int main(){ void change(int);//why function prototype is before ...READ MORE

Jun 6, 2022 in C++ by Nicholas
• 7,760 points
505 views
0 votes
0 answers

Use of min and max functions in C++

Are std::min and std::max better than fmin ...READ MORE

Jun 2, 2022 in C++ by Nicholas
• 7,760 points
595 views
0 votes
1 answer

Function default argument value depending on argument name in C++ [duplicate]

When the function is called with no argument for the corresponding parameter, the default argument is evaluated.  In a default argument, a parameter must not appear as a potentially evaluated expression.  A function's parameters declared before a default argument are in scope and can obscure the namespace and class member name. It provides the following example: int h(int a, ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
606 views
0 votes
1 answer

Are virtual functions the only way to achieve Runtime Polymorphism in C++?

fprintf is a polymorphism function in the C programming language. It can print to a file, stdout, a printer, a socket, or whatever else the system can represent as a stream if you supply it different handles. FILE* file = fopen("output.txt", "w"); ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
488 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,020 points
701 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,020 points
2,422 views
0 votes
1 answer

How to pass large records to map/reduce tasks?

Hadoop is not designed for records about ...READ MORE

answered Sep 25, 2018 in Big Data Hadoop by Frankie
• 9,830 points
1,461 views
0 votes
1 answer

Invalid method parameters for eth_sendTransaction

params needs to be an array, try {"jsonrpc":"2.0","method":"eth_se ...READ MORE

answered Sep 28, 2018 in Blockchain by digger
• 26,740 points
1,878 views
0 votes
1 answer

Use of min and max functions in C++

The functions fmin and fmax are designed ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
11,337 views
0 votes
1 answer

Cases of static and dynamic binding in C++

When an object's static type is used to associate it with a member function, this is known as static binding (understand the type of its class). When a pointer or reference is associated with a member function based on the dynamic type of the object, this is known as dynamic binding (understand the instance of the variable at runtime). Before continuing, keep in mind that dynamic binding only works with pointers, references, and virtual functions for the base class. Because everything needed to call the function is known at compile time, the first call is a static binding (also known as early binding). Derived1 d1(1, 10); d1.display_data(); You already know that the d1 instance is a Derived1 automatic variable, and that it will call the Derived1::display data method (). The first condition is incorrect: d1 is neither a pointer nor a reference. The second condition isn't acceptable:  There is no virtual Derived1::display data. The second call is for ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
698 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP