1 / 16

Lecture 17:User-Definded function II

Lecture 17:User-Definded function II. Introduction to Computer Science Spring 2006. User-Defined Functions. Void functions : do not have a data type Value-returning functions : have a data type. #include <iostream> using namespace std; /* Function Declarations */

taite
Download Presentation

Lecture 17:User-Definded function II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. #include <iostream> 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 */ 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; } } #include <iostream> 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; } Value-returning functions Void functions

  4. #include <iostream> 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;} #include <iostream> 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; } Void functions Value-returning functions

  5. void functionName() • { • statements • } Void Functions: function definition • Void functions and value-returning functions have similar structures: • Heading: • Name of the function • Number of parameters • Data type of each parameter • Function Type (type of the value returned by the function) • Body: • 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:

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

  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. #include <iostream> 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; } } Actual parameter list Call a void Function with parameters Formal parameter list

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

  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 function (will be covered later)

  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. Reference (cont.) #include<iostream> using namespace std; int main() { int x=10; int&y =x; y=y+1; cout<<"x="<<x<<endl; cout<<"y="<<y<<endl; }

  13. & & & & Parameters Passing #include <iostream>using namespace stdvoid 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.

  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. 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. End of lecture 17 Thank you!

More Related