CSC161 2011S Imperative Problem Solving

Laboratory: Function Basics in C

Summary: We consider basic issues related to the implementation and use of functions in C.

Prerequisites: C basics.

Contents:

Preliminaries

a. Create a new directory for this lab. I'd suggest something like courses/csc161/labs/functions.

b. In that directory, create the standard Makefile.

Exercises

Exercise 1: Effects on Parameters

Consider the following program.

#include <stdio.h>

void
increment (int i)
{
  printf ("Before incrementing, i is: %d\n", i);
  i += 1;
  printf ("After incrementing, i is: %d\n", i);
  i += 1;
  printf ("After incrementing again, i is: %d\n", i);
} // increment

int
main ()
{
  int x = 10;

  printf ("When we begin, x is %d.\n", x);

  increment (x);

  printf ("When we finish, x is %d.\n", x);

  return 0;
} // main

a. What output do you expect to get from this program? (You need only note the values of x and i.)

b. Check your answer experimentally.

c. What does your answer tell you about the way functions in C treat their integer parameters?

d. Change increment and the rest of the program to use float values instead of int values.

e. What effect do you expect this to have on the output of the program?

f. Check your answer experimentally.

g. What does your answer tell you about the way functions in C treat their real number parameters?

Exercise 2: Effects on Parameters, Revisited

Consider the following program:

#include <stdio.h>

void
munge (char str[])
{
  printf ("About to munge \"%s\"\n", str);
  str = "munged";
  printf ("After munging: \"%s\"\n", str);
} // munge

int
main (int argc, char *argv[])
{
  printf ("When we begin, argv[0] is %s.\n", argv[0]);
  munge (argv[0]);
  printf ("When we finish, argv[0] is %s.\n", argv[0]);

  return 0;
} // main

a. What output do you expect to get from this program? (You need only note the values of argv[0].)

b. Check your answer experimentally.

c. What does your answer tell you about the way functions in C treat their string parameters when our primary operation on strings is assignment?

Exercise 3: Parameters, Re-Revisited

Consider the following program:

#include <stdio.h>

void
munge (char *args[])
{
  printf ("About to munge \"%s\"\n", args[0]);
  args[0] = "munged";
  printf ("After munging: \"%s\"\n", args[0]);
} // munge

int
main (int argc, char *argv[])
{
  printf ("When we begin, argv[0] is %s.\n", argv[0]);
  munge (argv);
  printf ("When we finish, argv[0] is %s.\n", argv[0]);

  return 0;
} // main

a. What output do you expect to get from this program? (You need only note the value of argv[0].)

b. Check your answer experimentally.

c. What does your answer tell you about the way functions in C treat their array parameters?

Exercise 4: Effects on Parameters, Revisited Yet Again

Consider the following program:

#include <stdio.h>

void
munge (char str[])
{
  printf ("About to munge \"%s\"\n", str);
  str[0] = 'X';
  printf ("After munging: \"%s\"\n", str);
} // munge

int
main (int argc, char *argv[])
{
  printf ("When we begin, argv[0] is %s.\n", argv[0]);
  munge (argv[0]);
  printf ("When we finish, argv[0] is %s.\n", argv[0]);

  return 0;
} // main

a. What output do you expect to get from this program? (You need only note the value of argv[0].)

b. Check your answer experimentally.

c. What does your answer tell you about the way functions in C treat their string parameters when our primary operation on strings is assignment?

Exercise 5: Predeclarations

Determine through experimentation answers to the following questions:

a. What happens if you fail to predeclare a function that returns an int. That is, what happens if you call this function and the definition of the function follows the call?

b. What happens if you fail to predeclare a function that returns a float?

c. What happens if you fail to predeclare a functions that returns void?

d. What happens if you fail to predeclare a function and provide the function with the wrong type or number of parameters?

For Those With Extra Time

Extra 1: Exponentiation

As you may recall, while we often do exponentiation through loops, it is more efficient to use a recursive formulation.

Write a procedure that implements this approach. The procedure should have the signature

double 
exponentiate (double x, int n);

Extra 2: Primality

Write a procedure that determines whether its integer parameter is a prime number. Your procedure should have the signature

int 
is_prime (int n)

 

History

Friday, 1 October 2010 [Samuel A. Rebelsky]

Tuesday, 1 March 2011 [Samuel A. Rebelsky]

  • Added a new problem 4 (munging the first character of a string).
  • Minor cleanup.

Wednesday, 2 March 2011 [Samuel A. Rebelsky]

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Wed Mar 2 11:34:18 2011.
The source to the document was last modified on Wed Mar 2 11:34:16 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Labs/functions-lab.html.

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright © 2010 Samuel A. Rebelsky. This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.