>i>>j; k = FindMax(i,j); PrintMax(k); // Prints Max Value return 0; } /* Function Definitions */ int FindMax(int n1, int n2) { if (n1 > n2) { return n1; } else { return n2; } /* Function Definitions */ void PrintMax(int someNumber) { cout << "The max is " << someNumber << endl; } Value-returning functions Void functions"> >i>>j; k = FindMax(i,j); PrintMax(k); // Prints Max Value return 0; } /* Function Definitions */ int FindMax(int n1, int n2) { if (n1 > n2) { return n1; } else { return n2; } /* Function Definitions */ void PrintMax(int someNumber) { cout << "The max is " << someNumber << endl; } Value-returning functions Void functions">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 17:User-Definded function II Introduction to Computer Science Spring 2006.

Similar presentations


Presentation on theme: "1 Lecture 17:User-Definded function II Introduction to Computer Science Spring 2006."— Presentation transcript:

1 1 Lecture 17:User-Definded function II Introduction to Computer Science Spring 2006

2 2 User-Defined Functions Void functions: do not have a data type Value-returning functions: have a data type

3 3 #include using namespace std; int main() { int i, j; int k; cin>>i>>j; if (i > j) { k = i; } else { k = j; } cout << "The max is " << k << endl; return 0; } #include using namespace std; /* Function Declarations */ int FindMax(int n1, int n2); void PrintMax(int someNumber); int main() { int i, j; int k; cin>>i>>j; k = FindMax(i,j); PrintMax(k); // Prints Max Value return 0; } /* Function Definitions */ int FindMax(int n1, int n2) { if (n1 > n2) { return n1; } else { return n2; } /* Function Definitions */ void PrintMax(int someNumber) { cout << "The max is " << someNumber << endl; } Value-returning functions Void functions

4 4 #include using namespace std; int main() { float tempInF = 85.0; float tempInC; cout << "Hello World" << endl; float factor = 5./9.; float freezing = 32.0; tempInC = factor * (tempInF - freezing); cout << tempInF << " Fahrenheit equals " << tempInC << " Celsius " << endl; return 0; } #include using namespace std; /* Function Declarations */ void PrintHW(); float FtoC(float faren); int main() { float tempInF = 85.0; float tempInC; PrintHW(); /* Prints Hello World */ tempInC = FtoC(tempInF); cout << tempInF << " Fahrenheit equals " << tempInC << " Celsius " << endl; return 0; } /* Function Definitions */ float FtoC(float faren) { float factor = 5./9.; float freezing = 32.0; float celsius; celsius = factor * (faren - freezing); return celsius; } /* Function Definitions */ void PrintHW() { cout << "Hello World" << endl; } Value-returning functions Void functions

5 5 Void Functions: function definition Void functions and value-returning functions have similar structures: Heading: 1. Name of the function 2. Number of parameters 3. Data type of each parameter 4. Function Type (type of the value returned by the function) Body: 1. Code required to accomplish the task (the body of the function) The syntax of the function definition is: void functionName(formal parameter list) { statements } void is a reserved word A void function does not have a data type The syntax of the formal parameter list is: dataType identifier, dataType identifier,... Formal parameters are optional: void functionName() { statements }

6 6 Void Functions: function definition void PrintMax(int someNumber) { cout << "The max is " << someNumber << endl; } Function Heading void PrintMax(int someNumber) Function Body Function Name Formal parameter list void PrintHW() { cout << "Hello World" << endl; } Function Heading Function Body

7 7 Void Functions: call a void function To call a void function: Use its name, with the actual parameters (if any) in parentheses The return statement without any value is typically used to exit the function early A call to a void function is a stand-alone statement The syntax for a function call is: functionName(actual parameter list) functionName() (If no formal parameters in function definition) The syntax for the actual parameter list is: expression or variable,expression or variable,...

8 8 Actual parameter list Call a void Function with parameters #include using namespace std; /* Function Declarations */ int FindMax(int n1, int n2); void PrintMax(int someNumber); int main() { int i, j; int k; cin>>i>>j; k = FindMax(i,j); PrintMax( k ); return 0; } /* Function Definitions */ void PrintMax( int someNumber) { cout << "The max is " << someNumber << endl; } /* Function Definitions */ int FindMax(int n1, int n2) { if (n1 > n2) { return n1; } else { return n2; } Formal parameter list

9 9 Call a void Function without parameters #include using namespace std; /* Function Declarations */ void PrintHW(); int main() { /* Prints Hello World */ PrintHW(); return 0; } /* Function Definitions */ void PrintHW() { cout << "Hello World" << endl; }

10 10 Question How many values can be returned by void function? How many values can be returned by value- returning function? What if we want to return multiple values from the funtion? Answer1: Pass by reference (Using reference variables as parameters Answer2: pass a pointer to the variable to the functionpointer (will be covered later)

11 11 Reference Referencing is generally used in a wider context - in the context of "aliasing“. The "&" operator is used for referencing - meaning "reference to". This is of the form: Datatype& variable_name = initialisation_expression For example: int i = 10; int& j = i; // j is an alias for i Both i and j refer to the same object - modifying i is equivalent to modifying j, and vice versa. That is, a reference is an alias for an object and does not, itself, occupy any memory.

12 12 Reference (cont.) #include using namespace std; int main() { int x=10; int& y =x; y=y+1; cout<<"x="<<x<<endl; cout<<"y="<<y<<endl; }

13 13 #include using namespace std void swap(int x, int y); int main() { int x = 4; int y = 2; cout << "Before swap, x is " << x << ", y is " << y << endl; swap(x,y); cout << "After swap, x is " << x << ", y is " << y << endl; } void swap(int first, int second) { int temp; temp = second; second = first; first = temp; } A formal parameter receives a copy of the content of corresponding actual parameter. which is called Pass by value & & & & Any modifications to the local copy do not change the original variable in the calling program. An alias of the argument is passed to the called function. which is called Pass by reference When references are passed into a function, any changes made to the references will be seen in the calling function. Parameters Passing

14 14 Parameters Passing Pass by value (A formal parameter is a value parameter): The value of the corresponding actual parameter is copied into it The value parameter has its own copy of the data Any modifications to the local copy do not change the original variable in the calling program Pass by reference (A formal parameter is a reference parameter): An alias of the argument is passed to the called function. no copies of the actual parameter are made. When references are passed into a function, any changes made to the references will be seen in the calling function.

15 15 Reference Variables as Parameters  Reference parameters can:  Pass one or more values from a function  Change the value of the actual parameter  Reference parameters are useful in three situations:  Returning more than one value  Changing the actual parameter  When passing the address would save memory space and time

16 16 End of lecture 17 Thank you!


Download ppt "1 Lecture 17:User-Definded function II Introduction to Computer Science Spring 2006."

Similar presentations


Ads by Google