Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
20 views53 pages

PF Lecture 04

Chapter 6 covers modular programming, which involves breaking programs into smaller functions to enhance maintainability and simplify writing. It explains how to define and call functions, including the use of function prototypes and passing data by value or reference. The chapter also discusses local and global variables, as well as the return statement for returning values from functions.

Uploaded by

Ghulam Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views53 pages

PF Lecture 04

Chapter 6 covers modular programming, which involves breaking programs into smaller functions to enhance maintainability and simplify writing. It explains how to define and call functions, including the use of function prototypes and passing data by value or reference. The chapter also discusses local and global variables, as well as the return statement for returning values from functions.

Uploaded by

Ghulam Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 53

Chapter 6:

Functions

Copyright © 2012 Pearson Education, Inc.


6.1
Modular Programming

Copyright © 2012 Pearson Education, Inc.


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

Copyright © 2012 Pearson Education, Inc.


Copyright © 2012 Pearson Education, Inc.
6.2
Defining and Calling Functions

Copyright © 2012 Pearson Education, Inc.


Defining and Calling Functions
• Function call: statement causes a function
to execute
• Function definition: statements that make
up a function

Copyright © 2012 Pearson Education, Inc.


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 {}

Copyright © 2012 Pearson Education, Inc.


Function Definition

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


function header.

Copyright © 2012 Pearson Education, Inc.


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";
}

Copyright © 2012 Pearson Education, Inc.


Calling a Function
• 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.

Copyright © 2012 Pearson Education, Inc.


Functions in Program 6-1

Copyright © 2012 Pearson Education, Inc.


Flow of Control in Program 6-1

Copyright © 2012 Pearson Education, Inc.


6.3
Function Prototypes

Copyright © 2012 Pearson Education, Inc.


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

– Place function definition before calling function’s


definition

– Use a function prototype (function declaration) – like


the function definition without the body
• Header: void printHeading()
• Prototype: void printHeading();

Copyright © 2012 Pearson Education, Inc.


(Program Continues)

Copyright © 2012 Pearson Education, Inc.


Program 6-5 (Continued)

Copyright © 2012 Pearson Education, Inc.


Prototype Notes

• Place prototypes near top of program

• Program must include either prototype or full


function definition before any call to the
function – compiler error otherwise

• When using prototypes, can place function


definitions in any order in source file

Copyright © 2012 Pearson Education, Inc.


6.4
Sending Data into a Function

Copyright © 2012 Pearson Education, Inc.


A Function with a Parameter
Variable
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.

Copyright © 2012 Pearson Education, Inc.


(Program Continues)

Copyright © 2012 Pearson Education, Inc.


Copyright © 2012 Pearson Education, Inc.
The function call in line 11 passes the value 5
as an argument to the function.
Copyright © 2012 Pearson Education, Inc.
Other Parameter Terminology
• A parameter can also be called a formal
parameter or a formal argument
• An argument can also be called an actual
parameter or an actual argument

Copyright © 2012 Pearson Education, Inc.


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); //prototype
void evenOrOdd(int num) //header
evenOrOdd(val); //call

Copyright © 2012 Pearson Education, Inc.


Function Call Notes

• Value of argument is copied into parameter when


the function is called
• A parameter’s scope is the function which uses it
• Function can have multiple parameters
• There must be a data type listed in the prototype
() and an argument declaration in the function
header () for each parameter
• Arguments will be promoted/demoted as
necessary to match parameters

Copyright © 2012 Pearson Education, Inc.


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.

Copyright © 2012 Pearson Education, Inc.


(Program Continues)

Copyright © 2012 Pearson Education, Inc.


Program 6-8 (Continued)

Copyright © 2012 Pearson Education, Inc.


The function call in line 18 passes value1,
value2, and value3 as a arguments to the
function.
Copyright © 2012 Pearson Education, Inc.
6.5
Passing Data by Value

Copyright © 2012 Pearson Education, Inc.


Passing Data by Value
• Pass by value: when an argument is
passed to a function, its value is copied
into the parameter.

• Changes to the parameter in the function


do not affect the value of the argument

Copyright © 2012 Pearson Education, Inc.


Passing Information to
Parameters by Value
• Example: int val=5;
evenOrOdd(val);
val num
5 5
argument in parameter in
calling function evenOrOdd function

• evenOrOdd can change variable num, but


it will have no effect on variable val

Copyright © 2012 Pearson Education, Inc.


6.7
The return Statement

Copyright © 2012 Pearson Education, Inc.


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 }

Copyright © 2012 Pearson Education, Inc.


(Program Continues)
Copyright © 2012 Pearson Education, Inc.
Program 6-11(Continued)

Copyright © 2012 Pearson Education, Inc.


6.8
Returning a Value From a
Function

Copyright © 2012 Pearson Education, Inc.


Returning a Value From a
Function
• 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)


{
double result;
result = num1 + num2;
return result;
}

Copyright © 2012 Pearson Education, Inc.


A Value-Returning Function
Return Type

int sum(int num1, int num2)


{
double result;
result = num1 + num2;
return result;
}

Value Being Returned


Copyright © 2012 Pearson Education, Inc.
A Value-Returning Function

int sum(int num1, int num2)


{
return num1 + num2;
}

Functions can return the values of


expressions, such as num1 + num2

Copyright © 2012 Pearson Education, Inc.


(Program Continues)
Copyright © 2012 Pearson Education, Inc.
Program 6-12 (Continued)

Copyright © 2012 Pearson Education, Inc.


The statement in line 17 calls the sum function,
passing value1 and value2 as arguments.
The return value is assigned to the total variable.

Copyright © 2012 Pearson Education, Inc.


Returning a Value From a
Function
• The prototype and the definition must
indicate the data type of return value
(not void)

• Calling function should use return value:


– assign it to a variable
– send it to cout
– use it in an expression

Copyright © 2012 Pearson Education, Inc.


Local and Global Variables

• Variables defined inside a function are local to


that function. They are hidden from the
statements in other functions, which normally
cannot access them.
• Because the variables defined in a function are
hidden, other functions may have separate,
distinct variables with the same name.

Copyright © 2012 Pearson Education, Inc.


Copyright © 2012 Pearson Education, Inc.
Local Variable Lifetime
• A function’s local variables exist only while the
function is executing. This is known as the lifetime
of a local variable.

• When the function begins, its local variables and its


parameter variables are created in memory, and
when the function ends, the local variables and
parameter variables are destroyed.

• This means that any value stored in a local variable


is lost between calls to the function in which the
variable is declared.
Copyright © 2012 Pearson Education, Inc.
Global Variables and
Global Constants
• 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.

Copyright © 2012 Pearson Education, Inc.


6.13
Using Reference Variables as
Parameters

Copyright © 2012 Pearson Education, Inc.


Using Reference Variables as
Parameters
• A mechanism that allows a function to
work with the original argument from the
function call, not a copy of the argument
• Allows the function to modify values
stored in the calling environment
• Provides a way for the function to ‘return’
more than one value

Copyright © 2012 Pearson Education, Inc.


Passing by Reference
• A reference variable is an alias for another
variable
• Defined with an ampersand (&)
void getDimensions(int&, int&);
• Changes to a reference variable are made
to the variable it refers to
• Use reference variables to implement
passing parameters by reference

Copyright © 2012 Pearson Education, Inc.


The & here in the prototype indicates that the
parameter is a reference variable.

Here we are passing value by


reference.

(Program Continues)

Copyright © 2012 Pearson Education, Inc.


Program 6-25 (Continued)
The & also appears here in the function header.

Copyright © 2012 Pearson Education, Inc.

You might also like