1 / 28

Functions and recursion

Functions and recursion. CS101 2012.1. Functions. Single statement Statement block { … } If-then-else While, for, break, continue Next device for writing modular, reusable code: functions One declaration Many uses. Formal parameter. Return type. int abs(int a) {

mickey
Download Presentation

Functions and recursion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions and recursion CS101 2012.1

  2. Functions • Single statement • Statement block { … } • If-then-else • While, for, break, continue • Next device for writing modular, reusable code: functions • One declaration • Many uses Formal parameter Return type int abs(int a) { return a > 0? a : -a; } int x = -3, y = 5; int z = abs(x) – abs(y); Int b = z*2; A “call site” “Argument” Chakrabarti

  3. Passing parameters to functions • Default is “pass by value” void fun(int x) { x = 5; } main() { int a = 3; fun(a); cout << a << endl; } • Value 3 copied from a to x • x is locally modified • Modification has no effect outside fun • Even if x is changed to a Chakrabarti

  4. Modifying variables outside function • Use “pass by reference” instead void fun(int& x) { x = 5; } main() { int a = 3; fun(a); cout << a; } • No value copy • x becomes an alias for a • Modifying x amounts to modifying a • Efficient, even if you have no intention of modifying • In which case, sayconst int& x Demo Chakrabarti

  5. Beware of aliasing! • Swap function without temporary variablesvoid swap(int& a, int& b) { a = a-b; b = a+b; a = b-a;} • Consider the callint x=3, y=5; swap(x,y); • Compare with the callint x = 3; swap(x,x); • Can’t happen in pass by value Chakrabarti

  6. Execution • What happens when you runmain() { int a = 3; fun1(a); cout << a; } ? • Just before executing the implementation of fun1, the computer notes down in a special area of RAM what to do after fun1 returns • fun1 may call fun2 may call fun3… Chakrabarti

  7. Storing pending work in a stack • While I am reading a paper with a student … • The door opens and an office staff comes in to get some papers signed • I place those papers on top of the paper I was reading and begin to sign, when … • The phone rings • I take the call, speak, and hang up • Then I finish signing the papers • Then I return to the studentmeeting Last in, first out Chakrabarti

  8. How does the CPU know what to do next? Assign IDs (e.g., 1—8 in the example) CPU has a register with the ID of the next statement to be executed 1-2-3-4-5-6-8 or1-2-3-4-5-8 both possible (7 isn’t a real statement) cin >> fah; v2 = fah – 32; v3 = v2 / 9; cen = v3 * 5; if (cen < 5) { cout << “cold”; } kel = cen + 273; Program counter (PC) Chakrabarti

  9. Suppose input is n=4 Then the program counter will take values 1 2 3 43 43 43 4 6 Actually, the PC needs to count statements at the level of single instructions, not whole C++ statements A key use of the PC is in implementing function calls cin >> n; int sum = 0; for i = 1 to n { sum += i * i; } cout << sum; Program counter --- loops Chakrabarti

  10. Memory segments • A process is given three memory segments • Code segment • Stores compiled executable code • Read-only once loaded • Data segment a.k.a. “the heap” • Storage for string, vector<T>, matrix<T> etc. • Will discuss in more detail later • Stack segment: memory used for • Communication between caller and callee • Keeping track of pending work “Segmentation violation” Chakrabarti

  11. The stack pointer (SP) • SP is a special register like PC • Not directly accessible to the programmer • Points to the “top” ofthe stack segment • E.g., main calls fun1 calls fun2 SP Communication between fun1 and fun2 Address increases SP Communication between main and fun1 SP Base of stacksegment, say 4000 Chakrabarti

  12. Design of the callee High-level functionimplementation int abs(int a) { return a > 0? a : -a; } Activation record Compiled low-level code Chakrabarti

  13. Design of the caller main() { int x = -3, y = 5; int z = abs(x) – abs(y); int b = z*2; } Chakrabarti

  14. Saving register values • Suspend caller, execute callee, resume caller • CPU has a fixed, small set of registers • Callee must restore registers to values in caller just before callee was invoked • Also allocate space in activation record to save registers before call • After call, copy values back to registers before resuming caller • Overheads of function calls Chakrabarti

  15. Separate compilation units • Thus far, we have placed all our code in one C++ source file • Can’t share functions from many projects • Change one line, compile everything again • Example • MatrixPrinter.cpp and MatrixPrinter.hpp provide a function to print matrix<double> • Gaussian.cpp and MatrixInverse2.cpp call a function defined in MatrixPrinter.hpp and implemented in MatrixPrinter.cpp • The –c and –o flags to g++ Chakrabarti

  16. Make • When we change one file in a large project, which files need to be compiled again? • (File) system provides no consistency check between MatrixPrinter.cpp and MatrixPrinter.o and a.out • A makefile gives aformal specificationof dependencies Gaussian.exe Gaussian.o MatrixPrinter.o MatrixPrinter.hpp Gaussian.cpp MatrixPrinter.cpp Chakrabarti

  17. Makefile • The make program looks for a file called Makefile or makefile • A makefile consists of rules and actions • A rule has the formtarget: dependencyList • An action is a program to run, with command-line arguments • If any file on the dependency list is newer than the target, (re) run action Chakrabarti

  18. Make schedules “wavefront” of updates Gaussian.exe Gaussian.o MatrixPrinter.o MatrixPrinter.hpp Gaussian.cpp MatrixPrinter.cpp Chakrabarti

  19. Another example Gaussian.exe Gaussian.o MatrixPrinter.o MatrixPrinter.hpp Gaussian.cpp MatrixPrinter.cpp Chakrabarti

  20. main, argc and argv • So far we used main(){…} • But you can also pass command line arguments to main • Declare main(int ac, char *av[]){…} • ac gives the number of arguments • av is an array of legacy C strings (char pointers) • More about arrays and pointers soon Chakrabarti

  21. Using ac and av #include <iostream> #include <string> using namespace std; main(int ac, char *av[]) { int ax = 0; while (ax < ac) { string msg = av[ax]; cout << msg << endl; ax = ax + 1; } } This is a legacy C string (array of chars) Chakrabarti

  22. Recursion • A function can call itself • As long as this does not happen indefinitely • Each invocation results in another activation pushed on to the stack int fac(int n) { if (n == 0) return 1; else return n * fac(n-1); } int gcd(int m, int n) { if (n == 0) return m; else returngcd(n, m % n); } int fib(int n) { if (n < 2) return 1; else return fib(n-1) + fib(n-2); } Demo Chakrabarti

  23. fac(n) • fac(5) calls fac(4) calls fac(3) calls fac(2) calls fac(1) calls fac(0) • Returns 1 returns 1 returns 2 returns 6 returns 24 returns 120 • Easy way to visualize recursive calls • Pass additional recursion level parameter • Indent all messages by level blank spaces • Up to n activation records on the stack • Space-inefficient, time is O(n) for both recursive and iterative styles Chakrabarti

  24. 5 4 4 3 3 2 3 2 2 1 2 1 1 0 2 1 1 0 1 0 1 0 1 0 Recursion • Just because you can use recursiondoes not mean you should • Stack space limited, higher overhead 7 6 5 4 3 3 2 2 1 2 1 1 0 1 0 1 0 Chakrabarti

  25. Recursion can be a good thinking tool • E.g., mergesort reloaded void sort(vector<int>& vec, int lo, int hi) { if (hi-lo <= 1) return; int mid = (lo + hi)/2; sort(vec, lo, mid); sort(vec, mid, hi); vector<int> tmp; // will size to hi-lo merge(vec, lo, mid, mid, hi, tmp); copy(tmp, vec, lo, hi); } Chakrabarti

  26. Quicksort Choose an arbitrary pivot Sift elements to the orderless than pivot, pivot,greater than pivot Note that other elementsare not sorted Sort left and rightsegments Chakrabarti

  27. String edit distance • kitten  sitten  sittin  sitting • Replace k with s, replace e with i, append g • Let s[1..m], t[1..n] be input strings • Let d(i,j) be the (smallest) edit distance between s[1..i] and t[1..j] • Base cases • Recursion Chakrabarti

  28. Examples Chakrabarti

More Related