Title: Functions
1Functions
- Computer Programming I
- Dr. Tim Margush
- Mathematics, Science and Technology Department
- Farquhar Center - Nova Southeastern University
2What is a Function?
- A function is a program unit representing the
abstraction of some task - Standard functions are found in the libraries
included with most compilers - Programmers are free to define their own
functions to perform specialized tasks - Functions are used via a call-return mechanism
3Characteristics of a Function
- Declared in a function heading (prototype)
- double fabs(double) //usually in a header file
- Defined in a C or C source file
- May be written in another language
- Standard functions are precompiled and linked
with your application - Stored in a object file or library
- Source file is often not available
4Declaring and Using a Function
- Functions must be declared before they are used
in a program - Declare via a function heading (prototype)
- Declare via function definition
- Functions are invoked using the function
identifier and the function-call operator - x ceil(y)
- showMoney(amount .90)
5Function Heading or Prototype
- Declares an identifier as a function of a
particular type and establishes its signature - The signature of a function is its name and the
list of types of its formal parameters - string toUpper(string)
- double cubeRoot(double)
- float newPrice(float oldPrice, float discount)
- void showHex(double)
- void showHex(long)
6Function Definition
- A function is defined by repeating the function
heading and... - Naming each formal parameter
- Providing a block to be executed when the
function is called - Function definitions can be in the same source
file, or in a separately compiled file
7Sample Declaration, Definition, and Function Call
- long round(double) //rounds to nearest integer
- ...
- long balance round(amount) //inside a
function body - ...
- long round(double x)//rounds to nearest integer
- double rounded_x floor(x0.5)
- return long(rounded_x) //cast to long
8Standard Mathematical Functions
- Located in the cmath library
- include ltcmathgt //Math header file
- This brings all of the function headings into
your file as part of the source program - This declares (but does not define) the functions
- You must know the number and types of arguments
required, and the return type of every function
you use - Consult help files, or browse the header file
9cmath Header File
- // cmath standard header
- if _MSC_VER gt 1000
- pragma once
- endif
- ifndef _CMATH_
- define _CMATH_
- ifdef _STD_USING
- undef _STD_USING
- include ltmath.hgt
- define _STD_USING
- else
- include ltmath.hgt
- endif / _STD_USING /
- endif / _CMATH_ /
- /
- math.h - definitions and declarations for math
library -
- _CRTIMP double __cdecl pow(double, double)
- _CRTIMP double __cdecl sin(double)
- _CRTIMP double __cdecl sinh(double)
- _CRTIMP double __cdecl tan(double)
- _CRTIMP double __cdecl tanh(double)
- _CRTIMP double __cdecl sqrt(double)
10Pre/Post-conditions
- What must be true when a function call occurs
- What arguments are expected
- Number, types, and values
- What is true when the function call is complete
(assuming preconditions were met) - Changes to the program state
- Return value
11Internal Documentation
- Each function should be documented
- Explain the purpose of function
- Give an overview of what it does
- Describe preconditions
- Explain what arguments are expected, etc
- Describe postconditions
- Tell what is changed by the execution of the
function
12External Documentation
- Gives more detailed information about a function
- MSDN Library
- Program specifications
- User guide
- Sqrt Calculates the square root. double sqrt(
double x ) - Routine Required Header Compatibilitysqrt
ltmath.hgt ANSI, Win 95, Win
NT - Return Value
- The sqrt function returns the square-root of x.
If x is negative, sqrt returns an indefinite
(same as a quiet NaN). You can modify error
handling with _matherr. - Parameter x Nonnegative floating-point value
- Example
- ...
13Return Types
- Functions that represent a value must be defined
to have the proper return type - int ageNextYear(int) //return arg1
- Functions that represent an action (but not a
value) should have a return type of void - void clearScreen(long) //clear screen to color
specified in arg
14Void or Not Void
- Functions that return values are usually used in
expressions - cout ltlt "New age " ltlt ageNextYear(myAge)
- double dist sqrt(pow(a,2)pow(b,2))
- Functions that do not return values are usually
statements of their own - clearScreen(0x3C3FC2)
15Common Errors
- Forgetting the function call operator
- clearScreen //does nothing
- Forgetting about the return value
- sqrt(4) //computes 2.0 but does nothing with it
- Forgetting an argument
- clearScreen() //will not compile
- Using the wrong type of argument
- sqrt("Tim") //will not compile
16Argument - Parameter Correspondence
- Function calls must include the required number
of arguments (in parenthesis) - Arguments must match the number and types of the
parameters - Parameters are declared in the function
definition (in the heading) - Parameters are variables/objects that are
initialized according to the arguments specified
in the function call
17Function Example
- include ltiostreamgt
- include ltstringgt
- using namespace std
- void display(string, int)
- int main()
- const string me "Tim"
- const int age 46
- display(me, age)
- display("Phil", age9)
- display("Dave", age11)
- return 0
-
- void display(string Name,
- int anAge)
- cout ltlt Name ltlt " is "
- cout ltlt anAge ltlt " years."
- cout ltlt endl
- return
-
- Tim is 46 years.
- Phil is 55 years.
- Dave is 57 years.
18Functions With No Parameters
- Use the void specifier in place of the parameter
list - int myAge(void) rather than int myAge()
- Be sure to include the function call operator
when calling such a function - cout ltlt myAge() not cout ltlt myAge