Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions prototypes arguments overloading return values part I.

Similar presentations


Presentation on theme: "Functions prototypes arguments overloading return values part I."— Presentation transcript:

1 Functions prototypes arguments overloading return values part I

2 Functions are self-contained blocks of code, the inner workings of which are invisible 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 Functions Why use functions?
make programs easier to write, debug and maintain - divide and conquer! Two main types of functions: predefined -- found in the header files user-defined -- today’s topic *

4 Function - an example { int var1=1, var2=2, var3=3, var4=4;
function1(“ASU”, var1); some statements; function2(var4, var3); function3(var2); }

5 Function - an example void function1(char name, int place) { cout << name << “ is #” << place << endl; } void function2(int al, int mel) { cout <<“var3 x var4 = “ << mel / al<<endl; } void function3(int casey) { cout << casey << “ is the value in var2\n”; }

6 Function properties may be called may be passed data called arguments
may return a value to the calling program will not change the data stored in a received variable unless specifically instructed to do so

7 Functions 1. #include <iostream.h> 2. void demofunction(void);
3. void main(void) { cout << "In main\n"; demofunction(); cout << "Back in main\n"; } 9. void demofunction(void) { cout << "In DemoFunction\n"; cout << “Still in function.\n”; }

8 Function Output Line # - 12.) Still in function. * * * * 5. In main
6. (calls function - 11.) In Demo Function - 12.) Still in function. 7. Back in main * * * *

9 Function declaration double Pythagorus( double, double );
data types only

10 Function Syntax Syntax { statements function body } *
function header line function header { statements function body } *

11 Function Header Syntax
type function_name(parameters) no ; Example double Pythagorus(double a, double b)

12 { Function Definition Example double Pythagorus(double a, double b) {
type var Example double Pythagorus(double a, double b) { double c; c = sqrt(a*a + b*b); return c; } no ; * *

13 Function Call void main(void) { cout << “The hypotenuse is “
<< Pythagorus(12, 5); } OUTPUT The hypotenuse is 13

14 Program Structure #include <iostream.h> function prototypes; {
void main(void) { variable declarations; statements [including function calls] } function definition(s)

15 Function Prototypes Syntax return type function_name(type);
Example double Pythagorus(double, double); *

16 Function Prototypes Examples void do_stuff(void);
double Pythagorus(double, double); void do_stuff(void); double times-em(int, int, int, int); double myfunc(double, int); void print_em(char, double);

17 Function Calls Syntax function_name(arguments);
Example Pythagorus(3.0, 4.0); * *

18 Function Calls find_max(firstnum, secnum); memory find_max( , ) 865
9090 secnum memory get firstnum get secnum find_max( , ) find_max(firstnum, secnum); 865 9090 * * * * * *

19 Function Calls answer = Pythagorus(3.0,4.0);
cout << “The hypotenuse = “ << answer << endl; cout << “The hypotenuse = “ << Pythagorus(3.0,4.0)<<endl; *

20 Function Calls answer = Pythagorus(3.0,4.0); answer = answer * 100;
cout << “The hypotenuse = “ << answer << endl; cout << “Hypotenuse = “ << Pythagorus(3.0,4.0) * 100 <<endl; *

21 Program Structure #include <iostream.h> function prototypes; {
void main(void) { variable declarations; statements [including function calls] } function definition(s)

22 Program Structure #include <iostream.h> square prototype
main function square call cube call square function cube function #include <iostream.h> square prototype cube prototype

23 Program Structure . . . int square(int); // function prototype
int cube(int); // or function declaration void main(void) { int x = 8; cout <<“The square is “<< square(x) <<‘\n’; cout <<“The cube is “ << cube(x) <<endl; . . . } int square(int n) // function definition { continued on next slide

24 {OR return n*n; {OR return n*n*n; Program Structure
int square(int n) // function definition { int answer; answer = n*n; return answer; } int cube(int n) // function definition answer = n*n*n; {OR return n*n; {OR return n*n*n; *

25 Function Summary Prototype type function_name(parameter types);
double Pythagorus(double, double); Call function_name(actual parameters); Pythagorus(height, base); Definition formal type function_name(parameter types & names) { } double Pythagorus(double a, double b) * * * * *

26 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. * * *

27 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); *

28 { } Function Overloading . myFunction(3,4,5); myFunction(3.0, 4.0);
// a is used // c is used // b is used // d is used a double myFunction(int, int, int) b int myFunction(double, int, int) c int myFunction (double, double) d void myFunction(double) } Header * * * *

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

30 Returning Values The return statement:
tells the function which value to send back to the calling program terminates the function call and returns immediately to the calling program

31 Return Statement Syntax return expression; Examples return c;
return hypotenuse;

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

33 Passing Data passing by value gives a single value passing by reference may give back several values accomplished by using references (this topic) using pointers *

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

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

36 Passing Data - by Value * * 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; 4.0 3.0 * *

37 Passing Data - by Value * double Pythagorus(double a, double b)
4.0 3.0 double Pythagorus(double a, double b) { double c; a++; b++; c = sqrt(a*a + b*b); return c; } back in main: cout << height; cout << base: *

38 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;

39 Passing Data - by Value Output Value passed to the function is 3
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

40 Passing Data - by Reference
Syntax double Pythagorus(double &, double &); Pythagorus(height, base); double Pythagorus(double& a, double& b) function prototype function call function definition

41 Passing Data - by Reference
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; address of height address of base * *

42 Passing Data - by Reference
address of height address of base double Pythagorus(double& a, double& b) { double c; a++; b++; c = sqrt(a*a + b*b); return c; } back in main: cout << height; cout << base: *

43 Passing Data - by Reference
In main() values referenced as 1 value stored a height 1 value stored b base In Pythagorus() values referenced as *

44 Passing Data - by Reference
{ float a, b, c, sum, product; void calc(float, float, float, float &, float &); // prototype cout << "Enter three numbers: "; cin >> a >> b >> c; calc(a, b, c, sum, product); // call cout << a<<“ + “<<b<<“ + “c<<“ = " << sum; cout << ‘\n’<<a<<“ * “<<b<<“ * “c<<“ = " << product; } void calc(float x, float y, float z, float &tot, float& multiply) { tot = x + y + z; // definition multiply = x * y * z; x++; y++; z--; // for demo purposes

45 Passing Data - by Reference
Output Enter three numbers: = * 7 * 9 = 315 x is 6, y is 8, z is 8 tot and sum refer to the same address product and multiply refer to the same address *

46 Passing Data - by Reference
void main(void) { int w = 3; void print_val(int &); // function prototype 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 Reference
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 6

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

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

50 A Comparison formal actual parameter is parameter may be
value variable, constant, or expression type coercion may take place reference variable only of exact same type as formal

51 What’s Happening???? call sequence
1. memory is allocated 2. parameters are passed 3. transfer of control return sequence 1. value of the return is stored memory is deallocated 3. transfer of control * *

52 “Louis Pasteur’s theory of germs is ridiculous fiction.” Pierre Pachet
End Note “Louis Pasteur’s theory of germs is ridiculous fiction.” Pierre Pachet Professor of Physiology Toulouse, 1872 Copyright © by Freedom TLC, Inc.

53 Function Practice - I: wholepart( )
accepts a double returns the integer part of any fraction Hint: Assign the passed argument to an integer variable. Ex returns 123 the calling function displays the result Bronson p

54 Function Practice - I: fracpart( )
accepts a double returns the fractional part of any number passed to it. Ex returns .4567 the calling function displays the result Bronson p

55 Function Practice - I: find_abs( )
accepts a double computes its absolute value returns the value calling function displays the result Bronson p

56 Function Practice - I: mult( )
accepts two doubles multiplies them returns the result calling function displays the result Bronson p

57 Function Practice - I: powfun( )
accepts two integers raises the first to the power of the second the returned value is a long integer the calling function displays the result Bronson p

58 Function Practice - I: tempvert( )
accepts a double converts C to F or F to C returns the converted temperature the calling function asks the direction of conversion the temperature to convert displays the result Bronson p IN IN double tempvert(double in_temp, char type) { if (type == 'c' || type == 'C') return( (5.0/9.0) * (in_temp ) ); else //if (type == 'f' || type == 'F') return ((9.0/5.0) * in_temp ); }


Download ppt "Functions prototypes arguments overloading return values part I."

Similar presentations


Ads by Google