Download presentation
Presentation is loading. Please wait.
Published byArchibald Jessie Dorsey Modified over 9 years ago
2
Functions g prototypes g arguments g overloading g return values part I Re-read Section 1.2
3
Functions, again! 1 are subprograms in C++. 1 perform a specific task. 1 can act on data and return a value. 1 Every C++ program has at least one function: main(). * * 1 are self-contained blocks of code, the inner workings of which are invisible to the remainder of the program.
4
Functions Why use functions? 1 make programs easier to write, debug and maintain - divide and conquer! * Two main types of functions: 1 predefined -- found in the header files 1 user-defined -- today’s topic
8
Functions – I REPEAT! 1 are subprograms in C++. 1 perform a specific task. 1 can act on data and return a value. 1 Every C++ program has at least one function: main(). * * 1 are self-contained blocks of code, the inner workings of which are invisible to the remainder of the program.
9
Functions Why use functions? 1 make programs easier to write, debug and maintain - divide and conquer! * Two main types of functions: 1 predefined (library functions) found in the header files & the C++ library 1 user-defined – make your own, include it yourself
10
Functions - an example { int var1=1, var2=2, var3=3, var4=4; (‘A’, var1); //Call the function function1(‘A’, var1); //Call the function some statements; (var4, var3); //Call function2(var4, var3); //Call (var2); //Call function3(var2); //Call } Arguments to the functions
11
Function - an example void function1(char grade, int place) {cout << grade << “ is #” << place << endl; } void function2(int ml, int m2) {cout <<“var3 x var4 = “ << m1 * m2<<endl; } void function3(int Upick) {cout << Upick << “ is the value in var2\n”; }
12
Function properties 1 may be called (multiple times) 1 may be passed data called arguments 1 may return a value to the calling program 1 will not change the data stored in a received variable unless specifically instructed to do so
13
Functions 1. #include using namespace std; 2. void demofunction(void); //prototype 3. void main(void) 4. { 5. cout << "In main\n"; 6. demofunction(); 7. cout << "Back in main\n"; 8. } 9. void demofunction(void) 10. { 11. cout << "In DemoFunction\n"; 12. cout << “Still in function.\n”; 13. }
14
Function Function Output 5. In main 6. (calls function - 11. )In Demo Function - 12. )Still in function. 7. Back in main Line # * *
15
Function declaration (prototype) double Pythagorus( double, double ); data types only semicolon Function identifier – its name
16
Function Definition Syntax Syntax function header line function header { statements function body } *
17
Function Header Syntax Syntax type type function_name(parameters) no ; Example double double Pythagorus(double a, double b)
18
Example Example double Pythagorus(double a, double b) Function Definition * no ; { type var { double c; c = sqrt(a*a + b*b); return return c; } Communication into function Communication out of function
19
Function Call void main(void) { cout << “The hypotenuse is “ << Pythagorus(12, 5); } OUTPUT The hypotenuse is 13
20
Program Structure #include function prototypes; void main(void) { variable declarations; statements [including function calls] } function definition(s)
21
Function Prototypes Syntax return type function_name(type); Example double Pythagorus(double, double); * Need
22
Function Prototypes Examples double Pythagorus(double, double); void do_stuff(void); double times-em(int, int, int, int); double myfunc(double, int); void print_em(char, double);
23
Function Calls Syntax Example Syntax function_name(arguments); Example PrintPayCheck(employeeID, Amount); hyp = Pythagorus(3.0,4.0); big = find_max(a,b); * Return from function assigned to variables
24
Function Calls find_max(firstnum, secnum); get firstnum, find_max(, ) 865 get secnum * * * 865 865 firstnum 9090 secnummemory9090
25
Function Calls answer = Pythagorus(3.0,4.0); cout << “The hypotenuse = “ << answer << endl; * cout << “The hypotenuse = “ << Pythagorus(3.0,4.0)<<endl;
26
Function Calls answer = Pythagorus(3.0,4.0); answer answer * 100; answer = answer * 100; cout << “The hypotenuse = “ << answer << endl; * * 100 cout << “Hypotenuse = “ << Pythagorus(3.0,4.0) * 100 <<endl;
27
Program Structure #include function prototypes; void main(void) { variable declarations; statements [including function calls] } function definition(s)
28
Program Structure main function square call cube call square function cube function #include square prototype cube prototype
29
Program Structure int square(int);// function prototype int cube(int);// or function declaration void main(void) { int x = 8; square(x) cout <<“The square is “<< square(x) <<‘\n’; cube(x) cout <<“The cube is “ << cube(x) <<endl;... } int square(int n)// function definition { continued on next slide
30
Program Structure int square(int n)// function definition { int answer; answer = n*n; return answer; } int cube(int n) // function definition { int answer; answer = n*n*n; return answer; } { OR return n*n; { OR return n*n*n; *
31
Function Summary Prototype type function_name(parameter types) ; Call function_name(actual parameters) ; formal Definition formal type function_name(parameter types names) type function_name(parameter types & names) { } double Pythagorus(double, double); Pythagorus(height, base); double Pythagorus(double a, double b) * * * * *
32
The challenge! Look at the Function Exercises following 6.1 Pages 238 and 239 Do any or all of Exercises 2, 4, or 6. Run and test your programs ONE of these will be part of your next programming assignment.
33
Function Overloading Two or more distinct functions may have the same name. The data types of the arguments in the function calls must match those in the prototypes and in the definitions. The same function is given multiple definitions or implementations. The correct one is chosen by the compiler, not the programmer. * * *
34
Function Overloading The functions must differ in their parameter lists. The type and/or number of parameters must be different. Examples double myFunction(int, int, int); int myFunction(double, int, int); int myFunction (double, double); void myFunction(double); *
35
Function Overloading * *. // a is used // c is used // b is used // d is used myFunction(3,4,5); myFunction(3.0, 4.0); myFunction(11.1, 9, 2); myFunction(23.56); { callcall a double myFunction(int, int, int) b int myFunction(double, int, int) c int myFunction (double, double) d void myFunction(double) } Header
36
Returning Values A function can receive many values Only one value can be directly returned
37
Returning Values return The return statement: 1 tells the function which value to send back to the calling program 1 terminates the function call and returns immediately to the calling program
38
Return Statement Syntax return expression; Examples return c; return hypotenuse;
39
Return Statement int find_max(int x, int y) { int maximum; if (x >= y) maximum = x; else maximum = y; return maximum; } same data type *
40
Passing Data 1 passing by value gives a single value 1p1passing by reference may give back several values accomplished by using references (this topic) using pointers *
41
The NOT rule for functions The arguments in the function prototype, the function call, and the function header must agree in number, order, and type Remember this rule!
42
double Pythagorus(double a, double b) { double c; c = sqrt(a*a + b*b); return c; } Passing Data - by Value copy passing by value: A copy of a value is passed from the calling function to the called function. * double Pythagorus(double a, double b) { a = a * a; b = b * b; double c = sqrt(a*a + b*b); return c; }
43
x find_max(x, y) arguments y * find_max(firstnum, secnum); call to find_max value in first_num is passed 865 value in sec_num is passed 9090 Storing Values into Parameters
44
void main(void) {double height = 4.0, base = 3.0; double Pythagorus(double, double); cout << “Hypotenuse = “ << Pythagorus(height, base)<<endl;... } double Pythagorus(double a, double b) {double c; c = sqrt(a*a + b*b); return c; } Passing Data - by Value * 4.03.0
45
double Pythagorus(double a, double b) {double c; a++; b++; c = sqrt(a*a + b*b); return c; } Passing Data - by Value * back in main:cout << height; cout << base: 4.0 3.0
46
Passing Data - by Value void print_val(int); // function prototype void main(void) { int w = 3; cout <<"w before the function call is "<<w<<‘\n’; print_val(w); cout <<"w after the function call is "<<w<<‘\n’; } void print_val(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; }
47
Passing Data - by Value Output w before the function call 3 Value passed to the function is 3 Value at the end of the function is 6 w after the function call is 3
48
“Louis Pasteur’s theory of germs is ridiculous fiction.” Pierre Pachet Professor of Physiology Toulouse, 1872 Copyright © 1997 by Freedom TLC, Inc.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.