 Download Download Presentation Functions

# Functions

Télécharger la présentation ## Functions

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Functions

2. Topics What is a function? Why use functions? (Modular Programming) How to define a function? How to invoke (call) a function. Pass-by-value parameters Pass-by-reference parameters Array as Function parameter

4. What Is a Function? • Function—a collection of statements that perform a specific task. • Examples: • cout << sqrt(121);// prints 11 • cout << pow(3.0, 2);// prints 9.0

5. Why Use Functions? int main(){ void function1(){ function1(): statement1; function2(); statement2;} statement3; } void function2(){ statement4; statement5; statement6; statement7; statement8; }} • Consider the program: • int main(){ statement1; statement2; statement3; statement4; statement5; statement6; statement7; statement8;}

6. Why Use Functions? • To break up a complex program into manageable pieces (Modular Programming) • To reuse same set of code • To simply the program • To hide complexity of program • to make main() more understandable • To make debugging easier • To make modification of a program easier

7. How to Define a function #include <iostream>using namespace std;// Define function max() hereint main(){ int n1 = 3; int n2 = 5; int big = max(n1, n2); // function call cout << big << endl; return 0;}

8. How to Define a function int max(int a, int b){ int result; if (a > b) result = a; else result = b; return result;}

9. Introducing Functions A function is a collection of statements that are grouped together to perform an operation.

10. Void Function #include <iostream>#include <string>using namespace std;// Define functions read() & print() hereint main(){ string greeting; greeting = read(); print(greeting); return 0;}

11. Void Function string read(){ string result; cin >> result; return string;} void print (string msg){ cout >> msg; }

12. Pass by Value a 3 int max(int a, int b){ . . . return result;}int main(){ int n1 = 3; int n2 = 5; int big = max(n1, n2); . . .} b 5 n1 value is copied to a,n2 value is copied to b. n1 3 n2 5

13. Pass by Reference void max(int a, int b, int &big){ if (a > b) big = a; else big = b;}int main(){ int n1 = 3; int n2 = 5; int result;max(n1, n2, result); cout << result; . . .} a 3 b 5 big big and result refer to the same address n1 n2 5 3 result 5

14. Function Prototype • Before a function can be called (e.g., in main()), the compiler must know where it is coming from. • You can separate the function declaration from the function implementation. • Function prototype declares the function. Function implementation is defined elsewhere.

15. Function Prototype #include <iostream>using namespace std;// Function prototypeint max(int a, int b); int main(){ . . . cout << max(n1, n2); . . . } // Function implementation here

16. Function Prototype // Function implementatio here int max(int a, int b){ int result; if (a > b) result = a; else result = b; return result;}

17. Array as Parameters // Function prototypevoid display(int a[], int cnt);int main(){ int list[] = {2, 4, 6, 8, 10}; int count = 5;display(list, count); return 0;} // Function implementation

18. Array as Parameters // Function implementation void display(int a[], int cnt){ for (int i = 0; i < cnt; i++){ cout << a[i] << “ “; }}

19. Array Manipulations #incldue <iostream>using namespace std;// Function prototypesvoid readData(int a[], int cnt);void printData(int a[], int cnt);double average(int [], int cnt);int main(){ . . . readData(list, count); printData(list, count); cout << “AVE: “ << average(list, count); . . .}

20. Your Turn • Write the implementation of the function: • void readData(int a[], int cnt);// Precondition: Array a is // uninitialized and cnt// is the number of // elements to be // intitialized.// Postcndition: Array a is initialized// with cnt values, by// generating cnt random// numbers.

21. Your Turn • Write the implementation of the function: • double average(int a[], int cnt);// Precondition: Array a has cnt // elements// Postcndition: The average of values// in a is returned.

22. Your Turn • Write the implementation of the function: • void printData(int a[], int cnt);// Precondition: Array a has cnt // elements// Postcndition: The contents of a is// returned.

23. Scope of Variables A local variable: a variable defined inside a function. Scope: the part of the program where the variable can be referenced. The scope of a variable starts from its declaration and continues to the end of the block that contains the variable.

24. Scope of Local Variables (cont.) A variable declared in the initial action part of a for loop header has its scope in the entire loop. But a variable declared inside a for loop body has its scope limited in the loop body from its declaration and to the end of the block that contains the variable.

25. Scope of Local Variables (cont.)

26. Global Variables C++ also allows you to use global variables. They are declared outside all functions and are accessible to all functions in its scope. Local variables do not have default values, but global variables are defaulted to zero. VariableScopeDemo

27. Static Local Variables After a function completes its execution, all its local variables are destroyed. Sometimes, it is desirable to retain the value stored in local variables so that they can be used in the next call. C++ allows you to declare static local variables. Static local variables are permanently allocated in the memory for the lifetime of the program. To declare a static variable, use the keyword static. StaticVariableDemo

28. Function Abstraction You can think of the Function body as a black box that contains the detailed implementation for the Function.

29. Math Functions