Ask Question

Whatâs wrong with the following function definition:

float average (int value1, int value2, int value3)

{

float average;

average = value1 + value2 + value3 / 3;

}

+5
Answers (1)
  1. 10 May, 00:09
    0
    The Answer for the given question is

    First is return (average) statement is not used in the last line in function average.

    second not included the parenthesis in adding the value1 + value2 + value3 so include (value1 + value2 + value3)

    Third is not type casting the value so type cast the value by modify that statement

    average = float (value1 + value2 + value3) / 3;

    Explanation:

    In the given code the return type is float that means function should return float value. So adding the statement in the last line return (average) here all the parameters are of int type but "average" is float type and when an int is divided by int, it will return int value. So we have to type cast either denominator or numerator.

    average = float (value1 + value2 + value3) / 3;

    To define any function definition it must follow the proper syntax of function definition

    The syntax is:

    Returntype functionname (datatype parameter1, datatype parameter 2)

    {

    //statement

    }

    For example

    int mux (int a, int b)

    {

    int c;

    c=a*b;

    return (c);

    }

    In this the returntype is integer thats why it return integer value c. If return type is void it will not return any value.

    So the correct code is

    float average (int value1, int value2, int value3)

    {

    float average;

    average = float (value1 + value2 + value3) / 3;

    return (average);

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Whatâs wrong with the following function definition: float average (int value1, int value2, int value3) { float average; average = value1 + ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers