unit 4 functions
play

UNIT 4: FUNCTIONS TEXT FUNCTIONS Functions are encapsulated pieces - PowerPoint PPT Presentation

CS103L SPRING 2017 UNIT 4: FUNCTIONS TEXT FUNCTIONS Functions are encapsulated pieces of code - mini programs Also called procedures or methods Perform a computation given some inputs, usually return a value (result) When using


  1. CS103L SPRING 2017 UNIT 4: FUNCTIONS

  2. TEXT FUNCTIONS ▸ Functions are encapsulated pieces of code - mini programs ▸ Also called procedures or methods ▸ Perform a computation given some inputs, usually return a value (result) ▸ When using functions we treat as black-box ▸ We care *what* they do, not *how* ▸ Ex: double a = sin(x); ▸ There are many ways to compute or calculate sin(x), as long as we get the right answer back ▸ This is actually a useful, powerful concept ▸ Functions can be re-written, optimized, improved, without changing the code that *uses* the function

  3. TEXT ATTRIBUTES OF A FUNCTION ▸ Has a name to identify the function: avg, sin, max, min ▸ Zero or more inputs ▸ Zero or one output ▸ Note, only *one* output ▸ Performs a computation - code is in between { } ▸ Statements execute sequentially - like all C++ code ▸ Function is defined once, can be called as many times as necessary ▸ One function can call another, can call another, and so on

  4. TEXT #include <iostream> using namespace std; EXECUTING A FUNCTION int max(int a, int b) { ▸ When a function is called, calling code is “paused” while function is if(a > b) return a; executed else return b; ▸ Function executes based on the inputs given } ▸ When the function returns, the expression containing the function call evaluates to the return value. int main(int argc, char *argv[]) THIS EXPRESSION EVALUATES TO 6 { int x=6, z; z = max(x,4); THIS EXPRESSION EVALUATES TO 199 { cout << “Max is “ << z << endl; ▸ When a function hits a return statement, it immediately stops (returns) z = max(125, 199); with the given value. cout << “Max is “ << z << endl; ▸ Non-void functions must have at least one return statement that sets the return 0; return value } ▸ Void functions may have zero or more return statements (no value allowed)

  5. TEXT FUNCTION CALLS ▸ Function calls can be used like any expression ▸ Ex: min(a,b) || max(c,d) ▸ Ex: 1.0 + sin(x)/cos(y); ▸ Ex: max of three numbers? int x=5,y=10,z=20; //max of x,y,z? int max = max(max(x,y),z);

  6. TEXT ANATOMY OF A FUNCTION DEFINITION ▸ Formal parameters are the inputs when you define the function FORMAL PARAMETERS (TYPE AND NAME) ▸ Have a type and name FUNCTION NAME ▸ Are local variables to the function RETURN TYPE double avg(int a, int b) { double sum = a + b; FUNCTION BODY return sum/2; RETURN STATEMENT }

  7. TEXT ANATOMY OF A FUNCTION CALL ▸ Actual parameters are the values of what is passed to the function when it is called ▸ Important to note: A *copy* of the actual parameter is given to the function FUNCTION NAME int x = 5, y = 10; double a = avg(x,y); DESTINATION FOR RETURNED VALUE ACTUAL PARAMETERS

  8. TEXT PASS BY VALUE #include <iostream> ▸ Functions in C/C++ defined this way are using namespace std; pass-by-value void inc(int x) ▸ A *copy* of the actual parameter is given to { the function x = x+1; } ▸ Nothing happens to the actual parameter in the callee int main() { ▸ What does this code do? int x = 6; inc(x); ▸ How many x’s do we have? cout << x << endl; } ▸ Are they the same?

  9. TEXT PROGRAM DECOMPOSITION ▸ C is a procedural language. Procedures are the basic unit of abstraction: programs are broken down into a set of procedures, called in some order to solve a problem. ▸ Functions (procedures, methods) are units of code that can be called from other pieces of code, taking inputs and producing outputs. ▸ C++ is considered “object oriented” - but we can still use functions ▸ We’ll get to the difference later in the semester

  10. TEXT EXERCISE - DECOMPOSITION TECHNIQUES ▸ When developing your recipe, plan or algorithm ▸ List out the verbs and/or tasks that make up the solution to the problem ▸ Ex: modeling (simulating) a Blackjack casino game? ▸ shuffle(), deal(), bet(), double_down()… ▸ Ex: a program that models social networks? ▸ addUser(), addFriend(), updateStatus()…

  11. TEXT FUNCTION DEFINITIONS AND COMPILERS ▸ C/C++ compilers are single-pass, top-to-bottom ▸ The compiler needs to “know” about something before it can be used ▸ What happens here? int main() { double area; area = triangle_area(5.0, 3.5); } double triangle_area(double b, double h) { return 0.5*b*h; }

  12. TEXT FUNCTION DEFINITIONS SOLUTION #1 ▸ Move function definitions above main ▸ Not considered the best solution. ▸ Why? double triangle_area(double b, double h) { return 0.5*b*h; } int main() { double area; area = triangle_area(5.0, 3.5); }

  13. TEXT FUNCTION PROTOTYPES ▸ Better solution: double triangle_area(double, double); ▸ prototype (declare) function before main int main() { ▸ Implement anywhere double area; ▸ Why is this better? area = triangle_area(5.0, 3.5); } ▸ Prototypes are like a promise to the compiler: “Hey, compiler, I’m eventually double triangle_area(double b, double h) going to define this and it will look like { this…” return 0.5*b*h; ▸ After seeing the prototype the compiler can } compile code that uses the function before it even sees the implementation

  14. TEXT funcA() { NEED FOR FUNCTION PROTOTYPES if( condition ) funcB(); ▸ Get in the habit of using prototypes, it will save return; } you frustration and is good programming practice funcB() { if( condition ) ▸ Consider the following three functions funcC(); return; ▸ Called “mutually recursive” - 104/170 topic } ▸ Can’t be done without prototypes funcC() { if( condition ) funcA(); return; }

  15. TEXT FUNCTION SIGNATURES ▸ A signature is can uniquely identify you ▸ Functions have a signature: ▸ name ▸ number and type of arguments ▸ Two functions can have the same name! (as long as they have different signatures over all) ▸ int f1(int), int f1(double), int f1(int, double), int f1(int, char), double f1(), void f1(char) ▸ All of these specify different functions called f1 - don’t do this ;-p

  16. TEXT FUNCTION OVERLOADS ▸ Two functions with the same name, but different signatures are said to be “overloaded” ▸ Which is easier? OVERLOADED VERSIONS ▸ int max_int(int, int) ▸ int max(int, int) ▸ double max_double(double, double) ▸ double max(double, double) ▸ int pow_ints(int, int) ▸ int pow(int, int) ▸ double pow(double, double) ▸ double pow(double, double)

  17. TEXT IN CLASS EXERCISES ▸ abs_func ▸ Remove Factor ▸ ASCII square ▸ overloading

  18. TEXT void print_char_10_times(char); FUNCTION CALL SEQUENCING void print_char(char); int main() { ▸ Functions can call other functions char c = '*'; g print_char_10_times(c); g y = 5; ... return 0; ▸ Each calling function is “paused” while the o } called function is excecuted void print_char_10_times(char c) { for(int i=0; i < 10; i++) { ▸ When the function finishes the calling function print_char(c); } resumes return; } ▸ Each function call has it’s own “scope”, void print_char(char c) { variables inside the function are only visible/ cout << c << endl; } accessible to that invocation

  19. TEXT funcA(int x) { ANOTHER SEQUENCING EXAMPLES if( x>0 ) funcB(x-1); ▸ Since one function can call return; } // Computes rectangle area, another, and that can call another // prints it, & returns it int print_rect_area(int, int); void print_answer(int); how does the compiler keep funcB(int x) int main() { everything straight? { int wid = 8, len = 5, a; a = print_rect_area(wid,len); if( x>0 ) } t funcA(x-1); int print_rect_area(int w, int l) ) { return; int ans = w * l; print_answer(ans); } return ans; } void print_answer(int area) int main() { cout << “Area is “ << area; { cout << endl; int x=2; } funcB(x); }

  20. TEXT Address COMPUTER MEMORY ORGANIZATION 0 Code … ▸ To answer that we need to see how memory is organized - Globals for your program … ▸ Entire memory address space (here 32-bits) is broken up Heap at and assigned for different purposes ▸ Compiled code goes at the bottom (near address 0) … ▸ Then global variables are assigned some space … Stack ▸ Then the heap (discussed later in the semester) (area for data local to a function) fffffffc ▸ Then the stack Memory (RAM)

  21. TEXT THE STACK // Computes rectangle area, // prints it, & returns it int print_rect_area(int, int); ▸ The stack is what we care about here void print_answer(int); int main() ▸ The stack is segmented into pieces to hold the data (variables) for each function. Why { they are called “stack-local” variables int wid = 8, len = 5, a; a = print_rect_area(wid,len); } ▸ Each time a function is called, a new “stack frame” is allocated. The code running for that function only has access to variables in it’s stack frame int print_rect_area(int w, int l) { ▸ When one function is finished the stack frame is deallocated and control returns to the int ans = w * l; print_answer(ans); function below it on the stack return ans; } Code for all functions Code for all functions Address 0x0000000 void print_answer(int area) Data for print_answer (area) { System and return link to print_rect cout << “Area is “ << area; Memory cout << endl; Data for print_rect (w,l,ans) and return link to main } (RAM) Data for main (wid,len,a) and return link to OS System stack area 0xffff ffff

Recommend


More recommend