forked from CPRF-Session2/Assignment5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment5.txt
More file actions
5 lines (4 loc) · 2.3 KB
/
Assignment5.txt
File metadata and controls
5 lines (4 loc) · 2.3 KB
1
2
3
4
1a) Passing by value makes a copy of the variable which it gives or "passes" to the function. Therefore, the value is the same, but the identity of the variable is different. Passing by value can be compared to an analogy where you want to share a web page with a friend and you give them a printed-out copy, or a disconnected copy of the original. You won't see any of the changes to the web page if they are made, but you can scribble or modify your copy without the changes showing up on the web page. If you decide to destroy your print-out, YOUR copy may be destroyed, but the original web page remains unaffected.
1b) Passing by reference can also be compared to the analogy where you want to share a web page with a friend, and you send them the URL.The web page that you and your friend are looking at would be identical, even if there are any changes made. If one of you happens to delete the URL, you are deleting your reference to that page, and the page itself would remain perfectly intact.
2) The function of the return statement in C is to end the execution of a function and to "return" control to the calling of the function, while also specifying a value to sent to the caller function. This value becomes the value of the function call expression.
3) The purpose of writing functions is to make things easier, save time, and generally allow the programmer to be more efficient. Since "www.cprogramming.com" defines functions as "blocks of code that perform a number of pre-defined commands to accomplish something productive." Since the calling of a function can be tied to a variable, a function can be called multiple times to perform the same task wirh a simple variable, versus continuously writing the code over and over again. For example, a function made to generate a random number between 1 - 10 would be immensely useful for the programmer who is working with a plethora of variables and needs to randomize each of them within this predefined range in order to accomplish a successful program. Instead of writing the code over and over every time they declare a variable that requires such a value, they could just write the variable which would call the function, making them immensely useful. They are also useful for organizational purposes, since they can be used to separate each task that needs to be ran in the program.