Top Banner
Chapter 6: Function s
43

Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Dec 24, 2015

Download

Documents

Brian Garrison
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Chapter 6:

Functions

Page 2: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Outline

• What is “Function”• Sending data into function• The return statement• Local and Global variables

Page 3: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 3

Modular Programming• Modular programming: breaking a program up into

smaller, manageable functions or modules

• Function: a collection of statements to perform a task

• Motivation for modular programming:– Improves maintainability of programs– Simplifies the process of writing programs– In summary: Modular programs are easier to develop,

modify and maintain than programs constructed otherwise

Page 4: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 4

Page 5: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

main() – driver function

• To provide for the orderly placement and execution of functions, each C++ program must have one and only one function named main(). The main() function is referred to as a driver function because it directs or "drives" the other modules or functions in the order in which they are to be executed

Page 6: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 8

Function Definition

Page 7: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 9

Function Return Type

• If a function returns a value, the type of the value must be indicated:int main()

• If a function does not return a value, its return type is void:void printHeading(){

cout << "Monthly Sales\n";}

Page 8: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Function Return Type• The return statement can then be eliminated from

the main ( ) function. The structure of main( ) can be as follows:

void main( ){ //program statements go here;}

Page 9: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.
Page 10: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.
Page 11: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Function example#include<iostream>using namespace std;

/*********************//define a function **********************/void display_message(){

cout<< "Hello from the function displaymessage.\n";}

/*********************//main function **********************/

void main(){

cout<<"hello world!! from main function.\n";display_message();

}

Page 12: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.
Page 13: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 15

Calling a Functionvoid printHeading(){

cout << "Monthly Sales\n";}

• To call a function, use the function name followed by ()and ;printHeading();

• When called, program executes the body of the called function

• After the function terminates, execution resumes in the calling function at point of call.

Page 14: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 17

Function Prototypes

• Ways to notify the compiler about a function before a call to the function:

1. Place function definition before calling function’s definition

or

2. Use a function prototype (function declaration) – like the function definition without the body

• Header: void printHeading()• Prototype: void printHeading();

Page 15: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 18

(Program Continues)

Page 16: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 19

Page 17: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Outline

• What is “Function”• Sending data into function• The return statement• Local and Global variables

Page 18: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 22

Sending Data into a Function• Can pass values into a function at time of call:

displayValue(5); // function call

• Values (data) passed to function are arguments• Variables in a function that hold the values passed as

arguments are parameters

void displayValue(int num)

{

cout << "The value is " << num << endl;

}

The integer variable num is a parameter. It accepts any integer value passed to the function.

Page 19: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 23

What is the output?

Page 20: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 24

The function call in line 11 passes the value 5as an argument to the function.

Page 21: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 25

Parameters, Prototypes, and Function Headers

• For each function argument,– the prototype must include the data type of each

parameter inside its parentheses– the header must include a declaration for each

parameter in its ()void evenOrOdd(int); //prototypevoid evenOrOdd(int num) //headerevenOrOdd(val); //call

Page 22: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 26

Passing Multiple Arguments

When calling a function and passing multiple arguments:

– the number of arguments in the call must match the prototype and definition

– the first argument will be used to initialize the first parameter, the second argument to initialize the second parameter, etc.

Page 23: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 27

Page 24: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 28

The function call in line 18 passes value1, value2, and value3 as arguments to the function.

Page 25: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 30

Passing Data by Value• Example: int val=5;

evenOrOdd(val);

int evenOrOdd (int num){ … num = num+10; …}

• evenOrOdd can change variable num, but it will have no effect on variable val

5val

argument incalling function

5num

parameter inevenOrOdd function

Page 26: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Outline

• What is “Function”• Sending data into function• The return statement• Local and Global variables

Page 27: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 32

The return Statement

The RETURN statement has two purposes:

• The return statement causes a function to END immediately

• A function may send ONE value back to the part of the program that called the function

Page 28: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 33

The return Statement

• Used to end execution of a function• Can be placed anywhere in a function– Statements that follow the return statement will

not be executed

• Can be used to prevent abnormal termination of program

• In a void function without a return

statement, the function ends at its last }

Page 29: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 34

(Program Continues)

Page 30: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 35

Program 6-11(Continued)

Page 31: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 36

Returning a Value From a Function• A function can return a value back to the statement that

called the function.• A function returning a value must specify, in its header

line, the data type of the value that will be returned. • In a value-returning function, the return statement can

be used to return a value from function to the point of call. Example:

int sum(int num1, int num2){ int result; result = num1 + num2; return result;}

Page 32: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 37

A Value-Returning Function

