1 / 44

Functions

Functions. Functions, locals, parameters, and separate compilation. What is a Function?. A function is a named block of code Type: the type of the return value it computes Input: parameters that are passed to the function

korene
Download Presentation

Functions

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 Functions, locals, parameters, and separate compilation

  2. What is a Function? • A function is a named block of code • Type: the type of the return value it computes • Input: parameters that are passed to the function • Output: its return value computed based on input parameters and maybe other stuff • Local variables: variables declared inside the function block

  3. Typical Function type name input int max(int a, int b) { int maxval; if (a > b) maxval = a; else maxval = b; return maxval; } { local variable code block output

  4. Exercise 1 • Write the max function as given • Use forward declaration for max() • Write a similar min function • Write a little main function to • Declare two ints • Ask for user input to set their values • Call max and min • Print out the inputs, then their max and min (with appropriate indications) • Compile and test your program

  5. What is a Function? • Recall the two parts of a function: • Function header • Function body (or definition) • Recall function header: • Type: the type of the return value • Name: name of the function • Formal parameter list: the input parameters with their types

  6. Function Signature, Body • Function signature is: • Name • Formal parameter type list • This allows compiler and linker to figure out which code to invoke • Function body (or definition) • Block of code, may have local vars • Has return statement for return value

  7. What is a Function? • Function generally has: • Input: parameters that are passed to the function (may be null list) • Output: its return value computed based on input parameters (if any) and maybe other stuff • Local variables: variables declared inside the function block – generally these go away on function return

  8. How Does a Function Pass Parameters? • Function names input parameters • These are “formal parameters” that may be referenced as variables inside the function • The scope of these variables is confined to the function code block • When invoked, expressions are used • These are “actual parameters” • Formal parameters are “bound” to them

  9. Another Function int sum(int a, int b) { int sum = 0; for (int i = a; i <= b; ++i) sum += i; return sum; }

  10. Exercise 2 • Write the sum function as given • Write a little main function to • Declare two ints X and Y • Ask for user input to set their values • Print out the inputs, then call sum and print out the sum from X to Y inclusive • Compile and test your program • What could possibly go wrong? • Stand up, walk around, talk about it

  11. Analysis • What could go wrong with the sum function as written? • What if the actual parameter passed in as a is greater than the actual parameter passed in as b? • Did you test that case? • How do we fix it?

  12. Exercise 3 • Use the max and min functions from before to fix the sum function • Compile and test • Did you apply max and min from inside sum, or before you called sum? • Which way is “safer”? Why?

  13. Separate Compilation • A way to make the development process more efficient and manageable • Typically three kinds of files: • Main program file – has main() • Header file(s) – has declarations, included in main (and elsewhere) • Implementation file(s) – has function definitions

  14. Exercise 4 • Make a header file with declarations for max, min, and sum • Make a main (or driver) file with only the main() function - #include your header file • Make an implementation file with the function definitions only • Compile impl files with -c option • Compile main with impl.o files

  15. Example $ ls mainSum.cpp minMaxSum.h minMaxSum.cpp $ g++ -c minMaxSum.cpp $ ls *.o • minMaxSum.o $ g++ -c mainSum.cpp $ g++ mainSum.o minMaxSum.o $ ./a.out ...

  16. Analysis • What is going on here? • Compiler produces “relocatable object” code that has information needed for linking • What references are dangling • What references it can bind • Linker can then link these .o files to make an a.out executable file

  17. Why Bother? Exercise 5 • Take your old file that had main that called max and min and make it into a driver only (no max or min function definitions in it) – just #include the header file you just made • Now compile your new main with the minMaxSum.o file • Test it out!

  18. Reflection • So now you are able to define and compile useful functions separately from the code that invokes them! • What are some benefits of this? • Only have to recompile the files that change (directly or indirectly) • Can have team members each work on their own file(s)

  19. Hey, I Know a Better Way... • Look back at the sum function • Is there a better way to compute the same value? • Euler did this as a schoolchild • sum(a, b) = count x average = (b – a + 1) * (a + b) / 2

  20. Exercise 6 • Take your old impl file with the definition of sum() in it and change it to compute the sum the Euler way • Now compile your new impl with the -c option • Now link your old main.cpp to the new relocatable object file • Test it out!

  21. Reflection • The header did not change • The interface to the functions did not change • The result of the computation did not change • So we did not need to recompile the driver or change the header!

  22. More Reflection • However, even though only the sum() function changed, we had to recompile the whole impl file with max() and min() in it • What if each of these took 10 minutes to compile? • Better still to separate out each function in its own impl file and compile each separately

  23. Exercise 7 • Split the impl file into three files – one each for sum, max, and min • Note that you will need the header file (at least in sum.cpp) • Now compile them with the -c option • Now link your old main.cpp to the new relocatable object files • Test it out!

  24. (Same) Reflection • The header did not change • The interface to the functions did not change • The result of the computation did not change • So we did not need to recompile the driver or change the header!

  25. How Does a Function Pass Parameters? • There are several ways a function may pass parameters • By value • By reference • By copy-in-copy-out • By pointer (sometimes also called reference) • By name

  26. Call-by-Value • This is the most common way to pass parameters • Space for the type declared in the formal parameter list is allocated on the stack • The value of the expression used at invocation is stored into that place on the stack

  27. Call By Value void swap(int a, int b) { int temp = a; a = b; b = temp; } void main() { int a = 2; int b = 3; swap(a, b); }

  28. Questions?

  29. Call By Value void swap(int a, int b) { int temp = a; a = b; b = temp; } void main() { int a = 2; int b = 3; swap(a, b); }

  30. Call By Reference void swap(int* ints, int i_1, int i_2) { int temp = ints[i_1]; ints[i_1] = ints[i_2]; ints[i_2] = temp; } public void main() { int[] ints = {1, 2, 3, 4}; swap(ints, 0, 3); } ints 1 2 3 4

  31. Call By Reference void swap(int* ints, int i_1, int i_2) { int temp = ints[i_1]; ints[i_1] = ints[i_2]; ints[i_2] = temp; } public void main() { int[] ints = {1, 2, 3, 4}; swap(ints, 0, 3); } ints 1 2 3 4

  32. Call By Reference void swap(int* ints, int i_1, int i_2) { int temp = ints[i_1]; ints[i_1] = ints[i_2]; ints[i_2] = temp; } public void main() { int[] ints = {1, 2, 3, 4}; swap(ints, 0, 3); } ints 1 2 3 4

  33. Call By Reference void swap(int* ints, int i_1, int i_2) { int temp = ints[i_1]; ints[i_1] = ints[i_2]; ints[i_2] = temp; } public void main() { int[] ints = {1, 2, 3, 4}; swap(ints, 0, 3); } ints 1 2 3 4

  34. Call By Reference void swap(int* ints, int i_1, int i_2) { int temp = ints[i_1]; ints[i_1] = ints[i_2]; ints[i_2] = temp; } public void main() { int[] ints = {1, 2, 3, 4}; swap(ints, 0, 3); } ints 1 2 3 4

  35. Call By Reference void swap(int* ints, int i_1, int i_2) { int temp = ints[i_1]; ints[i_1] = ints[i_2]; ints[i_2] = temp; } public void main() { int[] ints = {1, 2, 3, 4}; swap(ints, 0, 3); } ints 1 2 3 4

  36. Function Calls • In C++, each function may specify the manner by which its parameters are received. • The type declaration of the parameter determines whether the data is passed “by value” or “by reference.” • Value types are said to be passed “call by value”. • On the other hand, reference types are said to be passed “call by reference.”

  37. Call By Reference (2) void swap(int &a, int &b) { int temp = a; a = b; b = temp; } void main() { int a = 2; int b = 3; swap(a, b); }

  38. Call By Reference (2) void swap(int &a, int &b) { int temp = a; a = b; b = temp; } void main() { int a = 2; int b = 3; swap(a, b); }

  39. Call By Reference (2) void swap(int &a, int &b) { int temp = a; a = b; b = temp; } void main() { int a = 2; int b = 3; swap(a, b); }

  40. Call By Reference (2) void swap(int &a, int &b) { int temp = a; a = b; b = temp; } void main() { int a = 2; int b = 3; swap(a, b); }

  41. Call By Reference (2) void swap(int &a, int &b) { int temp = a; a = b; b = temp; } void main() { int a = 2; int b = 3; swap(a, b); }

  42. Call By Reference (2) void swap(int &a, int &b) { int temp = a; a = b; b = temp; } void main() { int a = 2; int b = 3; swap(a, b); }

  43. Call By Reference (2) void swap(int &a, int &b) { int temp = a; a = b; b = temp; } void main() { int a = 2; int b = 3; swap(a, b); }

  44. Questions?

More Related