1 / 26

Functions

Functions. CIS 230 01-Feb-06. Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions Function Prototype Function Call Function Definition. Variable Declaration Variable Scope Pass by Value Function Overloading Default Parameter Values.

vin
Download Presentation

Functions

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. Functions CIS 230 01-Feb-06

  2. Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions Function Prototype Function Call Function Definition Variable Declaration Variable Scope Pass by Value Function Overloading Default Parameter Values Summary Slide

  3. Summary Slide (cont.) • Variable Declaration • Variable Scope • Pass by Value • Function Overloading • Default Parameter Values

  4. Using Functions • Name of the function • What it does • Type of data required • Type of data returned • Library required

  5. Using Functions • Function called by giving function’s name and passing data to it • Called function: Function summoned into action • Calling function: Origination of function call

  6. Mathematical Functions • #include <cmath>

  7. Mathematical Functions

  8. Math: | -8.39 | = 8.39 210 = 1024 e-3.2 = 0.0407622 C++: abs(-8.39) = 8.39 pow(2,10) = 1024 exp(-3.2) = 0.0407622 Mathematical Functions

  9. Mathematical Functions • Storing returned Data: int pos_val; pos_val = abs(-4); cout << pos_val;

  10. #include <cctype> tolower(value) toupper(value) isalpha(value) isdigit(value) Instead of: if (again == ‘y’ || again == ‘Y’) Use: if (( again = tolower(again) == ‘y’) cin >> invalue; if (isalpha(invalue)) Or: do { cin >> invalue; } while (!isalpha(invalue)) Misc. Functions

  11. Functions: Naming Conventions

  12. Writing Functions • Function Declaration/Prototype • Return type • Data type and order of values • Function call • Name of function • Arguments • Function Definition • Function Header • Function Body

  13. Writing Functions • Functions may receive none or several values (parameters) • Values are passed (arguments) to special variables (parameters) within the function • Parameters are identified inside the ( ) • Functions generally return one value

  14. Function Prototype returnDataType functionName (argument data types); int calcTotal ( int, int ); double swap ( int, char, char, double );

  15. // Header file for function: x // Name: // last modified: #ifndef X_H #define X_H // header file contents: // any named constant declarations? // // header of function ended with semicolon #endif // Header file for function: calcTotal // Name: B. Gilden // Last modified: 30-Jan-06 #ifndef calcTotal_H #define calcTotal_H int calcTotal ( int, int ); #endif .h Files

  16. Function Call #include “functionName.h” int main ( ) { int first, second, answer; cout << “Enter two values to sum from the \n” cout << “first to the second : “; cin >> first >> second; if ( first <= second ) { answer = calcTotal (first, second); cout << “The total is “ << answer << ‘\n’; else cout << “The values were not valid \n”; }

  17. Function Definition type functionName ( type var1, type var2, … ) { // Named constants; // Variable declarations; // Other C++ statements; return variable; } Formal Parameter List Function Header Function Body

  18. functionName.cpp // Header commments int calcTotal ( int a, int b ) { int counter, sum; sum = 0; for ( counter = 1; counter <= b; ++counter ) sum = sum + counter; return sum; }

  19. Assumptions: main () -> main.cpp main () includes “calcTotal.h” makes a call to calcTotal() calcTotal.h calcTotal.cpp >g++ -c main.cpp >g++ -c calcTotal.cpp Produces: main.o calcTotal.o >g++ -o program main.o calcTotal.o Produces: program (the executable) OR… >g++ -o program main.cpp calcTotal.cpp Compiling

  20. Outside a function (global) In the parameter area Inside the function // 1. outside - global int total; // 2. parameter area void sum (int a, int b) { // 3. inside the function int c; } Variable Declaration

  21. Variable Scope • Global variables • Local variables

  22. Pass by Value • Function receives copies of the values • Function cannot access/change the calling function’s variables. • Function may return at most one value.

  23. Function Overloading • Permits the same function to be defined for different argument data types. • Second function, same name • Parameters must be different

  24. Write a function to find the larger of two values. int max (int a, int b) { if (a >= b) return a; else return b; } What about floating point values? float max (float x, float y) { if (x >= y) return x; else return y; } Function Overloading

  25. Write a function that calculates the area of a rectangle. float rectArea (float length, float width) { float area; area = length * width; return area; } Invoked: result = rectArea(x,y); A square is a rectangle, with sides the same length. float rectArea (float length) { float area; area = length * length; return area; } Invoked: result = rectArea(x); Function Overloading

  26. Default Parameter Values • Assign the value in the prototype • non-default(s) first, defaults last: • int sample (int, char=‘a’, int = 7); • int example (int, int, double = 6.78); • Calling the function: • answer = sample (t, ch, w); // no defaults • answer = sample (t, ch); // last default (7) used • answer== sample (t); //defaults ‘a’ and 7 used • NOT: • answer = sample (t, ,w); //This will NOT work!

More Related