1

Can anyone give me a hint?

I'm doing Pset5, and I can't seem to come up with an answer for "Why do you think we pass before and after by reference (instead of by value) to calculate, even though we’re not changing their contents?" in questions.txt (question 3.).

The calculate function is always used as it's return value is assigned to a variable. So there should be no problem by passing it by value. So then why pass by reference?

Edit: Wait. Is it only because it takes less steps? Meaning you don't have to pass a copy but directly doing the calculations via the adresses?

1

I would suggest reading this article in addition to all the shorts that CS50 has on pointers: http://cslibrary.stanford.edu/102/PointersAndMemory.pdf

In particular, section 3 walks through the pros/cons of passing by value and passing by reference. It concludes with a clear summary that should help you answer this question.

The more you read that can help clarify pointers and their syntax, the better. The sample code in the article along with the diagrams of memory and explanations of the referencing and dereferencing operators really clarified things for me. It's worth going through slowly and methodically.

1
  • Okay, read section 3. What I got from it was that passing by reference saves a bit of memory and time. But the speller.c the implementation would work without pointers too (because it explicitly returns a value and is not void), just would not be the most efficient. Correct?
    – Bubi
    Feb 14 '16 at 10:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .