cmpt 128: introduction to computing science for engineering students
Post on 02-Jan-2016
25 Views
Preview:
DESCRIPTION
TRANSCRIPT
1 © Janice Regan, CMPT 128, Jan 2007
CMPT 128: Introduction to Computing Science for Engineering Students
Functions (2)
2© Janice Regan, CMPT 128, 2007-2012
3 steps: User defined functions Function Declaration/prototype
Information than the compiler needs to properly interpret calls to the function
Function Definition Actual implementation of the function
Function Call Using the function in the calling function
3© Janice Regan, CMPT 128, 2007-2012
Declaring a user’s function A function prototype tells us (and the compiler)
the name and type of the function The identifier of the function The type of each parameter The identifier of each parameter (optional) The order of the parameters
The function prototype is usually placed after the #include statements before the int main () statement The function can then be called by any other functions
4© Janice Regan, CMPT 128, 2007-2012
Writing a function prototype The function declaration or function prototype always
has a similar form. <return type> <function identifier> ( <parameter list >);
int myMean ( int a, int b, int c, int d);
double sumOfSquares ( double in1, double in2);
bool isReady ( int , double , int ); // In a prototype identifiers in the parameter list are optional
5© Janice Regan, CMPT 128, 2007-2012
Understanding the prototype (1)
The function declaration or function prototype always has a similar form.
<return type> <function identifier> ( <parameter list >); The function identifier indicates the name of the function The return type indicates the type of the value returned by
the function When the function is called the statement calling the function can
be considered to be an expression The value of this expression (the call) in the calling function has a
type, return type
6© Janice Regan, CMPT 128, 2007-2012
Understanding the prototype (2)
The function declaration or function prototype always has a similar form.
<return type> <function identifier> ( <parameter list >);
A series of parameters (or arguments) follow the function identifier. These parameters indicate the data that is supplied to (and
sometimes under special conditions returned from) the function Each parameter is of a particular data type that is specified within
the declaration
7© Janice Regan, CMPT 128, 2007-2012
Understanding the prototype (3)
The function declaration or function prototype always has a similar form.
<return type> <function identifier> ( <parameter list >); each parameter in the parameter list includes a type and an
optional identifier The function can have any number of parameters Each parameter of the function can have any type Different parameters can have different types Order of the parameters is important
Parameters in the declaration, definition, and call must always be in the same order
8© Janice Regan, CMPT 128, 2007-2012
Prototypes: an example As an example consider
double sinc ( double x ); OR double sinc ( double );
The identifier (name) of the function is sinc The function returns a value of type double to the calling function
The value of the function call, the expression sinc(inputvalue) in the calling function is of type double
The parameter list contains a single parameter of type double An identfier for the parameter may be given (optional)
9© Janice Regan, CMPT 128, 2007-2012
3 steps: User defined functions Function Declaration/prototype
Information than the compiler needs to properly interpret calls to the function
Function Definition Actual implementation of the function
Function Call Using the function in the calling function
10© Janice Regan, CMPT 128, 2007-2012
Function Definition Two main parts
Function Head Gives the compiler information to match the function with
the function declaration
Function Body Actual C++ statements implementing the function
Function definitions are placed after or before the main function (not inside the main function)
11© Janice Regan, CMPT 128, 2007-2012
Placement of Function Definition
Int main (){ // body of main function}
double sinc(double x) // function head{
//function body}
12© Janice Regan, CMPT 128, 2007-2012
Sample Function Definitiondouble sinc(double x)
{
if (fabs(x) < 0.0001)
{
return(1.0);
}
else
{
return( sin(x)/x);
}
}
Function head
Function body
Function call to library function sin
13© Janice Regan, CMPT 128, 2007-2012
Function Head (examples) The first line of a function definition is the
function head
The head for the main function is int main ( )
The head for our example function is double sinc (double x)
There is no ; at the end of a function head
14
The function head indicates the type of the function double sinc( double x )
The function head indicates a function name or identifier double sinc( double x )
The function head indicates a list of parameters for the function, each parameter includes a type and an identifier
double sinc (double x)© Janice Regan, CMPT 128, 2007-2012
Function Head (content)
15© Janice Regan, CMPT 128, 2007-2012
Function head: formal parameters The parameters in the function head are
referred to as formal parameters The function has 0 or more formal parameters
double sinc( double x ) Each of a function’s formal parameters have types
double sinc( double x ) A function may have formal parameters of more than
one type Each parameter must be given its own type Multiple parameters are separated by commas int sample( double x, int y, char z, double a)
16© Janice Regan, CMPT 128, 2007-2012
The body of a function After the function head the body of the function is
enclosed in {}
double sinc(double x){ //variable declarations double y; //declaring variable //for return value //calculations to determine y return(y);}
Function body
17© Janice Regan, CMPT 128, 2007-2012
Parts of the function body Local variables are declared Calculate the return value of the function Return the value to the calling function
return (returnValue); In a more complicated function you must assure all
possible paths to completion of a function end with a return statement
For a void function return statements are not required Return statements without arguments may be used to return
from the function to the calling program
18© Janice Regan, CMPT 128, 2007-2012
3 steps: User defined functions Function Declaration/prototype
Information than the compiler needs to properly interpret calls to the function
Function Definition Actual implementation of the function
Function Call Using the function in the calling function
© Janice Regan, CMPT 128, 2007-2012 19
3 steps: User defined functions Function Declaration/prototype
Information than the compiler needs to properly interpret calls to the function
Function Definition Actual implementation of the function
Function Call Using the function in the calling function
© Janice Regan, CMPT 128, 2007-2012 20
Calling a function float fabs(float x); … limitValue = fabs(-9.7);
fabs(-9.7) is an expression known as a function call, or function invocation
The arguments in the brackets ( ) of a function call are called actual arguments or actual parameters.
An actual argument in a function call can be A literal (like -9.7) any variable whose value is of the correct type any expression whose value is of the correct type
© Janice Regan, CMPT 128, 2007-2012 21
Calling a function Consider a void function (returns no value) A function call to a void function does not have a value
(a void function does not return a value) A function call to a void function cannot be used in an
expression
Consider calling a function that is not void A function call to a non-void function has a value so it
can be used as part of a more complicated expressions bonus = fabs(mylimit) * myfactor; A function call to a function of any non void type is allowed
wherever it’s legal to use an expression
© Janice Regan, CMPT 128, 2007-2012 22
Returning a function’s value (1)
A non-void function will take the supplied values of the actual parameters, calculate a result, then return that result to the calling program
The function has a type. The type of the function is the type of the value returned by that function to the calling program
A function is invoked using a function call, the function call expression is given the value that is returned by the function
© Janice Regan, CMPT 128, 2007-2012 23
Sample Function Definitiondouble sinc(double x)
{
if (fabs(x) < 0.0001)
{
return(1.0);
}
else
{
return( sin(x)/x);
}
}
Function head
Function body
Function call to library function sin
© Janice Regan, CMPT 128, 2007-2012 24
Using our sample function // declare functions used
double sinc(double x);
int main (void){
// declare variables double a, b;
// obtain input data a, call function, print resultscout << “ enter value for which sinc is to be determined “);cin >> a;b= sinc(a);cout << "sinc( " << a << " ) = " << b;
}
Function prototype or function declaration
Function call
© Janice Regan, CMPT 128, 2007-2012 25
Returning a function’s value (2)
Our sample function determines the value of sin(x)/x when we supply a value for the parameter x
The function sinc(x) will take the value of x, calculate the value of sin(x)/x and return the resulting value to the calling program
To return the value of the function to the calling program following command is used
return(ValueToBeReturned); The type of variable or expression ValueToBeReturned
should match the type of the function returning the value A function of any type other than void must contain at least
one return statement. It may contain more. There must be 1 return statement ending each flow of control through the function
© Janice Regan, CMPT 128, 2007-2012 26
Calling Void Functions For a function declared as
void showResults(double x, double y); Can call the function in a calling function as follows
showResults(degreesF, degreesC); showResults(32.5, 0.3);
A function call to a void function does not have a value, because a void function does not return a value. A = showResults(32.5,0.3) * 3.0; //this is an invalid statement A function call to a void function has no value to be used in an
arithmetic expression ( or any other kind of expression) A function call to a void function cannot be assigned to a variable
since it has no value to place in that variable
top related