Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Functions Part 1 Prototypes Arguments Overloading Return values.

Similar presentations


Presentation on theme: "1 Functions Part 1 Prototypes Arguments Overloading Return values."— Presentation transcript:

1 1 Functions Part 1 Prototypes Arguments Overloading Return values

2 2 Functions Provide the principle means of organizing and structuring procedural programs. Are self-contained blocks of code, the inner workings of which are hidden to the remainder of the program. Are subprograms in C++. Perform a specific task. Can act on data and return a value. Every C++ program has at least one function: main().

3 3 Why use functions? Make programs easier to: write save repeated typing of blocks of code debug isolate error sources maintain Programs are clearer Make programs more reliable Testing of individual functions rigorously Enable programming in the large. Structured programming Provide some reuse of code Build user defined libraries *

4 4 Types of functions Predefined -- found in the header files pow(x,n), strlen(word), etc. User-defined -- today’s topic DisplayMenu(); F(x);

5 5 Types of Functions Procedural functions general purpose, does not have to compute a value. Communication via parameters usually called on their own name Verb Noun e.g. PrintReport(); Functional functions focus on return value usually right hand side of = name describes return data description cos(x), exp(x), NextCutomerID()

6 6 Good Practice Make function names descriptive Procedural verb followed by a noun Functional describe return value Avoid wishy-washy names DoIt() Go() DealWithInput() Make sure each function does only one thing sin(), GetCustomerName(), EraseFile(), DisplayMenu() GetNameAndChangePhoneNumberAndPrintReport();

7 7 Function properties When you use a function you call the function May be passed data called arguments to function parameters May return directly a value to the calling program using return statement. May return indirectly more than one value via parameters and arguments pass by reference Can not change arguments passed to a parameter. Pass by value

8 8 Functions #include void demofunction(void); int main(void) { cout << "In main\n"; demofunction(); cout << "Back in main\n"; return 0; } void demofunction(void) { cout << "In DemoFunction\n"; cout << “Still in function.\n”; } Prototype Call Definition (called) Demonstrate using debugger Caller

9 9 Function Prototype (declaration ) double Pythagorus( double, double ); data types Semicolon! do not need to specify parameter names! BUT DO IT ANYWAY

10 10 Function Prototype (declaration ) double Pythagorus( double B, double C); data types Semicolon! do not need to specify parameter names! BUT DO IT ANYWAY

11 11 Purpose of Prototypes A function cannot be called unless it has been declared. It describes the identifier return type and parameters types it therefore ensure it is called correctly. They enable access to functions written in other source files

12 12 Function Definition double Pythagorus(double B, double C) { double A; A = sqrt(B*B + C*C); return A; } * no semicolon! The instructions needed to complete sub-task parameter names must be specified Local variable

13 13 Function Call #include double Pythagorus(double, double); int main(void){ cout << “The hypotenuse is “<< Pythagorus(3, 4); return 0; } double Pythagorus(double B, double C) { double A; A = sqrt(B*B + C*C); return A; } OUTPUT The hypotenuse is 5 Demonstrate! Values passed to function Values received by arguments a and b

14 14 Program Structure #include function prototypes; Global constants int main(void){ variable declarations; statements [including function calls] return 0; } function definition(s)

15 15 Function Prototypes Examples double Pythagorus(double, double); void DisplayMenu(void); double times_em(int, int, int, int); double Logistic(double); void print_data(char, double);

16 16 Returning Values A function can receive many values Only one value can be directly returned

17 17 Returning Values used in “functional” functions The return statement: As in return 0 in main tells the function which value to send back to the calling function terminates the function call and returns immediately to the calling program note void functions do not need a return;

18 18 Return Statement Syntax return expression; Examples return c; return hypotenuse; return n*n*n; return status;

19 19 Return Statement int find_max(int x, int y) { int maximum; if (x >= y) maximum = x; else maximum = y; return maximum; } same data type *

20 20 Passing Data passing by value passes a single value to receiving parameter does not change value of variables referred to in call. passing by reference Changes value of variables referred to indirectly may change any number of variables. Accomplished by using references (this topic) using pointers (later topic) *

21 21 Passing by value before call double Biggest(double x, double y) { do stuff; } 00100 00300 int main() { double M=2,N=7; double Max = 0 Max = Biggest(M, N); return 0; } M N 7 2 00130 00230 Max 0 00280

22 22 Passing by value during call double Biggest(double x, double y) { do stuff; } x y 00100 00300 7 2 int main() { double M=2,N=7; double Max = 0 Max = Biggest(M, N); return 0; } M N 7 2 00130 00230 Max 0 00280

23 23 Passing by value after call double Biggest(double x, double y) { do stuff; } 00100 00300 int main() { double M=2,N=7; double Max = 0 Max = Biggest(M, N); return 0; } M N 7 2 00130 00230 Max 7 00280

24 24 Passing Data - by Value passing by value: copy A copy of a value is passed from the calling function to the called function. * double Pythagorus(double B, double C){ double A A = sqrt(B*B + C*C); return A; }

25 25 x=865 find_max(x, y) y=9090 * find_max(firstnum, secnum); call to find_max value in first_num is passed value in sec_num is passed Storing Values into Parameters firstnum = 865 secnum = 9090 arguments parameters

