86
FUNCTION CSC128

TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

  • Upload
    dotu

  • View
    226

  • Download
    0

Embed Size (px)

Citation preview

Page 1: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

FUNCTIONCSC128

Page 2: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Introduction

What is function?

Module/mini program/sub-program

Each function/module/sub-program performs specific task

May contains its own variables/statements

Can be compiled/tested independently

An approach to handle complexity in solving problems where:-

A larger / complex problem is broken up into smaller problems (less complex)

The solution to the problem (a module), consists of – a combination of solutions

for the smaller problems (sub modules)

Multiple functions form a larger program

Modular programming

Break 1 large program or 1 module into sub-modules

Page 3: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Introduction

Page 4: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Example

WITHOUT functions

int main()

{

float num1, num2, sum;

cin >> num1 >> num2;

sum = num1 + num2;

cout << "Total Sum: " << sum;

return 0;

}

4

Page 5: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Example

WITH functions 1/2 float num1, num2, sum;

void getInputs();

void calculateSum();

void displaySum();

int main()

{

getInputs();

calculateSum();

displaySum();

return 0;

}5

Variables

declaration

Modules/functions

declaration

Calling

modules/functions

Main function

Page 6: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Example

WITH functions 2/2void getInputs()

{

cin >> num1 >> num2;

}

void calculateSum()

{

sum = num1 + num2;

}

void displaySum()

{

cout << "Total Sum: " << sum;

}

6

Function Definitions or

the actual modules are here, right after void

main().

Each module perform

specific task

Page 7: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Example

WITHOUT and WITH functions

Page 8: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Benefits of functions Divide and conquer

Manageable program development

Software reusability

Use existing functions as building blocks for new programs

Abstaction-hide internal details (library functions)

Avoid repetition

Simplify main() function/program

Planning, coding, testing, debugging, understanding, maintaining a

computer program will be easier

Same function can be reused in another program

Prevent function duplication

Reduce time of writing a program

Page 9: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Introduction

TWO (2) types of functions :-

[1] Pre-defined/built-in/library

Required special pre-processor (*.h files – older version)

iostream: cin, cout

cstring: strcpy, strcmp

cmath: sqrt(), pow(), abs()

[2] Programmer-defined/independent

Depends on programmer what kind of task will be performed by the function

Page 10: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Contents

Predefined Function

User – defined Function

Page 11: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Family of function

Page 12: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Predefined

function

Page 13: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Predefined Function

Functions that have been defined by the producer of a compiler.

Declaration of the functions are stored in header file. (.h extension – older version)

Also known as built in functions

Used by the programmers to speed up program writing.

Page 14: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Predefined Function

Programmers may use the existing code to perform

common tasks without having to rewrite any code.

These functions are predefined by the producer of

a compiler, (C++) and are stored in the header files

(.h files) called libraries

To use a pre‐defined function, appropriate library

file must be included in the program using the

#include directive.

Page 15: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Those without the .h is C++ header files while those

with .h are C header files.

This only applies to the standard header files in C++.

The old standard used the #include <filename.h>

syntax. When namespaces and templates were added to

the language, the standard was changed to #include

<filename>

The #include <filename.h> was common in C++ code

prior to the C++ standard. The standard changed it to

#include <filename> with everything from the header

placed in the std namespace.

Page 16: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Predefined FunctionHeader files Functions Descriptions

cmath pow(x, y) Raise to power.

sqrt(x) Compute square root.

floor(x) Round down value.

ceil(x) Round up value.

abs(x) Absolute value

cstring strcpy(x,y) Copy sequence of character

strcmp(x,y) To compare two sequence of character

strcat(x,y) To concatenate sequence of character

Page 17: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Predefined Function iomanip: header file to manipulate

input/output data.

Built-in functions:

17

Function Description

setw(x) Set field width to x

setfill(x) Set the fill character with x

setprecision(x) Set the floating point precision to x

Page 18: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Predefined Function

cstring: header file contains string

manipulation functions.

Built-in functions:

18

Function Description

strcmp(s1, s2) Compares one string to another

strcpy(s1, s2) Copies one string to another

strlen(s) Calculates the length of a string

strcat(s1, s2) Appends one string to another

Page 19: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Predefined Function cctype: header file for character

handling functions.

Built-in functions:

19

Function Description

toupper(c)Converts character c from lowercase

to uppercase letter

tolower(c)Converts character c from

uppercase to lowercase letter

isupper(c)Return TRUE if c is an uppercase

letter

islower(c) Return TRUE if c is a lowercase

letter

Page 20: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Predefined Function

cctype: header file for character

handling functions.

Built-in functions:

20

Function Description

isdigit(c) Return TRUE if c is digit

