Practice: Functions

The 30 questions of unit 7: 14 short checks and 16 complete programs, from the first call to a tested charging station controller. Every answer is hidden until you open it.

How to use this page

Questions 1 to 14 are short checks: answer in a value, a sentence or a few lines of code. Questions 15 to 30 are complete programs. For each program, submit working code and three tests: a normal case, a boundary case, and an invalid or special case when there is one.

Try each question before you open the answer. The programs use scanf, so they start with #define _CRT_SECURE_NO_WARNINGS for Visual Studio (see the scanf lesson).

Prototypes and calls

1. Prototype

In int limit(int value, int high); state the function name, the return type, and the parameter types.

Answer

Try it yourself first, then open the solution step by step.

    2. Call result

    Given int triple(int x) { return x * 3; }, what is the value of triple(4)? Show the value of x.

    Answer

    Try it yourself first, then open the solution step by step.

      3. Argument and parameter

      In the call square(count), identify the argument. In int square(int value), identify the parameter. Answer in one or two sentences.

      Answer

      Try it yourself first, then open the solution step by step.

        4. void or value

        Choose a return type for a function that only prints a separator line. Give one reason.

        Answer

        Try it yourself first, then open the solution step by step.

          Copies and scope

          5. Pass by value

          Trace this code. What is a after the call?

          void f(int x)
          {
              x = 9;
          }
          int a = 3;
          f(a);
          Answer

          Try it yourself first, then open the solution step by step.

            6. Local scope

            A function declares int error = 0;. Can main use that variable by writing error?

            Answer

            Try it yourself first, then open the solution step by step.

              Finding faults

              7. Conflicting declaration

              Find the fault: the prototype is int average(int, int); and the definition is double average(int a, int b). Write the corrected code and name the original fault.

              Answer

              Try it yourself first, then open the solution step by step.

                8. Lost fraction

                Why does return (a + b) / 2; fail for 7 and 8 in a function that returns double? Repair it.

                Answer

                Try it yourself first, then open the solution step by step.

                  Contracts, tests and names

                  9. Function contract

                  Write a contract for is_valid_adc: the input range is 0 through 1023 inclusive; the output is 1 when valid and 0 otherwise.

                  Answer

                  Try it yourself first, then open the solution step by step.

                    10. Boundary set

                    Choose the smallest useful set of tests for a function that accepts 10 through 20 inclusive. List the inputs and what each one checks.

                    Answer

                    Try it yourself first, then open the solution step by step.

                      11. Clear name

                      Which name best states a returned result: check, thing, or is_temperature_safe?

                      Answer

                      Try it yourself first, then open the solution step by step.

                        Returns and program order

                        12. Early return

                        What does this function return for -8, 0 and 5?

                        int sign(int x)
                        {
                            if (x < 0) {
                                return -1;
                            }
                            if (x > 0) {
                                return 1;
                            }
                            return 0;
                        }
                        Answer

                        Try it yourself first, then open the solution step by step.

                          13. Caller responsibility

                          Explain the difference between value = clamp(value, 0, 100); and clamp(value, 0, 100);.

                          Answer

                          Try it yourself first, then open the solution step by step.

                            14. Program order

                            Place these in a valid common order: function definition, include directive, prototype, main.

                            Answer

                            Try it yourself first, then open the solution step by step.

                              Functions with one value

                              15. Absolute value

                              Read one int and print its absolute value using int absolute_value(int x). Do not use a library function.

                              Input: one integer. Output: its absolute value.

                              Answer

                              Try it yourself first, then open the solution step by step.

                                16. Minimum of three

                                Read three int values and print the smallest using int min3(int a, int b, int c).

                                Answer

                                Try it yourself first, then open the solution step by step.

                                  17. Even test

                                  Read one int. Use int is_even(int x) and print EVEN or ODD.

                                  Answer

                                  Try it yourself first, then open the solution step by step.

                                    18. Clamp

                                    Read value, low and high. If low is greater than high, print INVALID; otherwise print clamp(value, low, high).

                                    Output: INVALID if low > high; otherwise the value moved inside the range.

                                    Answer

                                    Try it yourself first, then open the solution step by step.

                                      19. Temperature conversion

                                      Read a temperature in Celsius as a double. Print it in Fahrenheit with exactly one decimal, using a function that returns the value.

                                      Answer

                                      Try it yourself first, then open the solution step by step.

                                        Functions with loops

                                        20. Digit count

                                        Read a nonnegative int and print its number of decimal digits using int digit_count(int n). Zero has one digit.

                                        Answer

                                        Try it yourself first, then open the solution step by step.

                                          21. Digit sum

                                          Read a nonnegative int and print the sum of its digits using int digit_sum(int n).

                                          Answer

                                          Try it yourself first, then open the solution step by step.

                                            22. Prime classifier

                                            Read an int and print PRIME or NOT PRIME using int is_prime(int n).

                                            Answer

                                            Try it yourself first, then open the solution step by step.

                                              23. Greatest common divisor

                                              Read two positive int values and print their greatest common divisor, using Euclid's algorithm in a function.

                                              Answer

                                              Try it yourself first, then open the solution step by step.

                                                24. Integer power

                                                Read a base and an exponent from 0 through 12. Print the base raised to the exponent, using a loop inside int power_int(int base, int exponent).

                                                Answer

                                                Try it yourself first, then open the solution step by step.

                                                  Engineering functions

                                                  25. Voltage validity

                                                  Read millivolts and print VALID for 12000 through 14500 inclusive, otherwise INVALID. Put the rule in a function.

                                                  Answer

                                                  Try it yourself first, then open the solution step by step.

                                                    26. Charge current

                                                    Write charge_current_ma: 2000 below 13500 mV, 1000 from 13500 through 14500, and 0 above 14500. Reject negative input.

                                                    Answer

                                                    Try it yourself first, then open the solution step by step.

                                                      27. Triangle classifier

                                                      Read three positive side lengths. Use functions to check that they form a triangle, and print EQUILATERAL, ISOSCELES, SCALENE or INVALID.

                                                      Answer

                                                      Try it yourself first, then open the solution step by step.

                                                        28. Sensor calibration

                                                        Read raw, offset, numerator and denominator. Print (raw + offset) * numerator / denominator with two decimals, using a function. A denominator of zero prints INVALID.

                                                        Answer

                                                        Try it yourself first, then open the solution step by step.

                                                          29. Menu calculator

                                                          Read a double, an operator (+, -, * or /) and a second double. Use one function per operation and print the result with two decimals. Division by zero, or an unknown operator, prints INVALID.

                                                          Answer

                                                          Try it yourself first, then open the solution step by step.

                                                            30. Charging station mission

                                                            Read voltage_mV and temperature_c. Print SAFE <current> when the voltage is 12000 to 14500 inclusive and the temperature is at most 45; otherwise print STOP 0. Use is_voltage_safe and charge_current_ma (question 26).

                                                            Answer

                                                            Try it yourself first, then open the solution step by step.