Title: Lecture 7 User Defined Functions
1Lecture 7User Defined Functions
- ENGR17 Engineering Programming
- Section 1
- Fall 2001
9/28/01
2Outline
- Creating and invoking void functions
- Passing information by value
- Passing information by reference
- Creating a function that returns a value
- Passing a string to a function
3Programmer-Defined Function
- Can be either
- Value-returning function
- Void function
- Called in exactly the same way as a built-in
function - Void Function
- A function that does not return a value
- Could be used to display information
- Could be used to output to a file
- And other uses
4Example Program
- Sample OutputEnter a number 4-----------------
------------------------------------------------S
quare and Square Root CalculationsSquare
16Square Root 2Press any key to continue_
5(No Transcript)
6Void Function Definition
- Defines the functions task
- Is entered in programmer-defined section of
program - Uses same naming rules as naming variables
- Function header begins with keyword void
- Enclose statements that comprise the function in
a set of braces - Formal parameter
- Each item of information received by a function
7Function Definition
8Calling a Void Function
- Include a function definition and function
call(s) in a program - Enter function prototype above the main function
- Function prototype and function definitions
header must begin with keyword void - Use the functions name and actual arguments, if
any, as a separate statement in the program
9Calling a Void Function
10Calling a Void Function
11Using Function Prototypes
- Statements that tell C the following
- The functions name
- The data type of its return value
- The data type of each of its formal parameters
123 Parts of a Function
- include ltiostream.hgt
- include ltmath.hgt
- // function prototypes
- void displayLine()
- void main ()
- //declare and initialize variables
- float numberInput (float) 0.0
-
- displayLine()
-
-
- void displayLine()
-
1. Prototype
2. Call
3. Definition
13Passing Information to a Function
- Include one or more actual arguments in the
function calls argumentlist - Typically, information passed to a function is
one of the following - A literal constant
- The contents of a variable
- The address of a variable in memory
- Used when the function needs to process
information
14Passing Information to a Function
- Allow a function to receive information passed to
it by including formal parameter(s) in function
headers parameterlist - Data type and name of formal parameters must be
explicitly stated in function header - Data type of formal parameters must match data
type of actual arguments - Order in which formal parameters are listed in
function header must match order of actual
arguments listed in the function
15Passing Information to a Function
Argument List
Parameter List
16(No Transcript)
17Passing Information to a Function
18The Scope of a Variable
- Indicates which portions of the program can use
the variable - Is determined by where the variable is declared
in the program - Local
- Declared in a statement block known only to
function or statement block in which they are
declared - Global (avoid using)
- Declared outside of any function in the program
- Allow unintentional errors
- Lifetime of a Variable
- Indicates how long the variable remains in the
computers memory
19Passing Variables by Value
- C passes only the contents of the variable to
the receiving function - Receiving function does not have access to the
variable in memory, so it cannot change the
variables contents - Used when the receiving function needs to know,
but not change, the value stored inside the
variable - Unless specified otherwise, all variables in C
are passed by value
20Passing Variables by Reference
- C passes the variables address in memory
- Contents of the variable can be changed by the
receiving function - Used when the receiving function needs to change
the contents of the variable - Must include an ampersand, called the address-of
operator, before the corresponding formal
parameters name in the receiving functions
parameterlist
21Pass Variables by Value and Reference
- To pass the three variables, modify the
following - Function prototype
- Function call
- Function definition
22(No Transcript)
23Pass Variables by Value and Reference
24Pass Variables by Value and Reference
25(No Transcript)
26Pass Variables by Value and Reference
27Pass Variables by Value and Reference
28Summary of Void Functions
- Creating and invoking void functions
- Passing information by value
- Passing information by reference
- Variable Scope
29Value-returning Functions
- Similar to creating and invoking a void function
- Enter same three items of information
- A function prototype
- One or more function calls
- A function definition
30Value-returning Functions
- Function prototype and function definitions
header must begin with a data type - Function definition must end with a return
statement that returns a value to the caller - A call to a value-returning function does not
typically appear as a statement by itself, but as
a part of another statement - Can return only one value
31Value-returning Functions
32Value-returning vs. Void Functions
33continued
34Value-returning Functions
35Passing a String to a Function
- Strings are always passed by reference
- Thus, strings can be modified in the function
- Examplevoid main () char first50
Jon char last50 Smith char name50
make_string(first, last, name)void
make_string(char first , char last , char
name ) strcpy(name, last) strcat(name, ,
) strcat(name, first)
Dont need string lengths in header
36Summary of Functions
- Creating and invoking void functions
- Passing information by value
- Passing information by reference
- Variable Scope
- Creating a function that returns a value
- Passing a string to a function
37Questions
- What comes before main() when you use functions?
- Function prototype
- What is within main() when you use a function?
- Function call
- What is after main() when you use a function?
- Function definition
- What is the name for the information within the
() of a function call? - Argument list
- What is the name for the information within the
() of a function header in function definition? - Parameter list
38Questions
- Give the function header for a void function
foo with no arguments - void foo(void)
- Give the function header for a function foo
that returns and integer but has no arguments - int foo(void)
- Give the function header for a function foo
that returns and integer and is passed the
integer a by value - int foo(int a)
- Give the function header for a void function
foo that is passed the float a by reference
and the char b by value - void foo(float a, char b)
39Example
- Rewrite Lab01 using functions
- Design a program to compute the area and
perimeter of a rectangle. The user will input the
height and width (in meters) of the rectangle and
the program will print the area and perimeter to
the screen. - Use a value returning function with pass by value
to calculate the area - Use a void function with pass by reference and
pass by value to calculate the perimeter
40Code (1)
- include ltiostream.hgt
- float calc_area(float height, float width)
- void calc_perimeter(float perimeter, float
height, float width) - int main ()
- float height
- float width
- float area
- float perimeter
- // get height and width from user
- cout ltlt "Enter height "
- cin gtgt height
- cout ltlt "Enter width "
- cin gtgt width
- // calculate area
- area calc_area(height,width)
41Code (2)
- float calc_area(float height, float width)
- return height width
-
- void calc_perimeter(float perimeter, float
height, float width) - perimeter 2height 2width
-