Introduction to Functions
- main is a function
- place code in units based on purpose of code
- Readable code
- functions can be reused
- User defined
- Built-in
Defining a Function
- Place at same "level" as main
- Return answer type
- int, bool, double, ...
- void
- name
- Use same rules for naming variable identifiers
- Optional parameters
- list of variable identifiers separated by commas
- Body of Code
- Any C++ code (except another function definition)
- Template
-
returnType functionName (params)
- {
- FunctionBody
-
}
// This function rounds a double number and
// returns an integer
int round (double number) // number passed by value
{
int answer;
answer = (int) (number + 0.5);
return answer;
}
Calling a Function
- functionName (paramValues); // ONLY for void functions
- var = functionName (paramValues);
- Sample 1
double input;
int grade;
cout << "enter a number to be rounded =>";
cin >> input;
grade = round (input);
cout << "the rounded number is: " << grade;
- Sample 2
double input;
cout << "enter a number to be rounded =>";
cin >> input;
cout << "the rounded number is: " << round (input);
- Complete Program
Rounding Program Code
Output
Function Semantics
- The function call
- Control is transferred from the caller to callee
- Parameters are passed in in-order
- Memory is allocated for local variables (including parameters
passed by value)
- The body of the function is executed
- Function Return
- If it is not a void function, the return value is calculated
and returned to caller
- Memory for local variables is deallocated
- Control is transferred back to the caller
Passing Parameter by Value
- A copy of the value of the actual argument is placed in the memory
location for the formal parameter
- Default type of passing parameters
- The only technique that has been discussed so far
- Value arguments are like local variables, but function gets them
automatically
- Common Mistake:Declaring parameter again inside function
double fee(int hoursWorked, int minutesWorked)
{
int quarterHours; // local variable
int minutesWorked; // NO!
- Choosing Formal Parameter Names
Use a meaningful name within the function
It does not need to be the same name as the calling function
Function Prototypes
- Instead of putting function code before main, put it afterwards
- Cleaner program code - start in main, it should be first
- Need to "declare" the header of the functions that will
appear after main
- Same as function header, EXCEPT a semicolon is needed
- Do not need to provide parameter names, just types
-
Improved Rounding Program Code
Overloading Functions
- Overloading:Giving two or more functions the same name
Some type must be different (return type, parameter type)
- Automatic Type Conversion (bool-to-int, char-to-int, int-to-double)
- Rules for Resolving Overloading
- Exact match
- Match using type conversion
- Default Arguments
Values for actual arguments may be given in the function prototype
These default values are used, it the call doesn't include all
of the parameters (remember: pass in order)
- Sample calls and prototypes
myFunction(3,4,5);
// a is used
myFunction(3.0, 4.0); // c is used
myFunction(11.1, 9, 2); // b is used
myFunction(23.56);
// d is used
- double myFunction(int, int, int).
- int myFunction(double, int, int)
- int myFunction (double, double)
- void myFunction(double)
Math functions
- include <cmath>
- abs(x)
- floor(x) and ceil(x)
- pow(x,y)
- sqrt(x)
- rand()
- srand(seed)
- frequently used with time(0) found in ctime
- max(a, b)
- min(a,b)
Character functions
- include <cctype>
- isupper(c)
- islower(c)
- isdigit(c)
- isspace(c)
- isalpha(c)
- ispunct(c)
- toupper(c)
- tolower(c)
- isupper(c)