Modular Programming – User Defined Functions. CSCE 1062 Outline Modular programming – user...

15
Modular Programming – User Defined Functions

description

CSCE 1063 General Functions’ Guidelines To use any function in general, you need to:  include the correct header file  know the name of the function  know the number of parameters/arguments, if any  know the data type of each parameter  know the data type of the value computed by the function, called the type of the function A value-returning function is either used in an assignment statement or in an output statement.

Transcript of Modular Programming – User Defined Functions. CSCE 1062 Outline Modular programming – user...

Page 1: Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.

Modular Programming – User Defined Functions

Page 2: Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.

CSCE 106 2

Outline Modular programming – user defined

functions Value returning functions return statement

Page 3: Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.

CSCE 106 3

General Functions’ GuidelinesTo use any function in general, you need to:

include the correct header file know the name of the function know the number of parameters/arguments, if any know the data type of each parameter know the data type of the value computed by the

function, called the type of the function

A value-returning function is either used in an assignment statement or in an output statement.

Page 4: Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.

CSCE 106 4

User-Defined FunctionsYou can define two types of functions:

1. “void” function that does not return a value (and does not have a data type).

2. Value-returning function that has a data type.

A function is defined by writing its heading and body.void drawCircle(){ cout << “ * “ << endl; cout << “* *“ << endl; cout << “ * * “ << endl;}

Function nameFunctiontype

Function statements

Page 5: Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.

CSCE 106 5

User-Defined Functions (cont’d) You need to declare function prototypes (functions’

headings without the body of the functions) before your main() function.

Like standard functions, you need to call the function from your main() to activate it.

The function definition should come after your main().

Page 6: Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.

CSCE 106 6

#include <iostream>using namespace std;

void drawCircle();

void main(){ cout << “-----------“ << endl; drawCircle(); cout << “-----------“ << endl;}

void drawCircle(){ cout << “ * “ << endl; cout << “* *“ << endl; cout << “ * * “ << endl;}

Function prototype

Function heading

Function heading

Function callExec

utio

n

Page 7: Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.

CSCE 106 7

Functions With Arguments#include <iostream>using namespace std;

void drawCircleChar(char symbol);

void main(){ cout << “-----------“ << endl; drawCircleChar(‘*’); cout << “-----------“ << endl; drawCircleChar(‘o’); }

void drawCircleChar(char symbol){ cout << “ “ << symbol << endl; cout << symbol << “ “ << symbol << endl; cout << “ “ << symbol << “ “ << symbol << endl;}

Formal parameter

Parameter

Actual parameter

Page 8: Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.

CSCE 106 8

Value-Returning FunctionsThe syntax for defining a value-returning function is:

functionType functionName(formal parameter list) {

statements }

functionType – data type of the value returned by the function

formal parameter - a variable declared in the function heading

actual parameter - a variable or expression listed in a call to a function

Page 9: Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.

CSCE 106 9

Value-Returning Functions (cont’d)

The syntax of the formal parameter list is: dataType identifier, dataType identifier, ...

The syntax for a function call is: functionName(actual parameter list)

The syntax for the actual parameter list is: expression or variable,expression or variable, ...

There is a one-to-one correspondence between actual and formal parameters

The formal parameter list can be empty.

Page 10: Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.

CSCE 106 10

The return Statement Once the function computes the value, the

function returns this value via the return statement

The syntax of the return statement is: return expression or variable;

When a return statement executes in a function, the function immediately terminates and the control goes back to the caller

When a return statement executes in the function main(), the program terminates

Page 11: Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.

CSCE 106 11

ExerciseWrite a complete C++ program, for an algorithm that calculates the average of two numbers by the use of a value returning function (computeAverage). The main function should input the two numbers, as well as outputting the average.

Page 12: Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.

CSCE 106 12

#include <iostream>using namespace std;

float computeAverage(float num1, float num2);

int main(){ float x, y, av; cout << “Please enter two numbers:“ << endl; cin >> x >> y; av = computeAverage(x,y); cout << “The average of the two numbers is:“ << av << endl; return 0;}

float computeAverage(float num1, float num2) // 2 parameters{ // Compute the average of the data. return ((num1 + num2) / 2.0);} // end computeAverage function

Actual parameters

Formal parameters

Page 13: Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.

CSCE 106 13

Argument Correspondence

Actual Argumentxy

Corresponds to Formal Argumentnum1num2

Page 14: Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.

CSCE 106 14

Tracing Exercise#include <iostream>using namespace std;

int Blend( int red, int green ); // prototype void main(){

int red = 5, blue; 

blue = Blend(3, red + 1);cout << red << ' ' << blue << '\n';blue = Blend(blue, red);cout << red << ' ' << blue << '\n';

} int Blend( int red, int green ){ int yellow;  cout << “enter Blend “ << red <<‘ ‘ << green << ‘\n’;

yellow = red + green; cout << “leave Blend “ << red <<‘ ‘ << green << ‘\n’;

return (yellow + 1);}

Output:

enter Blend 3 6leave Blend 3 65 10enter Blend 10 5leave Blend 10 55 16

Page 15: Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.

CSCE 106 15

Next lecture we will continue the Modular Construct in C++