Programming for Engineers: Functions 18 February 2011 23 February 2011 25 February 2011
Function Architecture Prototype Specifies the return type The function name ought to have mnemonic content Specifies the argument (N.O.T.) Number Order Type Names ought to have mnemonic content The Prototype catches errors and oversights Invocation Assigns return values Obeys N.O.T. but without type declaration Definition May contain local variables Uses the variables in an algorithm to calculate a return value
Example Function Prototype: void starbar(void); Invocation: starbar(); Definition: void starbar(void) { int count; for(count=1;count =WIDTH;count++) putchar(*); putchar( n); }
letterhead1.c #include <stdio.> #define NAME GIGATHINK, INC #define ADDRESS 101 Megabuck Plaza #define PLACE Megapolis, CA 94904 #define WIDTH 40 void starbar(void); /* prototype */
letterhead.c (cont d) int main(void) { starbar(); printf( %s n,name); printf( %s n,address); printf( %s n,place); starbar(); return 0; }
letterhead.c (cont d) void starbar(void) { int count; for(count=1;count =WIDTH;count++) putchar(*); putchar( n); } The output is **************************************** GIGATHINK, INC 101 Megabuck Plaza Megapolis, CA 94904 ****************************************
Generalizing and Centering the Letterhead We use a function to display any number of a character. We use a function to determine the number of spaces needed to center the name and address.
Generalizing and Centering the Letterhead (cont d) #include <stdio.> #define NAME GIGATHINK, INC #define ADDRESS 101 Megabuck Plaza #define PLACE Megapolis, CA 94904 #define WIDTH 40 #define SPACE void show n char(char ch,int num); /* prototype */ int spaces(int width,char *str);/* prototype */
Generalizing and Centering the Letterhead (cont d) int main(void) { show n char(space,spaces(width,name)); printf( %s n,name); show n char(space,spaces(width,address)); printf( %s n,address); show n char(space,spaces(width,place)); printf( %s n,place); } return 0;
Generalizing and Centering the Letterhead (cont d) int spaces(int width,char * str) { int temp; temp= (width-strlen(str))/2 return Temp; } void show n char(char ch,int num) { int count; for(count=1; count <= num; count++) putchar(ch); }
Other Argument Lists No Arguments int myfunc(void); Unspecified Arguments int printf(char *, ); Required Arguments first in list (char *) Optional Arguments as ellipsis ( )
Recursion Function that calls itself Only works with some algorithms not general Uses a lot of stack space Runs slow because of call and return overhead
Recursive Function Rules The function must return a value. The function must call itself, which starts the winding of the stack. The function must have tail recursion, which starts unwinding of stack. The argument and the return value must be the same type.
Classic Example of Recursion The growth of an idealized (biologically unrealistic) rabbit population, assuming that: in the first month there is just one newly-born pair, new-born pairs become fertile from after their second month, each month every fertile pair begets a new pair, and the rabbits never die Let the population at month n be F n. Then F 0 = 0 F 1 = 1 F n = F n 1 + F n 2
Sample Fibonacci Program unsigned int fib(intn) { switch(n) { case 0: return 0; //special tail recursion case 1: case 2: return 1; //normal tail recursion default: return fib(n-1)+fib(n-2); } }
Recursion Pros and Cons Pros For some algorithms recursion is simplest solution Cons May exhaust memory (stack space) Hard to understand and maintain Call and return overhead
Place multiple source files in the same file Control Module Support Modules Header files Use the #include Include prototypes for support functions in the header file
#include #include < > looks in standard system directories #include looks in current working directory and then standard system directories
letter.h #define NAME GIGATHINK, INC #define ADDRESS 101 Megabuck Plaza #define PLACE Megapolis, CA 94904 #define WIDTH 40 #define SPACE void show n char(char ch,int num); /* prototype */ int spaces(int width,char *str);/* prototype */
letterheadmultiple.c #include <stdio.h> #include letter.h #include letterfun.c int main(void) { show n char(space,spaces(width,name)); printf( %s n,name); show n char(space,spaces(width,address)); printf( %s n,address); show n char(space,spaces(width,place)); printf( %s n,place); } return 0;
letterfun.c #include <stdio.h> #include letter.h int spaces(int width,char * str) { int temp; temp= (width-strlen(str))/2 return Temp; } void show n char(char ch,int num) { int count; for(count=1; count <= num; count++) putchar(ch); }
: A First Loook & operator retrieves address of data item * operator gets data indirectly using address Example bla = -6789; // @ 0x1234 ptr = &bla; //ptr = 0x1234 not -6789 ph = * ptr; //ph = -6789
void swapviadata(int x, int y) { int temp; temp = x; x=y; y=temp; } Initially we have x = 5 y = 13. After swapviadata we have x = 5 y = 13.
void swapviaptr(int * xptr, int * yptr) { int temp; temp = *xptr; *xptr =*yptr; *yptr=temp; } Initially we have After swapviaptr we have x = 5 y = 13. x = 13 y = 5.
Declare variable as pointer use indirect operator void swapviaptr(int * x, int * y) To get address of variable use address operator swapviaptr(&x,&y); To get data given ptr use indirection operator temp = *xptr; *xptr=*yptr; *yptr=temp;
ACLU #9 1. Translate the spaces function to a pointer notation function. 2. Write factorial as a recursive function. Write the tail recursion. Write the recursive call.
Programming Assignment Complete Chapter 9 Exercise 4 Complete Chapter 9 Exercise 5 Complete Chapter 9 Exercise 10 Due Date: 02 March 2011