13
CHAPTER 6 USER-DEFINED FUNCTIONS I

CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn

Embed Size (px)

Citation preview

Page 1: CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn

CHAPTER 6

USER-DEFINED FUNCTIONS I

Page 2: CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn

In this chapter, you will:•Learn about standard (predefined) functions

and discover how to use them in a program•Learn about user-defined functions•Examine value-returning functions, including

actual and formal parameters•Explore how to construct and use a value-

returning, user-defined function in a program

Page 3: CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn

Functions are like building blocks. They allow complicated programs to be divided into manageable pieces.

Some of the advantages of functions are:

1. While working on one function, you can focus on just that part of the program and construct it, debug it, and perfect it.

2. Different people can work on different functions simultaneously.

3. If a function is needed in more than one place in a program, or in different programs, you can write it once and use it many times.

Functions are often referred to as modules. Functions are like miniature programs. They can be put together to

form a larger program.

Page 4: CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn

Pre-defined Functions Some of the pre-defined mathematical functions are abs(x), sqrt(x), and pow(x,y).

pow(2,3) = 8.0 and pow(2.5,3) = 15.625.

The function pow is of the type double or that the function pow returns a value of the type double.

The square root function, sqrt(x), calculates the non-negative square root of x for x >= 0.0.

sqrt(2.25) is 1.5.

The function sqrt is of the type double and has only one parameter.

Math functions are contained in the header file cmath. #include <cctype>

toupper('a’) ====> ‘A’toupper(’*’) ====> ‘*’toupper(’A’) ====> ‘A’tolower(’A’) ====> ‘a’

Page 5: CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn
Page 6: CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn

USER-DEFINED FUNCTIONS

User-defined functions in C++ Value-returning functions - functions that have a data type. Void functions - functions that do not have a data type.

Page 7: CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn

The function abs might have the following definition:

int abs(int number) // function heading{if(number < 0) // function body { } number = -number;

return number; // value return function } // require a return statement

The variable declared in the heading of the function abs is called the formal parameter of the function abs.

The formal parameter of abs is number. The function return type is int. To invoke (call) the function:

int num;

num = abs(-3); // -3 is the actual parameter

Page 8: CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn

The function “Hello” is a void function::

void Hello(string name) // function heading{ // function body { } cout << “Hello, my name is “ << name;

}

To invoke (call) the function:

string name = “John Smith”;

Hello(name); // name is the actual parameter

Page 9: CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn

• Formal Parameter: A variable declared in the function heading.

• Actual Parameter: A variable or expression listed in a call to a function.

Page 10: CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn

Function Prototype• Function Prototype: Function heading without the body of the

function is called a function prototype.

Syntax: Function Prototype

functionType functionName(parameter list);

• Note the semicolon at the end.

Example:

For the function larger, the function prototype is:

void Hello(string name);

int abs(int number); We can rewrite the function prototype of the function larger as

follows: void Hello(string );

int abs(int );

Page 11: CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn

//Program: Largest of three numbers#include <iostream>using namespace std;double larger(double x, double y);double compareThree(double x, double y, double z);int main() { double one, two; //Line 1 cout<<"Line 2: Larger of 5 and 10 is "

<<larger(5,10)<<endl; //Line 2 cout<<"Line 3: Enter two numbers: "; //Line 3 cin>>one>>two; //Line 4 cout<<endl; //Line 5 cout<<"Line 6: Larger of "<<one<<" and "

<<two<<" is "<<larger(one,two)<<endl;//Line 6

cout<<"Line 7: Largest of 23, 34, and 12 is " <<compareThree(23,34,12)<<endl; //Line 7

return 0;}

Page 12: CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn

double larger(double x, double y){ if(x >= y)

return x; else return y;}

double compareThree (double x, double y, double z){ return larger(x,larger(y,z));}

Sample Run: The user input is in red.Line 2: Larger of 5 and 10 is 10Line 3: Enter two numbers: 25 73Line 6: Larger of 25 and 73 is 73Line 7: Largest of 23, 34, and 12 is 34

Page 13: CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn

• Recall that in a value-returning function the return statement returns the value.

• Consider the following return statement:

return x, y; // only the value y will be returned

• This is a legal return statement.

• You might think that this return statement is returning the values of x and y.

• This is not the case.

• A return statement returns only one value, even if the return statement contains more than one expression.

• If a return statement contains more than one expression, only the value of the last expression is returned.

• Therefore, in the case of the above return statement, the value of y is returned.