programmer-defined functions - lecture 16 sections 6.1 -...

27
Programmer-Defined Functions Lecture 16 Sections 6.1 - 6.4 Robb T. Koether Hampden-Sydney College Wed, Oct 2, 2019 Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 1 / 27

Upload: others

Post on 18-Dec-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

Programmer-Defined FunctionsLecture 16

Sections 6.1 - 6.4

Robb T. Koether

Hampden-Sydney College

Wed, Oct 2, 2019

Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 1 / 27

Page 2: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

1 Programmer-Defined FunctionsThe FormExamples

2 Function Calls

3 Prototypes

4 Examples

5 Header Files

6 Assignment

Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 2 / 27

Page 3: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

Outline

1 Programmer-Defined FunctionsThe FormExamples

2 Function Calls

3 Prototypes

4 Examples

5 Header Files

6 Assignment

Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 3 / 27

Page 4: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

Programmer-Defined Functions

A programmer may create his own functions.If his function is invoked in main(), then

Execution leaves main() (which is itself a function) and goes tobeginning of the function.The function executes its statements from top to bottom or until ithits a return statement, just as in main().When the function is finished, execution returns to its departurepoint in main() and continues.

Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 4 / 27

Page 5: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

Outline

1 Programmer-Defined FunctionsThe FormExamples

2 Function Calls

3 Prototypes

4 Examples

5 Header Files

6 Assignment

Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 5 / 27

Page 6: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

The Form of a Function

The Form of a Functionreturn-type function-name(formal-param-list){

function-bodyreturn return-value;

}

function-name is the name that the programmer chooses for thefunction.The formal-param-list is a list of object types and names (i.e.,declarations) passed to the function as parameters.The function-body is a sequence of C++ statements that willcompute the appropriate value for the returned object.

Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 6 / 27

Page 7: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

The Form of a Function

The Form of a Functionreturn-type function-name(formal-param-list){

function-bodyreturn return-value;

}

return-type is the type of object that the function returns to thecalling function.return-value must be of type return-type.

Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 7 / 27

Page 8: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

The Form of a Function

The Form of a Functionvoid function-name(formal-param-list){

function-bodyreturn // Optional - no value given

}

The return type may be void, in which case the returnstatement is optional.If it is written, then there is no return value specified.

Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 8 / 27

Page 9: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

Outline

1 Programmer-Defined FunctionsThe FormExamples

2 Function Calls

3 Prototypes

4 Examples

5 Header Files

6 Assignment

Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 9 / 27

Page 10: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

Example of a Function

Function Definitionfloat average3(float a, float b, float c){

float avg = (a + b + c)/3.0f;return avg;

}

This function will return the average of three numbers.

Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 10 / 27

Page 11: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

Example of a Function

Function Definitionfloat average3(float a, float b, float c){

return (a + b + c)/3.0f;}

It is legal to return the value of an expression.

Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 11 / 27

Page 12: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

Example of a Function

Function Definitionbool isOdd(int n){

return n % 2 == 1;}

This function will return the average of three numbers.

Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 12 / 27

Page 13: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

Outline

1 Programmer-Defined FunctionsThe FormExamples

2 Function Calls

3 Prototypes

4 Examples

5 Header Files

6 Assignment

Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 13 / 27

Page 14: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

Function Calls

Function Callfunction-name(actual-param-list)

To call a function, weName the function, andGive the actual-param-list (a list of actual objects or expressions).

A function call occurs in an expression.If the function is void type, then the function call is a completestatement and must occur on a line by itself.If the function is not void type, then the function call occurs withinan expression where an object of the function’s return type ispermitted.

Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 14 / 27

Page 15: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

Function Calls

If the function is void type, then the function call is a completestatement and must occur on a line by itself.If the function is not void type, then the function call occurs withinan expression where an object of the function’s return type ispermitted.

Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 15 / 27

Page 16: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

Function Usage

Data types are not specified in the actual parameter list.Actual vs. formal parameters

Types must match, or elseThere must be a conversion rule to convert the actual type into theformal type, the same as with assignment statements.

Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 16 / 27

Page 17: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

Example of Function Usage

Function Usagecout << "Enter the three grades: ";float grade1, grade2, grade3;cin >> grade1 >> grade2 >> grade3;float avg_grade = average3(grade1, grade2, grade3)cout << avg_grade << endl;

This program finds the average of three grades.

Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 17 / 27

Page 18: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

Example of Function Usage

Function Usagecout << "Enter the three grades: ";float grade1, grade2, grade3;cin >> grade1 >> grade2 >> grade3;cout << average3(grade1, grade2, grade3) << endl;

The function call may be placed in the output statement, althoughthis can make the code harder to read.

Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 18 / 27

Page 19: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

Outline

1 Programmer-Defined FunctionsThe FormExamples

2 Function Calls

3 Prototypes

4 Examples

5 Header Files

6 Assignment

Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 19 / 27

Page 20: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

Prototypes

Before the compiler can check and compile a function call, it musthave already seen the function’s prototype.Typically, we place the prototypes of all of the functions beforemain().Then we are free to use them anywhere in the program.

Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 20 / 27

Page 21: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

Example of a Prototype

Function Prototypefloat average3(float a, float b, float c);

int main(){...

float avg = average3(x, y, z)...

}

float average3(float a, float b, float c){return (a + b + c)/3.0f;}

The prototype of the average() function.Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 21 / 27

Page 22: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

Outline

1 Programmer-Defined FunctionsThe FormExamples

2 Function Calls

3 Prototypes

4 Examples

5 Header Files

6 Assignment

Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 22 / 27

Page 23: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

Examples

Create and runAverage3.cppHypotenuse.cppSquareRoot.cpp

Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 23 / 27

Page 24: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

Outline

1 Programmer-Defined FunctionsThe FormExamples

2 Function Calls

3 Prototypes

4 Examples

5 Header Files

6 Assignment

Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 24 / 27

Page 25: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

Example of Header File

ExampleisVowelFunc.cpp, isvowel.cpp, isvowel.h

Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 25 / 27

Page 26: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

Outline

1 Programmer-Defined FunctionsThe FormExamples

2 Function Calls

3 Prototypes

4 Examples

5 Header Files

6 Assignment

Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 26 / 27

Page 27: Programmer-Defined Functions - Lecture 16 Sections 6.1 - 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019...1 Programmer-Defined Functions The Form Examples 2 Function Calls

Assignment

AssignmentRead Sections 6.1 - 6.4.

Robb T. Koether (Hampden-Sydney College) Programmer-Defined Functions Wed, Oct 2, 2019 27 / 27