Title: Lecture 17:User-Definded function II
1Lecture 17User-Definded function II
- Introduction to Computer Science
- Spring 2006
2User-Defined Functions
- Void functions do not have a data type
- Value-returning functions have a data type
3include ltiostreamgt using namespace std /
Function Declarations / int FindMax(int n1, int
n2) void PrintMax(int someNumber) int
main() int i, j int k
cingtgtigtgtj k FindMax(i,j)
PrintMax(k) // Prints Max Value
return 0
include ltiostreamgt using namespace std int
main() int i, j int k
cingtgtigtgtj if (i gt j) k i
else k j
cout ltlt "The max is " ltlt k ltlt
endl return 0
Value-returning functions
Void functions
4include ltiostreamgt using namespace std /
Function Declarations / void PrintHW()float
FtoC(float faren) int main() float
tempInF 85.0 float tempInC PrintHW
() / Prints Hello World / tempInC
FtoC(tempInF) cout ltlt tempInF ltlt "
Fahrenheit equals " ltlt tempInC ltlt "
Celsius " ltlt endl return 0
include ltiostreamgt using namespace std int
main() float tempInF 85.0 float
tempInC cout ltlt "Hello World" ltlt endl
float factor 5./9. float freezing
32.0 tempInC factor (tempInF -
freezing) cout ltlt tempInF ltlt " Fahrenheit
equals " ltlt tempInC ltlt " Celsius " ltlt
endl return 0
Void functions
Value-returning functions
5Void Functions function definition
- Void functions and value-returning functions have
similar structures - Heading
- Name of the function
- Number of parameters
- Data type of each parameter
- Function Type (type of the value returned by the
function) - Body
- Code required to accomplish the task (the body
of the function) - The syntax of the function definition is
- void functionName(formal parameter list)
-
- statements
-
- void is a reserved word
- A void function does not have a data type
- The syntax of the formal parameter list is
- dataType identifier, dataType identifier, ...
6Void Functions function definition
Function Name
Formal parameter list
Function Heading
void PrintMax(int someNumber)
void PrintMax(int someNumber) cout ltlt "The
max is " ltlt someNumber ltlt endl
void PrintHW() cout ltlt "Hello World" ltlt
endl
7Void Functions call a void function
- To call a void function
- Use its name, with the actual parameters (if any)
in parentheses - The return statement without any value is
typically used to exit the function early - A call to a void function is a stand-alone
statement - The syntax for a function call is
- functionName(actual parameter list)
- functionName() (If no formal parameters in
function definition) - The syntax for the actual parameter list is
- expression or variable,expression or variable,
...
8include ltiostreamgt using namespace std /
Function Declarations / int FindMax(int n1, int
n2) void PrintMax(int someNumber) int
main() int i, j int k
cingtgtigtgtj k FindMax(i,j) PrintMax( k
) return 0 / Function Definitions
/ void PrintMax( int someNumber) cout ltlt
"The max is " ltlt someNumber ltlt endl /
Function Definitions / int FindMax(int n1, int
n2) if (n1 gt n2) return n1
else return n2
Actual parameter list
Call a void Function with parameters
Formal parameter list
9include ltiostreamgt using namespace std /
Function Declarations / void PrintHW() int
main() / Prints Hello World
/ PrintHW() return 0 /
Function Definitions / void PrintHW() cout
ltlt "Hello World" ltlt endl
Call a void Function without parameters
10Question
- How many values can be returned by void function?
- How many values can be returned by
value-returning function? - What if we want to return multiple values from
the funtion?
Answer1 Pass by reference (Using reference
variables as parameters
Answer2 pass a pointer to the variable to the
function (will be covered later)
11Reference
- Referencing is generally used in a wider context
- in the context of "aliasing. - The "" operator is used for referencing -
meaning "reference to". - This is of the form
- Datatype variable_name
initialisation_expression - For example
- int i 10
- int j i // j is an alias for i
- Both i and j refer to the same object -
modifying i is equivalent to modifying j, and
vice versa. - That is, a reference is an alias for an object
and does not, itself, occupy any memory.
12Reference (cont.)
- includeltiostreamgt
- using namespace std
- int main()
-
- int x10
- int y x
- yy1
- coutltlt"x"ltltxltltendl
- coutltlt"y"ltltyltltendl
13Parameters Passing
include ltiostreamgtusing namespace stdvoid
swap(int x, int y) int main() int x
4 int y 2 cout ltlt "Before swap, x
is " ltlt x ltlt ", y is " ltlt y ltlt endl swap(x,y)
cout ltlt "After swap, x is " ltlt x ltlt ", y is
" ltlt y ltlt endlvoid swap(int first, int
second) int temp temp
second second first first temp
A formal parameter receives a copy of the content
of corresponding actual parameter. which is
called Pass by value
Any modifications to the local copy do not
change the original variable in the calling
program.
An alias of the argument is passed to the called
function. which is called Pass by reference
When references are passed into a function, any
changes made to the references will be seen in
the calling function.
14Parameters Passing
- Pass by value (A formal parameter is a value
parameter) - The value of the corresponding actual parameter
is copied into it - The value parameter has its own copy of the data
- Any modifications to the local copy do not change
the original variable in the calling program - Pass by reference (A formal parameter is a
reference parameter) - An alias of the argument is passed to the called
function. - no copies of the actual parameter are made.
- When references are passed into a function, any
changes made to the references will be seen in
the calling function.
15Reference Variables as Parameters
- Reference parameters can
- Pass one or more values from a function
- Change the value of the 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
16End of lecture 17