26 26 Passing Data - by Value void DoubleMe(int); // function prototype int main(void){ int w = 3; cout <<"w before the function call is "<<w<<‘\n’; DoubleMe(w); cout <<"w after the function call is "<<w<<‘\n’; return 0; } void DoubleMe(int q) { cout<<"Value passed to the function is "<<q<<endl; q = q *2;// doubles the value cout<<"Value at the end of the function is "<< q <<endl; } Demonstrate Chris

27 27 Passing Data - by Reference to change value of calling variable not in C Syntax &B&C double Pythagorus(double &B, double &C); Pythagorus(height, base); & B& C double Pythagorus(double& B, double& C) function prototype function call function definition note use of & tells compiler to pass by reference

28 28 Passing Data - by Reference * void DoubleMe(int &); // function prototype int main(void){ int w = 3; cout <<"w before the function call is "<<w<<‘\n’; DoubleMe(w); cout <<"w after the function call is "<<w<<‘\n’; return 0; } void DoubleMe(int & q) { cout<<"Value passed to the function is "<<q<<endl; q = q *2;// doubles the value cout<<"Value at the end of the function is "<< q <<endl; } A reference to the address of w is passed q is an alias for calling variable it has the same address

29 29 Passing by reference before call void DoubleMe(double & x) { x = 2*x; } 00100 00300 int main() { double M=2; DoubleMe(M); return 0; } M 2 00130 00230 00280 void DoubleMe(double & x) ;

30 30 Passing by reference during call void DoubleMe(double & x) { x = 2*x; } x 00100 00300 int main() { double M=2,N=7; double Max = 0 DoubleMe(M); return 0; } M 4 00130 00230 00280

31 31 Passing by reference after call void DoubleMe(double & x) { x = 2*x; } 00100 00300 int main() { double M=2; DoubleMe(M); return 0; } M 4 00130 00230 00280

32 32 Swap Routine (Classic) void swap(float& num1, float& num2) { float temp; temp = num1; num1 = num2; num2 = temp; }

33 33 Passing by reference before call 00100 00300 7 M 2 00130 00230 00280 void Swap(double & x, double & y) { double temp; temp = x; x = y; y = temp; } int main() { double M=2; N = 7; Swap(M,N); return 0; } N

34 34 Passing by reference during call x 00100 00300 7 M 2 00130 00230 00280 void Swap(double & x, double & y) { double temp; temp = x; x = y; y = temp; } int main() { double M=2; N = 7; Swap(M,N); return 0; } N y temp

35 35 Passing by reference during call x 00100 00300 2 7 M 2 00130 00230 00280 void Swap(double & x, double & y) { double temp; temp = x; x = y; y = temp; } int main() { double M=2; N = 7; Swap(M,N); return 0; } N y temp

36 36 Passing by reference during call x 00100 00300 2 7 M 7 00130 00230 00280 void Swap(double & x, double & y) { double temp; temp = x; x = y; y = temp; } int main() { double M=2; N = 7; Swap(M,N); return 0; } N y temp

37 37 Passing by reference during call x 00100 00300 2 2 M 7 00130 00230 00280 void Swap(double & x, double & y) { double temp; temp = x; x = y; y = temp; } int main() { double M=2; N = 7; Swap(M,N); return 0; } N y temp

38 38 Passing by reference after call 00100 00300 2 int main() { double M=2; N = 7; Swap(M,N); return 0; } M 7 00130 00230 00280 N void Swap(double & x, double & y) { double temp; temp = x; x = y; y = temp;; }

39 39 Demo swap using call by reference and by value

40 40 What’s happening ?

41 41 Passing by value before call 00100 00300 7 M 2 00130 00230 00280 void Swap(double x, double y) { double temp; temp = x; x = y; y = temp; } int main() { double M=2; N = 7; Swap(M,N); return 0; } N

42 42 Passing by value during call x 00100 00300 7 M 2 2 00130 00230 7 00280 void Swap(double x, double y) { double temp; temp = x; x = y; y = temp; } int main() { double M=2; N = 7; Swap(M,N); return 0; } N y temp

43 43 Passing by value during call x 00100 00300 2 7 M 2 2 00130 00230 7 00280 void Swap(double x, double y) { double temp; temp = x; x = y; y = temp; } int main() { double M=2; N = 7; Swap(M,N); return 0; } N y temp

44 44 Passing by value during call x 00100 00300 2 7 M 7 2 00130 00230 7 00280 void Swap(double x, double y) { double temp; temp = x; x = y; y = temp; } int main() { double M=2; N = 7; Swap(M,N); return 0; } N y temp

45 45 Passing by value during call x 00100 00300 2 7 M 7 2 00130 00230 2 00280 void Swap(double x, double y) { double temp; temp = x; x = y; y = temp; } int main() { double M=2; N = 7; Swap(M,N); return 0; } N y temp

46 46 Passing by value after call 00100 00300 7 int main() { double M=2; N = 7; Swap(M,N); return 0; } M 2 00130 00230 00280 N void Swap(double x, double y) { double temp; temp = x; x = y; y = temp;; }

47 47 Data Type Mismatch value parameters implicit type conversion - value of the actual argument is coerced to the data type of the formal parameter reference parameters no coercion because an address is passed, not a value *

48 48 A Comparison formalactual parameter isargument may be valuevariable, constant, or expression type coercion may take place referenceargument only of exact same type as formal


Download ppt "1 Functions Part 1 Prototypes Arguments Overloading Return values."

Similar presentations


Ads by Google