isalpha(c)Return TRUE if c is an alphanumeric

character

isspace(c)Return TRUE if c is a space

character

Page 21: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Predefined Function cmath: header file for mathematical

functions.

Built-in functions:

21

Functions

pow(x, y) sin(x) acos(x) cosh(x)

sqrt(x) cos(x) atan(x) tanh(x)

ceil(x) tan(x) log(x) log10(x)

floor(x) asin(x) sinh(x) exp(x)

fabs(x)

Page 22: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Predefined Function

Function call: is used to call or to invoke a function.

z and y are called parameters or arguments of the

function pow.

# include<iostream>

# include<cmath>

using namespace std;

void main()

{

int x,y,z;

y = 2, z = 4;

x = pow(z,y);

cout<<“x:”<<x;

}

Function call

Page 23: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Predefined Function

#include <iostream>

#include <cmath>

using namespace std;

void main()

{

cout << pow(2, 3);

}

23

Pre-processor

files

Power function

retrieved from cmath

cout function

retrieved from iostream

Page 24: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Defining and Calling

Functions

Function call: statement causes a

function to execute

Function definition: statements that make

up a function

Page 25: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Function Definition

Definition includes:

return type: data type of the value that function returns to the part of the program that called it

name: name of the function. Function names follow same rules as variables

parameter list: variables containing values passed to the function

body: statements that perform the function’s task, enclosed in {}

Page 26: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Function Definition

Note: The line that reads int main() is the

function header.

Page 27: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Variables

Page 28: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Variables

Two types of variables:

1) Local variable

2) Global variable

Page 29: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Variables – local variable

Variable that is declared within a body of

a function definition.

Variable that is declared within the

main() function are said to be local to

main function.

Page 30: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Variables – local variable

Example:

#include <iostream>

using namespace std;

void main()

{

int a;

}

Local variable

Page 31: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Variables – local variable

Page 32: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Variables – global variable

Variable that is declared outside and above main function.

Accessible to all function in the program.

Its value can be changed at any point during execution

New value will replace old value assigned to a particular global variable

It is not wise to use global variables any more than you have to.

Page 33: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Variables – global variable

Example:

#include <iostream>

using namespace std;

int a;

void main()

{

}

global variable

Page 34: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Variables – global variable

Page 35: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

User – defined

function

Page 36: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

User-defined function

Functions that are defined / written by the programmer / user in a program.

Characteristics of user-defined function:

A function name is named with unique name.

Can be called from other function

A function performs a specific task.

Task is a discrete job that the program must perform as part of its overall operation.

A function is independent.

A function may receive values from the calling program (function call).

A function may return value to the calling program (function call).

Page 37: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

User-defined function

In general a function has the following syntax:

returnValueType functionName(list of parameters)

{

}

Page 38: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

User-defined function

void/ value returning function.

With/ without parameters.

returnValueType functionName(list of parameters)

{

}

Page 39: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

User-defined function

To create a user defined function, you must write

the following:

1) Function prototype (function’s declaration)

2) Function definition

3) Function call

If the function definition is defined before the

main function, you may not need the function

prototype.

Page 40: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

User-defined Function

Requirements

40

variable declarations; //global

void functionA();

void main()

{

variable declarations; //local

functionA();

statements; //other statements

}

void functionA()

{

statements;

}

1) Function

prototype

declarations

2) Function call

3) Function

definitions

Page 41: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

User-defined Function

Requirements

41

int calculateSum(int, int);

void main()

{

int num1, num2;

cin >> num1 >> num2;

cout << calculateSum(num1, num2);

}

void calculateSum (int x, int y)

{

int total = x + y;

return total;

}

1) Function

prototype

declarations

2) Function

call

3) Function

definitions

Page 42: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

User-defined Function

Requirements

42

int calculateSum(int, int);

void calculateSum (int x, int y)

{

int total = x + y;

return total;

}

void main()

{

int num1, num2;

cin >> num1 >> num2;

calculateSum(num1, num2);

}

1) Function

prototype

declarations

2) Function call

3) Function

definitions

Page 43: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

1) Function prototype

Is used to declare a function.

Problem statement: to create a function to

calculate total of 2 numbers.

Steps:

1) Identify function name.

2) void/ value returning function?

3) With/ without parameter?

Page 44: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

1) Function prototype..cont.

void function without parameter

void function with parameter

Value returning function, without parameter. (return integer value)

Value returning function, with parameter. . (return integer value)

void calculateSum();

void calculateSum (int x, int y);

int calculateSum();

int calculateSum (int x, int y);

Can you see the differences ?

?

Page 45: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

1) Function prototype..cont.

Is placed in between preprocessor directives and main

function.

#include <iostream>

