university of illinoisdmnicol.web.engr.illinois.edu/ece190/lecture-notes/... · 1 ·...

13

Upload: others

Post on 23-Sep-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: University Of Illinoisdmnicol.web.engr.illinois.edu/ece190/lecture-notes/... · 1 · lecture_16_txt_1.txt · 2010-10-14 12:28 · David Nicol Using functions in C requires - a function's
Page 2: University Of Illinoisdmnicol.web.engr.illinois.edu/ece190/lecture-notes/... · 1 · lecture_16_txt_1.txt · 2010-10-14 12:28 · David Nicol Using functions in C requires - a function's
Page 3: University Of Illinoisdmnicol.web.engr.illinois.edu/ece190/lecture-notes/... · 1 · lecture_16_txt_1.txt · 2010-10-14 12:28 · David Nicol Using functions in C requires - a function's
Page 4: University Of Illinoisdmnicol.web.engr.illinois.edu/ece190/lecture-notes/... · 1 · lecture_16_txt_1.txt · 2010-10-14 12:28 · David Nicol Using functions in C requires - a function's
Page 5: University Of Illinoisdmnicol.web.engr.illinois.edu/ece190/lecture-notes/... · 1 · lecture_16_txt_1.txt · 2010-10-14 12:28 · David Nicol Using functions in C requires - a function's
Page 6: University Of Illinoisdmnicol.web.engr.illinois.edu/ece190/lecture-notes/... · 1 · lecture_16_txt_1.txt · 2010-10-14 12:28 · David Nicol Using functions in C requires - a function's
Page 7: University Of Illinoisdmnicol.web.engr.illinois.edu/ece190/lecture-notes/... · 1 · lecture_16_txt_1.txt · 2010-10-14 12:28 · David Nicol Using functions in C requires - a function's
Page 8: University Of Illinoisdmnicol.web.engr.illinois.edu/ece190/lecture-notes/... · 1 · lecture_16_txt_1.txt · 2010-10-14 12:28 · David Nicol Using functions in C requires - a function's

1 · lecture_16_txt_1.txt · 2010-10-14 12:28 · David Nicol

Using functions in C requires - a function's prototype; or a function's declaration FUNCTION PROTOTYPE tells the compiler all it needs to know about calling the function, EXCEPT where the function is (!) example int Factorial(int n);this identifies * the return type (here int) * the symbolic name (so it can recognize calls to this function) * types of input arguments (here one int)

why? The compiler needs to figure out how large the stack framewill be when the function is called - return value may be in the stack frame - arguments passed will be on the stack frame

Prototypes are used - so that a function can be called from code in a file BEFORE the full function declaration is encountered in a top-to-bottom pass of the text - so that a function whose code is in a different file (or whose object code is in a library) can be called Other examples of function prototypes double cos(double); /* a trig function declared in math.h */ int getchar(void); /* reads a character from "STDIN", often the keyboard */

A function need not return an argument, in that case the return type is of type "void" A function need not have any input arguments, in that case the prototype will specify "void" as input e.g., void clearScreen(void);

Calling a function with "void" input or return type just omits that, e.g. x = 2*y+z; clearScreen(); /* does not return a value, has no arguments */ FUNCTION DEFINITION - gives the actual code for the function - identifies type of return value - identifies "formal parameter list" of variables and the order the compiler expects them to appear in. example : imagine a function that computes a binomial coefficient B(N,k), e.g., the number of unique ways you may choose k items from a collection of N items /* compute binomial coefficient in a way that will blow up on large N */ int Binomial(unsigned int N, unsigned int k) { if( N < k ) { printf("k=%d cannot dominate N=%d in binomial coefficient!\n",

Page 9: University Of Illinoisdmnicol.web.engr.illinois.edu/ece190/lecture-notes/... · 1 · lecture_16_txt_1.txt · 2010-10-14 12:28 · David Nicol Using functions in C requires - a function's

2 · lecture_16_txt_1.txt · 2010-10-14 12:28 · David Nicol

k, N); return 0; } int Nf = Factorial(N); int Nmkf = Factorial(N-k); int kf = Factorial(k); return Nf/(Nmkf*kf); }

When Binomial is called, the symbol names of the arguments passed do not have to match the formal input parameters. e.g., z = Binomial(x,y); /* ok if both x and y are unsigned ints */ z = Binomial(N,t); /* ok if both N and t are unsigned ints */ z = Binomial(k,N); /* first input will be seen as "N" in subroutine second input will be seen as "k" in subroutine */ UNDER THE COVERS ---------------- -- when the compiler sees the function call it allocates space for the 1st parameter, and the second parameter ... (where? On the stack frame created for the function call) -- when the compiler creates object code for the function, it knows where the 1st input parameter is on the stack frame, and where the 2nd input parameter is on the stack frame.... *** so names of variables are not carried along in the input parameters back to the Factorial function int Factorial(int n) { /* note there is no ';' here, the { starts body */ int i, result = 1; /* note that two variables are declared, one initialized */ for(i=1; i<=n; i++) result = result*i; return result; /* expression following "return" evaluated and passed back to the code code that calls Factorial, e.g. see Binomial */ }

AN IMPORTANT SUBLETY -------------------- - C passes arguments to function by VALUE - implicit understand of how the values are communicated to the function body through the stack frame example : consider this function int sumToN(int n) { int ans=0; while( 0 < n-- ) ans += n; return ans; } and somewhere in the code a call int sum, M=100;

Page 10: University Of Illinoisdmnicol.web.engr.illinois.edu/ece190/lecture-notes/... · 1 · lecture_16_txt_1.txt · 2010-10-14 12:28 · David Nicol Using functions in C requires - a function's

3 · lecture_16_txt_1.txt · 2010-10-13 22:11 · David Nicol

sum = sumToN(M); What value does M have after this call??? -- in "Call By Value" it has value 100 * 100 was copied onto stack frame, decremented from stack frame does not affect storage of M--- because M lives in a different memory location

-- some programming languages also support "Call By Reference" --- which you can accomplish in C, but that will wait.... Go through example of a program that computes PI

Page 11: University Of Illinoisdmnicol.web.engr.illinois.edu/ece190/lecture-notes/... · 1 · lecture_16_txt_1.txt · 2010-10-14 12:28 · David Nicol Using functions in C requires - a function's
Page 12: University Of Illinoisdmnicol.web.engr.illinois.edu/ece190/lecture-notes/... · 1 · lecture_16_txt_1.txt · 2010-10-14 12:28 · David Nicol Using functions in C requires - a function's
Page 13: University Of Illinoisdmnicol.web.engr.illinois.edu/ece190/lecture-notes/... · 1 · lecture_16_txt_1.txt · 2010-10-14 12:28 · David Nicol Using functions in C requires - a function's

1 · lecture_16_txt_2 · 2010-10-14 15:26 · David Nicol

Another example : area of a ring

On board : concentric circles, one of radius r, the other of radius R area of smaller circle : a = 2*PI*r area of larger circle : A = 2*PI*R area of ring : ringA = A - a #include <stdio.h> #include <math.h> #include <assert.h> double areaOfCircle(double); double computePI(double); main() { double r,R, ringA; printf("Enter radii (r1 r2) :"); scanf("%lf %lf", &r, &R);

/* sanity check --- program terminates with an error message if statement in assert macro does not evaluate TRUE */ assert( (0 <= r) && (0 <= R) ); ringA = fabs( areaofCircle(R) - areaOfCircle(r) ); printf("Area of the ring is %f\n", ringA);

} double areaOfCircle(double radius) { /* estimate the value of PI */ double pi = computePI(0.0000001); return pi*radius*radius; }