260 likes | 352 Views
CMPT 128: Introduction to Computing Science for Engineering Students. Functions (2). 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
E N D
CMPT 128: Introduction to Computing Science for Engineering Students Functions (2)
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
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
Writing a function prototype • The function declaration or function prototype always has a similar form. <return type> <function identifier> ( <parameter list >); intmyMean ( int a, int b, int c, int d); double sumOfSquares( double in1, double in2); boolisReady ( int , double , int ); // In a prototype identifiers in the parameter list are optional
Understanding the prototype (1) • The function declaration orfunction 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
Understanding the prototype (2) • The function declaration orfunction 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
Understanding the prototype (3) • The function declaration orfunction 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
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)
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
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)
Placement of Function Definition Int main () { // body of main function } double sinc(double x) // function head { //function body }
Sample Function Definition Function head double sinc(double x) { if (fabs(x) < 0.0001) { return(1.0); } else { return( sin(x)/x); } } Function body Function call to library function sin
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
Function Head (content) • The function head indicates the type of the function • doublesinc( 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)
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)
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
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
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 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
Calling a function float fabs(float x); … limitValue = fabs(-9.7); • fabs(-9.7) is anexpression 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
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
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
Sample Function Definition Function head double sinc(double x) { if (fabs(x) < 0.0001) { return(1.0); } else { return( sin(x)/x); } } Function body Function call to library function sin
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 results cout << “ 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
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
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