using namespace std;

void calculateSum ();

void main()

{

}

Page 46: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

2) Function definition

Is a block of code (statements) that perform specific

task.

Statements are usually declaration and/or executable

statements.

Must be declared 1st before it can be used

Place it BEFORE or AFTER main()

Page 47: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

2) Function definition..cont.

47

Syntax:

returnType functionName (type parameter_list)

{

declaration(s);

statement(s);

return expression;

}

Function

body

Function

header

Page 48: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

2) Function definition..cont.

48

Example using void return type:

void sumNumbers(int, int, int)

{

int total = a + b + c;

cout << total;

}If you use void in

function header, you must NOT have return

statement inside

function body

Page 49: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

2) Function definition..cont.

49

Example using int return type:

int sumNumbers(int, int, int)

{

int total = a + b + c;

return total;

} If you use int/float/double/long/char

in function header, you MUST HAVE return statement inside

function body

Page 50: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

3) Function call

When a function is called, the program control is passed

to the called function and the statements inside the

function will be executed until control is passed back

the calling function

To call a function, specify the function name and the

values of the parameters [if any]

Page 51: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

3) Function call..cont.

51

[3a]Function call that returns no value

Contains void return type on function

header

Once complete execute last statement

in function definition, the control is

passed back to the statement that calls

the function in the calling function

Then, next statement will be executed

Page 52: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

3) Function call..cont.

52

[3b]Function call that returns a value

Contains other than void on function header

When execute of function definition is complete, control is passed back to the calling function with a value

That returned value can be used:

In an arithmetic expression, logical expression, assignment statement, output statement

Page 53: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Recap…

53

No return value Return value

Without parameters void calculateSum () int calculateSum ()

With parameters void calculateSum (int a, int b) int calculateSum (int a, int b)

Page 54: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

void function without

parameter

General syntax:

Page 55: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

void function without

parameter

Example:

void calTotal()

{

int a, b, result;

a = 1, b = 2;

result = a + b;

cout<<result;

}

return_type

function_name parameter_list

function_body

Function header

Page 56: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

void function without

parameter

Function call: is used to invoke/ to call a function to

execute task.

Is placed inside main function.

Example:

int main()

{

calTotal();

return 0;

}

Page 57: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

void function without

parameter

Full code:

#include<iostream>

using namespace std;

void calTotal();

int main()

{

calTotal();

return 0;

}

void calTotal()

{

int a, b, result;

a = 1, b = 2;

result = a + b;

cout<<result;

}

Function call

Function prototype

Function definition

Function header

Page 58: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

void function without

parameter

Exercise 1: using void function,

write a program to calculate an

average of 3 numbers.

Page 59: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

void function with parameter

Parameter:

Parameter is ways how calling function and

called function are communicated.

Through parameter, data can pass from calling

function to called function and from called

function to calling function.

Formal parameters are optional

Two types of parameter:

Value parameter

Reference parameter

Page 60: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

void function with parameter

Value parameter

A formal parameter that receives a copy of the

content of the corresponding actual parameter.

Reference parameter*

A formal parameter that receives the location of

the corresponding parameter.

Page 61: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

void function with parameter (value)

void calTotal(int x, int y)

{

int result;

result = x + y;

cout<<result;

}

Formal parameter

Page 62: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

void function with parameter (value)

#include<iostream>

using namespace std;

void calTotal(int, int);

void main()

{

int a; int b;

a = 2, b = 3;

calTotal(a, b);

}

void calTotal(int x, int y)

{

int result;

result = x + y;

cout<<result;

}

Actual parameter

Page 63: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

void function with parameter (value)

Example:void findStatus(int);

void main()

{

int mark;

cout<<“enter mark:”;

cin>>mark;

findStatus(mark);

}

void findStatus(int value)

{

if (value>=80)

cout<<“excellent”;

else if(value>=60)

cout<<“good”;

else if(value>=50)

cout<<“pass”;

else

cout<<“fail”;

}

Page 64: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

void function with parameter (value)

Exercise 2: using void function with parameter, write a

program that require a user to input 2 numbers and

determine the maximum number.

Page 65: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Value returning function

Is a function that after completing its assigned task,

return precisely one value to the calling function

(function call).

Why we return a value?

To use it for further calculation or process.

Is used in an assignment statement or in an output

statement.

Page 66: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Value returning function

Function definition:

int calTotal(int x, int y)

{

int result;

result = x + y;

return result;

}

Return type or data type

Page 67: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Value returning function#include<iostream.h>

int calTotal(int, int);

void main()

{

int a,b,z;

a = 2, b = 3

z = calTotal(a, b);

cout<<“total :”<<z;

}

int calTotal(int x, int y)

