Lab: Functions

This laboratory exercise provides initial practice with functions and passing values as parameters within C programs. Download the starter code for this lab: functions.tar.gz and extract it with the following terminal commands:

$ cd csc161/labs
$ tar xvzf ~/Downloads/functions.tar.gz

This lab includes a Makefile, so you can run make to compile everything or make ___ where ___ is the name of the one program you want to compile. Again, we’ll discuss Makefiles in depth later in the course; for now, this is just a tool that can help you work a little more efficiently.

A. Using Functions to Repeat Tasks

Imperative languages allow functions to have side effects, meaning they perform some action that is visible after the function’s execution has finished. These functions will often have a void return type, meaning they do not return a value at all. You can call the function to perform some action, but it does not produce a result that you could use in an expression. Racket also allows side effects, but most functions you wrote in CSC 151 likely did not work this way.

Open the file quadratic.c to complete the following exercises.

Exercises

  1. Review the code in quadratic.c. Pay close attention to the comments, especially the ones that begin with TODO, since you will be completing the implementation here. Move on once you’re comfortable with what’s provided.
  2. Complete the implementation of the printFormula function. Your program should print quadratic formulas using the form \(y=Ax^2+Bx+C\), where \(A\), \(B\), and \(C\) are replaced with the parameter values.
  3. Run make quadratic to compile your code and test it. Make sure your formulas are printing correctly.

B. Using Functions to Compute Values

We also write functions to compute something useful for us. Sometimes we do this just to organize the code logically, but often we’ll end up calling the function multiple times to do the same computation on different inputs. This is likely familiar from CSC 151, where nearly all of the functions you wrote had a purpose that included computing an answer for you.

Continue working in quadratic.c to complete the following exercises. Your solutions will require some new functions that you can use from the math.h header file: sqrt and pow. You can refer to the manpages for these functions for more information about how to use them.

Exercises

  1. Recall that quadratic equations can have two roots, which you can find with the quadratic formula. Complete the implementations of findFirstRoot and findSecondRoot to compute the roots for a quadratic formula. You are welcome to look up the quadratic formula if you don’t remember it. Run the program and check to make sure your implementation is working correctly.
  2. You may have noticed that the square root portion of the quadratic formula is computed in both the functions you implemented. Write a new function that computes just the value \(sqrt(b^2-4ac)\) and update your findFirstRoot and findSecondRoot functions to use call this function instead of duplicating the code in each. Run the program to verify you get the same answers as before.

C. Passing Arrays to Functions

You can also pass arrays to functions, which we’ll often do when we work with collections of data. The trick with arrays in C is that it is often not enough just to pass in the array; we also need to know how long the array is, and this information is not available if you forget to pass it as a separate parameter.

Complete your work for the exercises below in the file array-params.c.

Exercises

  1. You’ll complete the sum function in a moment, but before you do that review the code in the provided file and make a guess about what the sum will be each of the three times it is computed. Explain each of your guesses.
  2. Complete the implementation of the sum function and make sure it returns the computed sum instead of 0.
  3. Run the program and compare its output to your guesses. For any output you guessed wrong, explain what you think is happening that you didn’t expect.
  4. Try passing in incorrect values for the length of the values array when calling sum. What happens if the length you pass is less than the actual length of the array? What if it is larger?

D. The Perils of Global Variables

Global variables are variables declared outside any function. You can use them in functions without passing them as parameters. This may seem convenient, but it can cause problems.

The exercises below refer to the code in globals.c.

Exercises

  1. Predict what the program will print. Write down your prediction.
  2. Verify your prediction by compiling and running the code. If your prediction was incorrect, try to explain where you went wrong.
  3. Write a brief explanation of why the use of global variables can make it difficult to predict how a program will behave. Try to focus on general characteristics rather than this specific example.
  4. (Optional) If you have time, try to rewrite this program using parameters and return values instead of relying on a changing global variable.