Is “C” Pass by Value or Pass by Reference?

This was the question one of our professors asked back in the engineering days. Easy enough, as it seemed, we all answered: “both”. The professor laughed and said “Think about it, will ask again in the next class”.

Apparently, there was nothing to think about it. Partially because everybody felt there was nothing to think about it – call by value is achieved by passing normal variables and call by reference is achieved by passing pointer to the variables. We were all determined to answer the same in the next class.

The professor came, asked the question, heard the answer and said, “No! you are wrong. C is strictly pass by value. It does NOT support pass by reference”. Now, when this comes from a professor who did his PhD for a duration of 7 years just for the kick of it, we all were sure that there is something wrong with our way of seeing things. Years later, now we see this happening with people surrounding us. When I ask people in interviews the same question, they remind me. However, when the professor explained it to us, we felt convinced of his argument. Now, when I explain things to interviewees, they don’t get convinced of the same.

If a language is pass by reference, then there are no different ways of passing the argument – you pass an argument and a reference is sent to the function – there are no syntactical differences between passing by value and passing by reference. However, in C, when the intent is to pass by reference, then we change the syntax a bit – that is we dont pass the variable anymore, but pass the address of it. Technically speaking, the address is still _copied_ to the parameter of the function and we use that _copied_ address to dereference it and get the value stored at the address. So, pointers is a tool to simulate/emulate call by reference, but the language as such does not _naturally_ support call by reference. So, C is strictly call by value and not by reference.

If you appreciate the rigor in this argument, then, welcome to the club. 😉

I got reminded of this when I was reading this post by Jon Udell where he raised a subtle point: “When you pass information, pass a reference to it, rather than sending a copy of it, it avoids duplication and the information remains current at all end points”. He also says that apart from the usual technical advantages of maintaining a single copy of information, there is a social advantage – that references (citations) can be tracked and one can connect with all those who share the references. Well, this is not a new thing that he is saying – WWW says the same: put your stuff on the Internet, cite references to it, make it a information highway.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s