Download presentation
Presentation is loading. Please wait.
Published byShon Cox Modified over 5 years ago
1
Functions, Part 2 of 42 Topics Functions That Return a Value
Parameter Passing Local Variables Miscellaneous hints Reading Sections 3.1 – 3.6
2
Functions 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 ; }
3
Using averageTwo #include <stdio.h>
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 ;
4
Parameter 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.
5
Parameter Passing (con’t)
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.
6
Local 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!
7
Parameter Passing and Local Variables
#include <stdio.h> 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 ; value value ave num num average int int float int int float
8
Same Name, Still Different Memory Locations
#include <stdio.h> 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 ; num num average num num average int int float int int float
9
Changes to Local Variables Do NOT Change Other Variables with the Same Name
#include <stdio.h> 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 OUTPUT int In addOne: num1 = 6 In main: num1 = 5
10
Sample Program #include <stdio.h>
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 ); result = funcA( 2.1, 3.5, 4.0 ); return 0; }
11
Sample Program (cont’d)
int funcA(int a, int b, int c ) { printf(" a = %d b = %d c = %d\n", a, b, c ); return ( a + b + c ); }
12
Sample Program Output [burt@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
13
Sample 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!
14
Sample 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 is 6, OK.
15
Sample 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!
16
Another Example #include <stdio.h> #include <stdlib.h>
int main( void ) { int negNum = -3; printf( "%d\n", abs( negNum ) * 2 ); return 0; }
17
Another Example Output
~]$ a.out 6 ~]$ =================== 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.
18
system( ) 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 <stdlib.h>
19
system( ) Function Example
#include <stdio.h> #include <stdlib.h> int main ( void ) { /* Clear the screen */ system( "clear" ); printf( "Hello, World!\n" ); return 0; }
20
Good 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.
21
Good Programming Style (cont’d)
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 lines of code and comments. Many functions have only one line of code.
22
Good Programming Style (cont’d)
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.