lecture11 Functions

# lecture11 Functions - CMPT 128 Introduction to Computing...

This preview shows pages 1–9. Sign up to view the full content.

1 © Janice Regan, CMPT 128, Jan 2007 CMPT 128: Introduction to Computing Science for Engineering Students Functions (2)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
© Janice Regan, CMPT 128, Jan 2007 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
© Janice Regan, CMPT 128, Jan 2007 3 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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
© Janice Regan, CMPT 128, Jan 2007 4 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, Jan 2007 5 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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
© Janice Regan, CMPT 128, Jan 2007 6 Sample Function Definition double sinc(double x) { } Function head Function body Function call to library function sin
© Janice Regan, CMPT 128, Jan 2007 7 Using our sample function 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 “); cout << "sinc( " << a << " ) = " << b; } Function prototype or function declaration Function call

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
© Janice Regan, CMPT 128, Jan 2007 8 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
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 05/18/2010 for the course CMPT 128 taught by Professor Regan during the Spring '08 term at Simon Fraser.

### Page1 / 28

lecture11 Functions - CMPT 128 Introduction to Computing...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online