Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Sprint * 100%-+ 10:02 AM a instructure-uploads.s3.amazonaws.com 1. Call-By-Reference Functions Sometimes we may want to retum more than one value from a function The ?@turn

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Sprint * 100%-+ 10:02 AM a instructure-uploads.s3.amazonaws.com 1. Call-By-Reference Functions Sometimes we may want to retum more than one value from a function The ?@turn statement can only be used to return one value. Istead, we will use the call-by-reference mechanism to update the arguments that are' passed to function. Following is an example in which we have used this method toreturn a value to main without using the Eeturn statement. A call-by-reference parameter is marked by the so that the compler will distinguish it from other parameters. Suppose, you wanted to ca??? function that would ask users to arput the floating-point numbers. a. b, and G. You could have called a function at get a bcla,b,) Now, the declaration of this function (using call-by-reference) will look lsike this: void get a bc(floats a, floats b, float&c) is worth noting that one may think that the use of 4 in all function calls sounds like a good idea. The argument one may make is that "it is safe to use it all the time, so when the value needs to get updated, it is already there. However, passing a variable using call-by reference may resut in unwanted and undesirable changes in the value, which may cause the program to produce incorrect results. As a rule, you need to keep all parameters as cal-by-value and only change those to cal by-reference that you need to get updated Let's look at a program and see how this works In the following example, the get input function obtains two values from the user, then returms for bring) them to the main function. In a sense, get input "returms" two values. This cannot be done with the return statement because the return statement retures exactly one value. If a function must produce more than one output value, then we must use call-by-reference parameters (one for each output value) include diostream uning namespace std Declaration of function that reads the values for i andj nt maing nt & eet inpul,i cout ex"About to call function process:i eciee"-ecje end /Call function process to increase i by 20 and decrease j by 20 process 20 return 0 n 30i ou e end Sprint * 100%-+ 10:02 AM a instructure-uploads.s3.amazonaws.com 1. Call-By-Reference Functions Sometimes we may want to retum more than one value from a function The ?@turn statement can only be used to return one value. Istead, we will use the call-by-reference mechanism to update the arguments that are' passed to function. Following is an example in which we have used this method toreturn a value to main without using the Eeturn statement. A call-by-reference parameter is marked by the so that the compler will distinguish it from other parameters. Suppose, you wanted to ca??? function that would ask users to arput the floating-point numbers. a. b, and G. You could have called a function at get a bcla,b,) Now, the declaration of this function (using call-by-reference) will look lsike this: void get a bc(floats a, floats b, float&c) is worth noting that one may think that the use of 4 in all function calls sounds like a good idea. The argument one may make is that "it is safe to use it all the time, so when the value needs to get updated, it is already there. However, passing a variable using call-by reference may resut in unwanted and undesirable changes in the value, which may cause the program to produce incorrect results. As a rule, you need to keep all parameters as cal-by-value and only change those to cal by-reference that you need to get updated Let's look at a program and see how this works In the following example, the get input function obtains two values from the user, then returms for bring) them to the main function. In a sense, get input "returms" two values. This cannot be done with the return statement because the return statement retures exactly one value. If a function must produce more than one output value, then we must use call-by-reference parameters (one for each output value) include diostream uning namespace std Declaration of function that reads the values for i andj nt maing nt & eet inpul,i cout ex"About to call function process:i eciee"-ecje end /Call function process to increase i by 20 and decrease j by 20 process 20 return 0 n 30i ou e end

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

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

12th edition

133544613, 978-0133544619

More Books

Students also viewed these Databases questions

Question

1. Write down two or three of your greatest strengths.

Answered: 1 week ago