-
Notifications
You must be signed in to change notification settings - Fork 13
passing parameters by reference #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please format tables using markdown instead of directly embedding HTML. Using markdown has several advantages: 1) it is more readable; 2) it is easier to edit; and 3) it allows the tools processing markdown to understand the table structure, which allows the document to be built in multiple formats. For an example of a table formatted in markdown, see the Overview section in:
https://raw.githubusercontent.com/cplusplus/SG20/ba572fe6aa22fbf1f3d2c53704f0ee9e98360d4a/sources/modules/compile-time-programming/requires-expressions.md
I wont dispute the merits of using markdown for tables, but this is the format prescribed by the skeleton file. Would you be willing to present a PR to convert all current tables to markdown? Or at least to open an issue suggesting such? I'd prefer to not deviate from the format used in the rest of the topic files in just this one. |
I've already done this in the PR that is pending (PR #70) for Florian to merge, which contains all of the changes from my forked SG20 repository. It would probably be a good idea to make a decision on PR #70 first. I prepared it in consultation with Florian, so it should be okay to merge, at least in theory. |
|
||
Explain what a reference type is and how it constrasts with a value type. | ||
|
||
## Foundational: Using reference types to avoid copies |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is entirely the wrong way of prhasing it. Reference types make the function parameter into an alias of the argument at the calling site. That is the basic thought. You need to state that before any considerations of performance optimization.
Now you can use call-by-reference for two reasons: 1. you want to alter the argument at the calling site 2. you want to cut down on copying. That's the second level.
And then there is the consideration that if you 1. not alter the argument and 2. still pass by reference, then you should add const
.
But I think material should be presented in that order or increasing refinement of concepts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For foundational knowledge, and given how early this bit creeps into teaching, it seems like a lot to expect of the student to understand aliases and the differences between "arguments at the calling site" vs the "function parameter". As an iterative process, I would expect a student to first become familiar with the syntax and a motivating reason to use it, and to later understand the deeper meaning of it, once they have the expanded knowledge of the language to support it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I think I see your point. But "Avoid copies" happens because the formal parameter becomes an alias of / reference to the argument in the calling code. The motivation and the workings are so close together that I see no reason not to teach/mention immediately the concept of reference.
Pass by value: formal parameter become local variable; compare to making a new variable in the main program.
Pass by ref: format parameter becomes reference: compare to making a reference in the main program.
* The lifetime of a constant reference cannot be expected to extend beyond the lifetime of the function, so the reference should not be saved off for future use. | ||
* Taking a reference is not always a time or space savings. Modern machines may use 8-bytes to reference a 4-byte integer, for instance. | ||
|
||
## Main: Using references to modify external data |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like the phrase "external data". How about "argument at the calling site" or something like that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that "external data" is not ideal. "argument at the calling site" also leaves a bit to be desired, though. "Modifying an argument" sounds more like choosing a different argument, not changing the value of the thing represented by the argument. As a quick-check, it took me a minute to figure out what you meant. Is there another alternative? I was looking for wording that was relative to the function being created, not so much the user of the function. "modify memory beyond the function" or "modify memory known at the call-site" or something like that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"modify the variable that is used as actual argument"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestions as to how to sequence/phrase things.
Issue #39