6.8: Returning a Value from a Function

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Write the definition of a function add, which receives two integer parameters and returns their sum .

int add (int x, int y) { return x+y;}

Write a statement that declares a prototype for a function twice, which has an int parameter and returns an int .

int twice (int);

toThePowerOf is a function that accepts two int parameters and returns the value of the first parameter raised to the power of the second. An int variable cubeSide has already been declared and initialized . Another int variable , cubeVolume, has already been declared . Write a statement that calls toThePowerOf to compute the value of cubeSide raised to the power of 3, and store this value in cubeVolume.

cubeVolume = toThePowerOf(cubeSide, 3);

Write the definition of a function powerTo, which receives two parameters . The first is a double and the second is an int . The function returns a double . If the second parameter is negative, the function returns 0. Otherwise, it returns the value of the first parameter raised to the power of the second.

double powerTo (double x, int n) { double prod = 1.0; if (n < 0) return 0.0; while (n > 0) {prod *= x; --n;} return prod; }

Write a statement that declares a prototype for a function powerTo, which has two parameters . The first is a double and the second is an int . The function returns a double .

double powerTo (double, int);

Given the integer variables x, y, and z, write a fragment of code that assigns the smallest of x, y, and z to another integer variable min. Assume that all the variables have already been declared and that x, y, and z have been assigned values ).

if(x < y){ if(x<z){ min = x; }else{ min = z; } }else{ if(y<z){ min = y; }else{ min = z; } }

Write a statement that declares a prototype for a function add, which has two int parameters and returns an int .

int add (int, int );

Write the definition of a function half, which receives an integer parameter and returns an integer that is half the value of the parameter . (Use integer division!) So if the parameter 's value is 7, the function returns the value 3. If the parameter 's value happens to be 44, the functions returns the value 22.

int half(int value) {return value/2;}

Write the definition of a function max that has three int parameters and returns the largest.

int max(int a, int b, int c){ int max = a; if (b>a) max = b; if (c>max) max = c; return max; }

Write the definition of a function min that has two int parameters and returns the smaller.

int min (int a, int b){ return ((a<b) ? a : b); }

Write the definition of a function oneMore, which receives an integer parameter and returns an integer that is one more than the value of the parameter . So if the parameter 's value is 7, the function returns the value 8. If the parameter 's value happens to be 44, the functions returns the value 45.

int oneMore (int amount) {return amount +1; }

Write the definition of a function signOf, that receives an integer parameter and returns a -1 if the parameter is negative, returns 0 if the parameter is 0 and returns 1 if the parameter is positive. So, if the parameter 's value is 7 or 803 or 141 the function returns 1. But if the parameter 's value is -22 or -57, the function returns -1. And if the parameter 's value is 0, the function returns 0.

int signOf ( int x ) { if ( x < 0 ) { return - 1; } else if ( x == 0 ) { return 0; } else { return 1; } }

Write the definition of a function square, which receives an integer parameter and returns the square of the value of the parameter . So if the parameter 's value is 7, the function returns the value 49. If the parameter 's value happens to be 25, the functions returns the value 625. If the value of the parameter is 0, the function returns 0.

int square ( int x ) { return x * x ; }

Write the definition of a function twice, that receives an integer parameter and returns an integer that is twice the value of that parameter .

int twice (int x) {return 2*x;}

Write the definition of a function oneLess, which receives an integer parameter and returns an integer that is one less than the value of the parameter . So if the parameter 's value is 7, the function returns the value 6. If the parameter 's value happens to be 44, the functions returns the value 43.

int oneLess (int amount) { return amount -1; }

add is a function that accepts two int parameters and returns their sum . Two int variables , euroSales and asiaSales, have already been declared and initialized . Another int variable , eurasiaSales, has already been declared . Write a statement that calls add to compute the sum of euroSales and asiaSales and store this value in eurasiaSales.

eurasiaSales = add(euroSales, asiaSales);

Given the integer variables x and y, write a fragment of code that assigns the larger of x and y to another integer variable max.

if(x > y){ max = x; }else{ max = y; }

Write the definition of a function absoluteValue, that receives an integer parameter and returns the absolute value of the parameter 's value . So, if the parameter 's value is 7 or 803 or 141 the function returns 7, 803 or 141 respectively. But if the parameter 's value is -22 or -57, the function returns 22 or 57 (same magnitude but a positive instead of a negative). And if the parameter 's value is 0, the function returns 0.

int absoluteValue ( int x ) { return abs ( x ) ; }


Set pelajaran terkait

LSAT Sufficient vs. Necessary Assumption Questions

View Set

Practice Exam Oregon Life/Health Insurance

View Set

Prep-u: Chapter 7: Growth and Development of the Adolescent

View Set

DNA and Replication Quiz [(Bio) P.8 - P.9]

View Set

Medical-Surgical Assignment Exam

View Set

MGT 11A - Chapter 1 Practice Quiz

View Set

Series 66 11.29.2016 Incorrect Questions

View Set