Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions CIS 230 01-Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.

Similar presentations


Presentation on theme: "Functions CIS 230 01-Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype."— Presentation transcript:

1 Functions CIS 230 01-Feb-06

2 Summary Slide 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

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

7 Mathematical Functions

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

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

10 Misc. Functions #include 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))

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 .h Files // 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

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; } Function Header Function Body Formal Parameter List

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 Compiling 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

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

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 Function Overloading 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; }

25 Function Overloading 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);

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!


Download ppt "Functions CIS 230 01-Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype."

Similar presentations


Ads by Google