functions cis 230 01-feb-06. summary slide using functions mathematical functions misc. functions...

26
Functions CIS 230 01-Feb-06

Upload: clemence-oneal

Post on 04-Jan-2016

215 views

Category:

Documents


1 download

TRANSCRIPT

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

Functions

CIS 230

01-Feb-06

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

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

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

Summary Slide (cont.)

• Variable Declaration

• Variable Scope

• Pass by Value

• Function Overloading

• Default Parameter Values

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

Using Functions

• Name of the function

• What it does

• Type of data required

• Type of data returned

• Library required

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

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

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

Mathematical Functions

• #include <cmath>

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

Mathematical Functions

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

Mathematical Functions

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

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

Mathematical Functions

• Storing returned Data:

int pos_val;

pos_val = abs(-4);

cout << pos_val;

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

Misc. Functions

• #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))

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

Functions: Naming Conventions

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

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

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

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

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

Function Prototype

returnDataType functionName (argument data types);

int calcTotal ( int, int );

double swap ( int, char, char, double );

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

.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

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

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”;}

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

Function Definition

type functionName ( type var1, type var2, … )

{

// Named constants;

// Variable declarations;

// Other C++ statements;

return variable;

}

Function Header

Function Body

Formal Parameter List

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

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;

}

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

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

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

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;

}

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

Variable Scope

• Global variables

• Local variables

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

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.

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

Function Overloading

• Permits the same function to be defined for different argument data types.

• Second function, same name

• Parameters must be different

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

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;

}

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

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

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

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!