Doubt in function parameters

Here we are writing :

void insertAtHead(node* head, int d)

so here if we are writing node* head that means head is a pointer of type node* which will point to the head. So it is already storing the address of head then why we are saying that it is pass by value and not pass by reference.

And for passing it by reference we write:
void insertAtHead(node* &head, int d)

Why ?

hello @yashsharma4304

first see this ->

void fun(int x){//x will have same value as y but both x and y are different variable right?
something
}
int y=5;
fun(y); //

...................................................... 
void fun(int &x){//x will have same value as y and both x and y are same ,changes in one will  get reflected  other in other.
something
}
int y=5;
fun(y); 

same is the case with pointer
when we are passing by value their value (i,e address) will be same , but when we are passing by refrence both value (ie address) and variable will be same , change in one will get reflected in other.

run this code and see the differnce->

1 Like

The way in which you have explained was great :slightly_smiling_face:. Thank you

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.