int sum(int num1, int num2){ double result; result = num1 + num2; return result;}

Return Type – Incorrect

Value Being Returned

Page 33: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 38

A Value-Returning Function

double sum(int num1, int num2){ double result; result = num1 + num2; return result;}

Return Type – Correction – Depending on design of code

Value Being Returned

Page 34: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 39

A Value-Returning Function

int sum(int num1, int num2){ int result; result = num1 + num2; return result;}

Return Type – Correction – Depending on design of code

Value Being Returned

Failure to match the return value exactly with the function's declared data type may not result in an error when the program is compiled, but it may lead to undesired results because the return value is always converted to the data type declared in the function declaration

Page 35: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Calling function to receive the value

• On the receiving side, the called function must:– be alerted to the type of value to expect– properly use the returned value

• provide a variable to store the value – accomplished by using a standard assignment statement, e.g.:

total = sum (firstnum, secondnum);

• use the value directly in an expression, e.g.:cout << sum(firstnum, secondnum);

The next program illustrates the inclusion of both the prototype and assignment statements for main( ) to correctly call and store a returned value from sum( ).

Page 36: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

int findSum(int, int); //the function prototypeint main ( void ){ int firstnum, secondnum, total;

cout << "Please enter your first number to be added: "; cin >> firstnum; cout << "Good Job!! Please enter your second number to be added: "; cin >> secondnum;

total = findSum(firstnum, secondnum); //the function is called here

cout << "The sum of the two numbers you have entered is " << total;

return 0;}

int findSum(int number1, int number2) //function header line{ //start of function body int sum; //variable declaration sum = number1 + number2; //calculates the sum return sum; //return statement }

Page 37: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

//function prototypesdouble inputFahrenheit( ); //reads fahrenheit temp from userdouble convertFahenheitToCelsius(double); //converts fahren temp to celsiusvoid displayConvertedDegree (double, double); //displays converted temperature

int main ( void ) { //Declaration statements double fahrenheit = 0.0; //stores the temp in fahren entered by user double celsius = 0.0; //stores the calculated converted temp in celsius

fahrenheit = inputFahrenheit( ); //function call to get data from user celsius = convertFahenheitToCelsius(fahrenheit);//function call-convert F˚ to celsius displayConvertedDegree(celsius, fahrenheit); //function call -display converted temp return 0;}/* This function allows the user to input the temperature in Fahrenheit that is to be converted to Celsius */ double inputFahrenheit( ){ double f; cout << "Please enter the temperature recorded in Fahreheit " << endl << "that you wish to be converted to

Celsius: "; cin >> f; return f;}

/* This function converts Fahrenheit to Celsius */double convertFahenheitToCelsius(double f_degrees){ return (5.0 / 9.0 ) * ( f_degrees - 32.0 ); }/*This function displays Celsius equivalent of the temperture that was entered in Fahrenheit */void displayConvertedDegree(double cel, double fahren){ cout << fahren << " Fahrenheit is " << cel << " Celsius. " << endl;}

Page 38: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

• Output:Please enter the temperature recorded in Fahreheit that you wish to be converted to Celsius: 3232 Fahrenheit is 0 Celsius.

• This program consists of 4 functionsmain( )inputFahrenheitToCelsius( )convertFahrenheitToCelsius( )displayConvertedDegree( )

Page 39: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Outline

• What is “Function”• Sending data into function• The return statement• Local and Global variables

Page 40: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 52

Local Variables

Page 41: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 53

When the program is executing in main, the num variable defined in main is visible. When anotherFunction is called, however, only variables defined inside it are visible, so the num variable in main is hidden.

Page 42: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 55

Global Variables• A global variable is any variable defined outside

all the functions in a program.

• The scope of a global variable is the portion of the program from the variable definition to the end.

• This means that a global variable can be accessed by all functions that are defined after the global variable is defined.

• Constant variable is a good example for global variable

Page 43: Chapter 6: Functions. Outline What is “Function” Sending data into function The return statement Local and Global variables.

Slide 6- 56

Global Variables• You should avoid using global variables because they make

programs difficult to debug.

– Global variables allow the programmer to "jump around" the normal safeguards provided by functions. Rather than passing variables to a function, it is possible to make all variables global ones. DO NOT DO THIS. By making all variables global, you instantly destroy the safeguards that C++ provides to make functions independent and insulated from each other, including the necessity of carefully designating the type of parameters a function needs, the variables used in the function, and the value returned. Using only global variables can be especially disastrous in larger programs that have many functions. A global variable can be accessed and changed by any function following the global declaration which makes it time-consuming and frustrating to locate the origin of an erroneous value in a large program