Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

into the variable x_address in the main function code with the statement X_address = &x;, then the following two lines are absolutely equivalent in the

image text in transcribed
into the variable x_address in the main function code with the statement X_address = &x;, then the following two lines are absolutely equivalent in the main function and you could use either: x = x + 4; and *X_address = *X_address + 4; k. Why do we go through all this rigamarole with addresses when we want to change a value in a function? Because in C (and many other computer languages) argument values are only ever **passed into** (copied into) function parameters. Nothing (!!) is EVER "passed back". So if you just copied in x to y, as in the earlier steps of this lab, you could change y all you wanted in the function... and x in the main function would still be the same old x it always was. You would only be changing the COPY of x in 13 (y would be the copy as you used it in the earlier steps 6 or 7). So, if we want a change to be made to a value in the main function, we pass the address of WHERE we want the change made (in the main function) to the helper function (such as 13); the helper function accesses that address/location in the main function's memory, which it does have (of course it does, we passed that address to the function) and the called function (13) makes the change there in the calling main function). Note also that using a return statement to bring something "back" is a completely separate issue, and it can only return a single value - whereas in a great many cases far more than one item from a function is wanted. In those cases, such as you did in this step and will also do in the step below, you have to pass an address and then use dereferencing in the called function, 1. Now that you've seen how to pass in an address of a variable to a function and in that function use that address to make a change to the variable, back in the calling (main in this case) function, you can comment all the preceding function calling and write and call another void function f4 which has three parameters. The first, an integer variable named y, will be passed the initial number x from the main function, as seen above. The other two parameters will receive addresses from the call. The second parameter, named addri, will be used to receive the address of a variable named num_of_tens which you will declare in the main function. The third parameter, named addr2, will be used to receive the address of a variable named remainder also declared in the main function. In the main function, the value will first be set for x, as above, and then f4 is to be called, passing it the value of x and the value of the address of num_of_tens (written as 8.num_of_tens) and the value of the address of remainder (written as Bremainder). Neither num_of tens or remainder have had anything put into them at the point where f4 is called. In fact, nothing in the main function will set them. f4 will, using the code you wrote earlier, determine the number of 10s in y (copied from x) and the remainder when y is divided by 10. By dereferencing addri and addr2, f4 will then set these calculated values into the variables num_of_tens and remainder (they are both in the main function's memory area or as we often term in they are in the main function") ... and then f4 will quit. Nothing is returned, nothing is "sent back". You passed in two addresses, made changes at those two addresses, and then the function is finished and control passes back to the main function. Immediately after the call to f4, in the main function write code so that it will print out the values of num_of_tens and remainder, properly labeled. Try this with a few different initial numbers. m. You final program should look like this. The user enters a number between 10 and 99 (validated), then the program calls the function 13 that gives both the quotient and the remainder of a division by 10. The two values are printed out (not in the f3 function, but in the main program)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases On The Web Designing And Programming For Network Access

Authors: Patricia Ju

1st Edition

1558515100, 978-1558515109

More Books

Students also viewed these Databases questions

Question

4. Write the quantity equation and explain it.

Answered: 1 week ago