lecture 12: functions professor: dr. miguel alonso jr. fall 2008 cgs2423/cop1220

16
Lecture 12: Functions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220

Upload: maryann-nelson

Post on 06-Jan-2018

224 views

Category:

Documents


0 download

DESCRIPTION

Defining and Calling Functions Concept: A function call is a statement that causes a function to execute. A function definition contains the statements that make up the function A function has the following parts Return type: Name: Parameter list: Can be empty Body: int main(int arg1) { cout

TRANSCRIPT

Lecture 12: Functions

Professor: Dr. Miguel Alonso Jr.Fall 2008

CGS2423/COP1220

Modular Programming

Concept: A program may be broken up into manageable functions, or groups of statements

int main(){

statement;statement;statement;statement;statement;statement;statement;statement;statement;

}

int main(){

statement;statement;statement;

}

void function2(){

statement;statement;statement;

}

int function3(){

statement;statement;statement;

}

Defining and Calling Functions

Concept: A function call is a statement that causes a function to execute. A function definition contains the statements that make up the function

A function has the following parts Return type: Name: Parameter list:

Can be empty Body:

int main(int arg1){

cout<<“Hello World\n”;return 0;

}

Void Functions and Calling

Void functions do not return anything void myFunction(){cout<<“I don’t return anything;}

Function header void myFunction()

Function Call myFunction();

Examples// This program has two functions: main and displayMessage#include <iostream>using namespace std;

//*****************************************// Definition of function displayMessage *// This function displays a greeting. *//*****************************************

void displayMessage(){ cout << "Hello from the function displayMessage.\n";}

//*****************************************// Function main *//*****************************************

int main(){ cout << "Hello from main.\n"; displayMessage(); cout << "Back in function main again.\n"; return 0;}

// The function displayMessage is repeatedly called from a loop.#include <iostream>using namespace std;

//*****************************************// Definition of function displayMessage *// This function displays a greeting. *//*****************************************

void displayMessage(){ cout << "Hello from the function displayMessage.\n";}

//*****************************************// Function main *//*****************************************

int main(){ cout << "Hello from main.\n"; for (int count = 0; count < 5; count++) displayMessage(); // Call displayMessage cout << "Back in function main again.\n"; return 0;}

// This program has three functions: main, first, and second.#include <iostream>using namespace std;

//*****************************************// Definition of function first *// This function displays a message. *//*****************************************

void first(){ cout << "I am now inside the function first.\n";}

//*****************************************// Definition of function second *// This function displays a message. *//*****************************************

void second(){ cout << "I am now inside the function second.\n";}

//*****************************************// Function main *//*****************************************

int main(){ cout << "I am starting in function main.\n"; first(); // Call function first second(); // Call function second cout << "Back in function main again.\n"; return 0;}

// This program has three functions: main, deep, and deeper#include <iostream>using namespace std;

//*****************************************// Definition of function deeper *// This function displays a message. *//*****************************************

void deeper(){ cout << "I am now inside the function deeper.\n";}

//*****************************************// Definition of function deep *// This function displays a message. *//*****************************************

void deep(){ cout << "I am now inside the function deep.\n"; deeper(); // Call function deeper cout << "Now I am back in deep.\n";}

//*****************************************// Function main *//*****************************************

int main(){ cout << "I am starting in function main.\n"; deep(); // Call function deep cout << "Back in function main again.\n"; return 0;}

Function Prototypes

Concept: A function prototype eliminates the need to place a function definition before all calls to the function.

Function prototype(declaration) is placed before the call, and the function itself is defined after the call Similar to the function header, except for the

semicolon void displayMessage();

// This program has three functions: main, First, and Second.#include <iostream>using namespace std;

// Function Prototypesvoid first();void second();

int main(){ cout << "I am starting in function main.\n"; first(); // Call function first second(); // Call function second cout << "Back in function main again.\n"; return 0;}

//*************************************// Definition of function first. *// This function displays a message. *//*************************************

void first(){ cout << "I am now inside the function first.\n";}

//*************************************// Definition of function second. *// This function displays a message. *//*************************************

void second(){ cout << "I am now inside the function second.\n";}

Sending Data into a function

Concept: When a function is called, the program may send values into the function

These values are called arguments Example: result = pow(2.0,4.0);

Parameter: a special variable to hold the arguments as they are passed into the function void displayValue(int num); num is the parameter

// This program demonstrates a function with a parameter.#include <iostream>using namespace std;

// Function Prototypevoid displayValue(int);

int main(){ cout << "I am passing 5 to displayValue.\n"; displayValue(5); // Call displayValue with argument 5 cout << "Now I am back in main.\n"; return 0;}

//*********************************************************// Definition of function displayValue. *// It uses an integer parameter whose value is displayed. *//*********************************************************

void displayValue(int num){ cout << "The value is " << num << endl;}

// This program demonstrates a function with a parameter.#include <iostream>using namespace std;

// Function Prototypevoid displayValue(int);

int main(){ cout << "I am passing several values to displayValue.\n"; displayValue(5); // Call displayValue with argument 5 displayValue(10); // Call displayValue with argument 10 displayValue(2); // Call displayValue with argument 2 displayValue(16); // Call displayValue with argument 16 cout << "Now I am back in main.\n"; return 0;}

//*********************************************************// Definition of function displayValue. *// It uses an integer parameter whose value is displayed. *//*********************************************************

void displayValue(int num){ cout << "The value is " << num << endl;}

// This program demonstrates a function with three parameters.#include <iostream>using namespace std;

// Function Prototypevoid showSum(int, int, int);

int main(){ int value1, value2, value3; // Get three integers. cout << "Enter three integers and I will display "; cout << "their sum: "; cin >> value1 >> value2 >> value3; // Call showSum passing three arguments. showSum(value1, value2, value3); return 0;}

//************************************************************// Definition of function showSum. *// It uses three integer parameters. Their sum is displayed. *//************************************************************

void showSum(int num1, int num2, int num3){ cout << (num1 + num2 + num3) << endl;}

Passing Data By Value When an argument

is passed into a parameter, only a copy of the argument’s value is passed. Changes to the parameter do not affect the original argument.

Using Functions in a Menu-Driven Program

The return Statement

Concept: The return statement causes a function to end immediately.

Returning a Value from a Function

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

Return Type

int main(int arg1){

cout<<“Hello World\n”;return 0;

}

int sum(int num1, int num2){

int result;result = num1 + num2;return result;

}

// This program uses a function that returns a value.#include <iostream>using namespace std;

// Function prototypeint sum(int, int);

int main(){ int value1 = 20, // The first value value2 = 40, // The second value total; // To hold the total

// Call the sum function, passing the contents of // value1 and value2 as arguments. Assign the return // value to the total variable. total = sum(value1, value2); // Display the sum of the values. cout << "The sum of " << value1 << " and " << value2 << " is " << total << endl; return 0;}

//*****************************************************// Definition of function sum. This function returns *// the sum of its two parameters. *//*****************************************************

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