CSC2212 C++ - Function
CSC2212 C++ - Function
C++ Programming
Functions
Ch 6: Gaddis, Starting Out with C++ Early Objects 8th ed
Lab five
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Modular Programming
• Modular programming: breaking a program up
into smaller, manageable functions or modules.
Supports the divide-and-conquer approach to
solving a problem.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-2
Modular Programming
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-3
Defining and Calling Functions
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-4
Function Definition
• Definition includes:
name: name of the function. Function names follow
same rules as variable names
parameter list: variables that hold the values passed
to the function
body: statements that perform the function’s task
return type: data type of the value the function
returns to the part of the program that called it
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-5
Function Definition (example 1)
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-6
Function Definition (example 2)
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-7
Function Header
• Function header consists of
– the function return type
– the function name
– the function parameter list
• Example:
int main()
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-11
Function Prototypes
The compiler must know the following about
a function before it is called
– name
– return type
– number of parameters
– data type of each parameter
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-12
Function Prototypes (cont.)
Ways to notify the compiler about a function before
a call to the function:
1. Place function definition before calling function’s
definition
2. Use a function prototype (similar to the heading ) of
the function
• Heading: void printHeading()
• Prototype: void printHeading();
– Function prototype is also called a function declaration
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-13
Function Prototype Notes
• Place prototypes near top of program.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-14
Sending Data into a Function
• Can pass values into a function at time of call
c = sqrt(a*a + b*b);
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-15
Parameters, Prototypes,
& Function Headings
• For each function argument,
– the prototype must include the data type of each parameter in its
()
Example: void evenOrOdd(int); //prototype
– the heading must include a declaration, with variable type and
name, for each parameter in its ()
Example: void evenOrOdd(int num); //heading
The function call for the above function would look like this:
evenOrOdd(val); //call
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-17
Calling Functions with
Multiple Arguments Illustration
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-18
Passing Data by Value
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-19
Passing Data by Value (Example)
val num
5 5
argument in parameter in
calling function evenOrOdd function
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-21
Reference Variables
• A reference variable is an alias for another
variable
• It is defined with an ampersand (&) in the
prototype and in the header
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 © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-22
Pass by Reference Example
void squareIt(int &); //prototype
int localVar = 5;
squareIt(localVar); // Call
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-24
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
• Without a return statement, the function
ends at its last }
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-25
Returning a Value from a Function
• return statement can be used to return a value
from the function to the module that made the
function call
• Prototype and definition must indicate data type
of return value (not void)
• Calling function can use the return value, e.g.,
– assign it to a variable
– send it to cout
– use it in expression
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-26
Returning a Value
the return Statement
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-29
Local and Global Variables
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-30
Static Local Variables
• Local variables
– Only exist while the function is executing
– Are redefined each time function is called
– Lose their contents when function terminates
• static local variables
– Are defined with key word static
static int counter;
– Are defined and initialized only the first time the
function is executed
– Retain their contents between function calls
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-31
Default Arguments
• Values passed automatically if arguments are
missing from the function call
• Must be a constant declared in prototype or
header (whichever occurs first)
void evenOrOdd(int a = 0);
• Calling the function:
• evenOrOdd();
• evenOrOdd(3);
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-32
Default Arguments (cont.)
• Multi-parameter functions may have default
arguments for some or all parameters
int getSum(int, int=0, int=0);
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-33
Default Arguments (cont.)
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-34
Exercise 1
Write a simple program that can use
function to find the biggest number
among two numbers.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-35
Overloading Functions
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-37
The exit() Function
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-40