{

int result;

result = x + y;

return result;

}

Function call: Assignment

statement

Function prototype

Page 68: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Value returning function#include<iostream.h>

int calTotal(int, int);

void main()

{

int a,b;

a = 2, b = 3

cout<<“total :”<< calTotal(a, b);

}

int calTotal(int x, int y)

{

int result;

result = x + y;

return result;

}

Function call: Output

statement

Page 69: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Passing Value Between

Functions

69

int calcSum(int, int);

void main()

{

int a, b;

cin >> a >> b;

cout << calcSum(a, b);

}

int calcSum(int x, int y)

{

return (x + y);

}

a = x = 1

b = y = 1

return 2

cout 2

Example using return

statement

Page 70: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Value returning function

Exercise 3: using value returning function, write a

program to determine maximum value of two numbers.

Page 71: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Value returning function

Exercise 4: using value returning function write a

program to calculate an average of three numbers.

Page 72: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Function

(reference

parameter)

Page 73: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Function – reference

parameter

If a formal parameter is a reference parameter It receives the address (memory location) of the

corresponding actual parameter

During program execution to manipulate the data The address stored in the reference parameter directs it to the

memory space of the corresponding actual parameter

Can pass one or more values from a function

Can change the value of the actual parameter

Page 74: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Function – reference

parameter

A reference parameter receives and stores the address

of the corresponding actual parameter

Reference parameters are useful in three situations:

Returning more than one value

Changing the actual parameter

When passing the address would save memory space and

time.

Page 75: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Function – reference

parameter

Page 76: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Function – value parameter#include <iostream>

using namespace std;

void addFour(int a, int b);

void main()

{

int num1,num2;

num1 = 3;

num2 = 1;

cout<< num1<<num2<<endl;

addFour(num1,num2);

cout<<num1<<num2<endl;

}

void addFour(int a, int b)

{

a = a + 4;

b++;

cout<<a<<b<<endl;

}

Value parameter

In main BEFORE calling

addFour function

3num1

In addFour function

7a

In main AFTER calling

addFour function

1num2 2b

3num1

1num2

Page 77: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Function – reference

parameter

Reference parameter

When the value of a

changes, value num1

changes as well

#include <iostream>

using namespace std;

void addFour(int& a, int b);

void main()

{

int num1,num2;

num1 = 3;

num2 = 1;

cout<< num1<<num2<<endl;

addFour(num1,num2);

cout<<num1<<num2<endl;

}

void addFour(int &a, int b)

{

a = a + 4; //line 1

b++; //line 2

cout<<a<<b<<endl;

}

In main BEFORE calling

addFour function

3num1

In addFour function

7a

In main AFTER calling

addFour function

1num2 2b

7num1

1num2

Reference parameter

Page 78: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Function – reference

parameter

How it works?

In main BEFORE calling

addFour functionIn addFour function

3num1 a

1num2 1b

Before statement in line 1 execute!!

Page 79: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

In main BEFORE

calling addFour function

In addFour function

7num1 a

1num2 2b

In function addFour, after statement in line 1 and line 2 execute!!

Page 80: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Scope of a local and global

variable#include <iostream>

void calTotal();

using namespace std;

void main()

{

int a;

}

void calTotal()

{

int a;

}

Variable a in function main() is different from variable a in

Function calTotal()

Page 81: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Global variable

#include <iostream>

using namespace std;

void calTotal();

int num1;

void main()

{ int num2;

total = num1 + num2

}

void calTotal()

{

int num2;

total = num1 – num2;

}

Variable num1 is a global variable that are accessible to all function

definitions in the file.

Page 82: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Summary

void function: a function that does not have a data type

A return statement without any value can be used in a void function to exit function early

The heading of a void function starts with the word void

To call a void function, you use the function name together with the actual parameters in a stand-alone statement

Page 83: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Summary (continued)

Two types of formal parameters: value parameters and

reference parameters

A value parameter receives a copy of its corresponding

actual parameter

A reference parameter receives the address (memory

location) of its corresponding actual parameter

Page 84: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Summary (continued)

If a formal parameter needs to change the value of an

actual parameter, in the function heading you must

declare this formal parameter as a reference parameter

Variables declared within a function (or block) are

called local variables

Variables declared outside of every function definition

(and block) are called global variables

Page 85: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,

Summary (continued)

If a formal parameter needs to change the value of an

actual parameter, in the function heading you must

declare this formal parameter as a reference parameter

Variables declared within a function (or block) are

called local variables

Variables declared outside of every function definition

(and block) are called global variables

Page 86: TOPIC 5: FUNCTION - … · A larger / complex problem is broken up into smaller problems ... If the function definition is defined before the ... assignment statement,