60 likes | 180 Vues
This guide explores key concepts in C++ related to function argument passing, specifically call by value, call by reference, and call by pointer. It demonstrates how these methods influence the way variables are accessed and modified in memory. Through examples, we will illustrate how data is passed to functions in C++, highlighting the differences between the methods using strings and integers. This comprehensive overview will improve your understanding of memory management and variable references in C++. Perfect for beginners looking to deepen their programming skills.
E N D
float x = 10.7; float& rx = x; cout<<x<< endl; //output: 10.7 cout<<&x<<endl; //address of x in memory cout<<rx<< endl; // output: 10.7 cout<<&rx<< endl; //address of in memory = &x --rx; // equivalent to --x;
inti,*j; i=5; cout<<“------------------------------" <<endl; cout<<i<<endl; //output: 5 cout<<&i<<endl; //output: i address in memory j=&i; cout<<j<<endl; //output: i address in memory cout<<“---------------------------------" <<endl;
#include <iostream> using namespace std; int count(int x); int main() { int x=3; cout<<"value of count(x)= "; cout<<count(x)<<endl; return 0; } int count (int x) { if (x==0) return 0; else { cout<<x; return count(x-1); } }
#include <iostream> using namespace std; int count(int x); int main() { int x=3; cout<<"value of count(x)= "; cout<<count(x)<<endl; return 0; } int count (int x) { int y; if (x==0) return 0; else { y = count(x-1); cout<<x; return y; } }
#include <iostream> #include <string> using namespace std; void call_by_v(string); //call by value void call_by_rr(string &); //call by reference using reference void call_by_rp(string *); //call by reference using pointer int main() { string s = "Welcome"; call_by_v(s); cout<<s<<endl; call_by_rr(s); cout<<s<<endl; call_by_rp(&s); cout<<s<<endl; return 0; } void call_by_v(string a) { a=" s will not changed"; } void call_by_rr(string & a) { a="s will change using reference"; } void call_by_rp(string *a) { *a="s will change using pointers"; }