Practice Problems

Functions

Notice when you get compile errors (when gcc gives you an error)
vs. errors that crash the program (run-time errors)
vs. logical errors (compile and run fine but problem with your code)

  1. Write a function named getTotals that takes four integer parameters: amount of pennies, nickels, dimes and quarters. The function will return the total amount of money.
  2. Write a function that takes a temperature value in Fahrenheit and returns the value in Celsius.
  3. Write a function that takes a temperature value in Celsius and returns the value in Fahrenheit.
  4. Write the function headers for the following:
    1. A function named check which takes 3 arguments, an integer number, a floating pt value and a character and returns nothing.
    2. A function named getAbs that takes a floating pt value and returns the absolute value of it.
    3. A function that prints out a table of the numbers 1 through 10 and each number's square and cube.
  5. Which of the following statements about function parameters is true?
    1. Empty parameter lists are declared with the keyword void
    2. If there is only one parameter, the function list parentheses are not required
    3. Parameters are separated with semi-colons.
    4. The parameters in a function definition are defined in the function's body,
  6. True or False: Local variables must be declared at the beginning of a function.
  7. True or False: Function parameters are global variables.
  8. Find any errors in the following function definition:
    	void fun ( int, x )
    	{
    		...
    		return;
    	}
    	
  9. Find any errors in the following function definition:
    	int fun ( int x, y )
    	{
    		int z;
    		...
    		return z;
    	}
    	
  10. Find any errors in the following function definition:
    	void fun ( int x, int y )
    	{
    		int z;
    		...
    		return z;
    	}
    	
  11. Find any errors in the following function calls:
    1. void fun( );
    2. fun( void );
    3. void fun( int , int y );
    4. fun( );
  12. Explain what is meant by the statement "a function should do only one thing".
  13. Write a program that reads 3 integers and then prints them in the order read, and then reversed. Use 3 functions, one to read the data, one to print them in the order read, and one to print them reversed.
  14. Write a function named whole that returns the integer part of any number passed to the function Hint: assign the passed argument to an integer variable.
  15. Write a function named fracpart that returns the fractional part of any number passed to the function. For example, if the number 123.4567 is passed to the function, the number .4567 should be returned. Make use of your function whole above as part of your answer.
  16. Write a function named maxOf2 that takes two integer arguments and returns the larger of the two.
  17. Write function named maxOf3 that takes three integer arguments and returns the largest of the three, making use of your function maxOf2 that you previously created.
  18. Make sure you write full programs to test all your functions you wrote in these practice problems!
  19. What's wrong with the following listing?
        #include 
        void print_msg( void );
        main()
        {
            print_msg( "This is a message to print" );
            return 0;
        }
        void print_msg( void )
        {
            puts( "This is a message to print" );
            return 0;
        }
    
  20. What's wrong with the following function definition?
        int twice(int y);
        {
            return (2 * y);
        }
       
  21. Write a program that uses a function to find the average of five type float values entered by the user.
  22. Write a recursive function to take the value 3 to the power of another number. For example, if 4 is passed, the function will return 81.
  23. How many values can a function return?
  24. If a function doesn't return a value, what type should it be declared?
  25. What's the difference between a function definition and a function prototype?
  26. 4. We're going to write a simple (very simple!) graphics program. We'll write a function printsquare which prints a square (made out of asterisks) of a certain size. Then we'll use our favorite for-i-equals-1-to-10 loop to call the function 10 times, printing squares of size 1 to 10.
          #include <stdio.h>
    
          int printsquare(int);
    
          int main()
          {
          	int i;
          	for(i = 1; i <= 10; i = i + 1)
          		{
          		printsquare(i);
          		printf("\n");
          		}
          	return 0;
          }
    
          int printsquare(int n)
          {
          	int i, j;
          	for(i = 0; i < n; i = i + 1)
          		{
          		for(j = 0; j < n; j = j + 1)
          			printf("*");
          		printf("\n");
          		}
          	return 0;
          }
    
          Type the program in and run it. Then see if you can modify it to print ``open'' squares, like this:
    
          ****
          *  *
          *  *
          ****
    
    instead of filled squares. You'll have to print the box in three parts: first the top row, then a number of ``* *'' rows, and finally the bottom row. There's obviously no way to print ``open'' boxes of size 1 or 2, so don't worry about those cases. (That is, you can change the loop in the top-level main function to
    for(i = 3; i <= 10; i = i + 1)
    .)
  27. Write a square() function and use it to print the squares of the numbers 1-10:
    1  1
    2  4
    3  9
    4  16
    ...
    9  81
    10 100
    
  28. Write the function
    	void printnchars(int ch, int n)
    
    which is supposed to print the character ch, n times. (Remember that %c is the printf format to use for printing characters.) For example, the call printnchars('x', 5) would print 5 x's. Use this function to rewrite the triangle-printing program of assignment 1 (exercise 4).
  29. Write a function to compute the factorial of a number, and use it to print the factorials of the numbers 1-7. Harder: do from 1 - 10.
  30. Write a function celsius() to convert degrees Fahrenheit to degrees Celsius. (The conversion formula is °C = 5/9 * (°F - 32).) Use it to print a Fahrenheit-to-Centigrade table for -40 to 220 degrees Fahrenheit, in increments of 10 degrees. (Remember that %f is the printf format to use for printing floating-point numbers. Also, remember that the integer expression 5/9 gives 0, so you won't want to use integer division.)