functions cis 230 01-feb-06. summary slide using functions mathematical functions misc. functions...
TRANSCRIPT
Functions
CIS 230
01-Feb-06
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
Summary Slide (cont.)
• Variable Declaration
• Variable Scope
• Pass by Value
• Function Overloading
• Default Parameter Values
Using Functions
• Name of the function
• What it does
• Type of data required
• Type of data returned
• Library required
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
Mathematical Functions
• #include <cmath>
Mathematical Functions
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
Mathematical Functions
• Storing returned Data:
int pos_val;
pos_val = abs(-4);
cout << pos_val;
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))
Functions: Naming Conventions
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
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
Function Prototype
returnDataType functionName (argument data types);
int calcTotal ( int, int );
double swap ( int, char, char, double );
.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
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”;}
Function Definition
type functionName ( type var1, type var2, … )
{
// Named constants;
// Variable declarations;
// Other C++ statements;
return variable;
}
Function Header
Function Body
Formal Parameter List
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;
}
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
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;
}
Variable Scope
• Global variables
• Local variables
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.
Function Overloading
• Permits the same function to be defined for different argument data types.
• Second function, same name
• Parameters must be different
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;
}
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);
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!