Title: Functions, Part 2 of 42
1Functions, Part 2 of 42
- Topics
- Functions That Return a Value
- Parameter Passing
- Local Variables
- Miscellaneous hints
- Reading
- Sections 3.1 3.6
2Functions Can Return Values
/
averageTwo -
calculates and returns the average of two
numbers Inputs num1 - an integer value
num2 - an integer value Outputs
the floating point average of num1 and
num2
/ float
averageTwo (int num1, int num2) float average
/ average of the two numbers / average
(num1 num2) / 2.0 return average
3Using averageTwo
- include ltstdio.hgt
- float averageTwo (int num1, int num2)
- int main ( )
-
- float ave
- int value1 5, value2 8
- ave averageTwo (value1, value2)
- printf (The average of d and d is f\n,
value1, value2, ave) - return 0
-
- float averageTwo (int num1, int num2)
-
- float average
- average (num1 num2) / 2.0
- return average
-
4Parameter Passing
- Actual parameters are the parameters that appear
in the function call. - average averageTwo (value1, value2)
- Formal parameters are the parameters that appear
in the function header. - float averageTwo (int num1, int num2)
- Actual and formal parameters are matched by
position. Each formal parameter receives the
value of its corresponding actual parameter.
5Parameter Passing (cont)
- Corresponding actual and formal parameters do not
have to have the same name, but they may. - Corresponding actual and formal parameters must
be of the same data type, with some exceptions.
6Local Variables
- Functions only see (have access to) their own
local variables. This includes main( ) . - Formal parameters are declarations of local
variables. The values passed are assigned to
those variables. - Other local variables can be declared within the
function body. - There is another type of variable called a global
variable. Do not use them! They are a major
source of errors!
7Parameter Passing and Local Variables
- include ltstdio.hgt float averageTwo (int
num1, int num2) - float averageTwo (int num1, int num2)
- int main ( ) float average
-
- float ave average (num1 num2)
/ 2.0 - int value1 5, value2 8 return
average -
- ave averageTwo (value1,
- value2)
- printf (The average of )
- printf (d and d is f\n,
- value1, value2, ave)
- return 0
-
- value1 value2 ave num1
num2 average -
- 5 8
- int int float
int int
float
8Same Name, Still Different Memory Locations
- include ltstdio.hgt float averageTwo (int
num1, int num2) - float averageTwo (int num1, int num2)
- int main ( ) float average
-
- float average average (num1
num2) / 2.0 - int num1 5, num2 8 return
average -
- average averageTwo (num1,
- num2)
- printf (The average of )
- printf (d and d is f\n,
- num1, num2, average)
- return 0
-
- num1 num2 average
num1 num2 average -
- 5 8
- int int float
int int
float
9Changes to Local Variables Do NOTChange Other
Variables with the Same Name
- include ltstdio.hgt
- void addOne (int number) void
addOne (int num1) - int main ( ) num1
- printf (In addOne )
- int num1 5 printf (num1 d\n, num1)
- addOne (num1)
- printf (In main )
- printf (num1 d\n, num1) num1
- return 0
-
int - num1
- 5 OUTPUT
- int In addOne num1 6
- In main num1 5
10Sample Program
- include ltstdio.hgt
- int funcA(int a, int b, int c )
- int main( void )
-
- int x 3, y 4, z 5
- int result
- result funcA( x, y, z )
- printf( "result d\n", result )
- result funcA( 6 4, y 2, 9 )
- printf( "result d\n", result )
- result funcA( 2.1, 3.5, 4.0 )
- printf( "result d\n", result )
- return 0
11Sample Program (contd)
- int funcA(int a, int b, int c )
-
- printf(" a d b d c d\n", a, b, c
) - return ( a b c )
12Sample Program Output
- burt_at_linux3 a.out
- a 3 b 4 c 5
- result 12
- a 10 b 6 c 9
- result 25
- a 2 b 3 c 4
- result 9
13Sample Program Analysis - 1
- int x 3, y 4, z 5
- result funcA( x, y, z )
- printf( "result d\n", result )
-
- a 3 b 4 c 5
- result 12
-
- Looks good!
14Sample Program Analysis - 2
- result funcA( 6 4, y 2, 9 )
- printf( "result d\n", result )
-
- a 10 b 6 c 9
- result 25
-
- 6 4 is 10, OK.
- y is 4 2 is 6, OK.
15Sample Program Analysis - 3
- result funcA( 2.1, 3.5, 4.0 )
- printf( "result d\n", result )
-
- a 2 b 3 c 4
- result 9
-
- Notice that 2.1 became 2, 3.5 became 3 and 4.0
became 4. This is called truncation. The
compiler tried to make the data fit the
prototype! This is called an implied conversion,
and can be the source for errors that is hard to
find. - Do not use implied conversions!
16Another Example
- include ltstdio.hgt
- include ltstdlib.hgt
- int main( void )
-
- int negNum -3
- printf( "d\n", abs( negNum ) 2 )
- return 0
-
17Another Example Output
- burt_at_linux3 a.out
- 6
- burt_at_linux3
-
- In this example the expression was the result of
another function multiplied by a constant. - Whatever the argument is, it is evaluated to the
proper data type and then the function is invoked
that that final version of the argument.
18system( ) function
- The system( ) function allows you to do any shell
command from inside a program. - The function must have an argument that is the
command you wish to have executed. - Must include ltstdlib.hgt
19system( ) Function Example
- include ltstdio.hgt
- include ltstdlib.hgt
- int main ( void )
-
- / Clear the screen /
- system( "clear" )
- printf( "Hello, World!\n" )
- return 0
-
20Good Programming Style
- Functions should do one thing and do it well!
- Functions can only return one thing! In CMSC201
and CMSC341, you will learn that it can be a data
structure as will as a simple variable. - It is better to have more than one function than
one complicated function.
21Good Programming Style (contd)
- Function subprograms allow us to remove the the
main function the code that provides the detailed
solutions to a subproblem. - Never write the same code twice.
- Limit your functions to a maximum of 50-100 lines
of code and comments. - Many functions have only one line of code.
22Good Programming Style (contd)
- Forgetting to return a value from a function that
is suppose to return a value can lead to
unexpected errors. - Programs should be written as a collection of
small functions. - The function prototype, function header and
function calls should all agree in the number,
type, and order of argument s and parameters, and
the type of the return value.