ECE 209 Example Functions

Function header

Printing stars

#include <stdio.h>

void printStars(int n) {
  int i ;
  for (i=0; i<n; ++i)
    putchar('*') ;
}

Calling a function

#include <stdio.h>
#include <stdlib.h>

void printStars(int) ;

int main(void) {
  int n ;
  printf("How many stars to print?\n") ;
  scanf("%d", &n) ;
  printStars(n) ;
  return(EXIT_SUCCESS) ;
}

Function prototype

Action of the function calls

Better functions

Generalizing a function

#include <stdio.h>

void printChars(char c, int n) {
  int i ;
  for (i=0; i<n; ++i)
    putchar(c) ;
}

Calling the generalized function

void printChars(char c, int n) ;

void printStars(int n) {
  printChars('*', n) ;
}

Calling the generalized function many times

void printChars(char c, int n) ;

void printRect(char c, int width, int heigth) {
  int i ;
  for (i=0; i<heigth; ++i)
    printChars(c, width) ;
}

A very little combinatorics

Iterative solutions

Factorial

int factorial(int n) {
  int f, i ;
  f = 1 ;
  for (i=1; i<=n; ++i)
    f = f*i ;
  return f ;
}

Permutations

int permutations(int m, int n) {
  int p, i ;
  p = 1 ;
  for (i=1; i<=m; ++i) {
    p = p * n ;
    --n ;
  }
  return p ;
}

Combinations

int combinations(int m, int n) {
  int c, i ;
  c = 1 ;
  for (i=1; i<=m; ++i) {
    c = c * n / i ;
    --n ;
  }
  return c ;
}

Bad solutions

Permutations

int permutations(int m, int n) {
  return factorial(n)/factorial(n-m) ;
}

Combinations

int combinations(int m, int n) {
  return factorial(n)/(factorial(m)*factorial(n-m)) ;
}

Recursive solutions

Factorial

int factorial(int n) {
  if (n==0)
    return 1 ;
  else
    return n*factorial(n-1) ;
}

Permutations

int permutations(int m, int n) {
  if (m==0)
    return 1 ;
  else
    return n*permutations(m-1,n-1) ;
}

Combinations

int combinations(int m, int n) {
  if (m==0)
    return 1 ;
  else
    /* See Combination under Wikipedia */
    return n*combinations(m-1,n-1)/m ;
}

Returning multiple values from a function

Suppose you wanted to write a function that received three values and returned the largest and smallest?

You can't.

Pointers

Pointers are C values that contain addresses.

Declaring pointers

 int    *pInteger ;
 double *pDouble ; 
 char   *pChar ;
 int    ***pConfusing ;

Two new operators

& unary operator that returns the address of a lvalue
* unary operator that returns value stored at an address
or unary operator that deferences a pointer

Confusing program segment

int i, j ;
int *p, *q ;
p = &i ;
q = &j ;
j = 5 ;
*p = *q + 1 ;
p = q ;
*p = *q + 1 ;

Setting multiple values from a function

void MinMax(int x, int y, int z, int *lo, int *hi) {
  int t ;
  if (x > y) {
    t = x ;
    x = y ;
    y = t ;
  }
  if (y > z) {
    t = y ;
    y = z ;
    z = t ;
  }
  if (x > y) {
    t = x ;
    x = y ;
    y = t ;
  }
  *lo = x ;
  *hi = z ;
}

The recommended way

Files for prototypes

Put your prototypes declarations in an header file whose name ends with the .h extension.

combinations.h

int combinations(int m, int n) ;

Files for the function

Put your function in a file and include the prototype file to make sure both agree.

combinations.c

#include "combinations.h"

int combinations(int m, int n) {
  if (m==0)
    return 1 ;
  else
    return n*combinations(m-1,n-1)/m ;
}

Files for the driver

Any program that uses your function should also include the header file.

numhands.c

#include <stdio.h>
#include <stdlib.h>

#include "combinations.h"

int main(void) {
  printf("There are %d different poker hands\n",
         combinations(5,52)) ;
  return(EXIT_SUCCESS) ;
}

Compiling the program

If you are not using an IDE, such as Netbeans, you'll have to compile the programs separately.

gcc -c -ansi -pendantic -Wall numhands.c
gcc -c -ansi -pendantic -Wall combinations.c
gcc -o numhands numhands.o combinations.o

Addresses on the stack

Try out the following to see how arguments are stored on the stack.

#include <stdio.h>

#include "combinations.h"

int combinations(int m, int n) {
  printf("m=%d, n=%d, &m=%08x, &n=%08x\n",
          m, n, (int) &m, (int) &n) ;
  if (m==0)
    return 1 ;
  else
    return n*combinations(m-1,n-1)/m ;
}