DOC PREVIEW
UMBC CMSC 104 - Functions, Part 2 of 42

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Functions, Part 2 of 42PowerPoint PresentationUsing averageTwoParameter PassingParameter Passing (con’t)Local VariablesParameter Passing and Local VariablesSame Name, Still Different Memory LocationsChanges to Local Variables Do NOT Change Other Variables with the Same NameSample ProgramSample Program (cont’d)Sample Program OutputSample Program Analysis - 1Sample Program Analysis - 2Sample Program Analysis - 3Another ExampleAnother Example Outputsystem( ) functionsystem( ) Function ExampleGood Programming StyleGood Programming Style (cont’d)Slide 22CMSC 104, Version 8/06 1L19Functions2.pptFunctions, Part 2 of 42Topics•Functions That Return a Value•Parameter Passing•Local Variables•Miscellaneous hintsReading•Sections 3.1 – 3.6CMSC 104, Version 8/06 2L19Functions2.pptFunctions 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 ;}CMSC 104, Version 8/06 3L19Functions2.pptUsing 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 ;}CMSC 104, Version 8/06 4L19Functions2.pptParameter 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.CMSC 104, Version 8/06 5L19Functions2.pptParameter 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.CMSC 104, Version 8/06 6L19Functions2.pptLocal 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!CMSC 104, Version 8/06 7L19Functions2.ppt#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 ;}value1 value2 ave num1 num2 average 5 8 int int float int int floatParameter Passing and Local VariablesCMSC 104, Version 8/06 8L19Functions2.ppt#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 ;} num1 num2 average num1 num2 average 5 8 int int float int int floatSame Name, Still Different Memory LocationsCMSC 104, Version 8/06 9L19Functions2.pptChanges to Local Variables Do NOTChange 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 5 OUTPUT int In addOne: num1 = 6In main: num1 = 5CMSC 104, Version 8/06 10L19Functions2.pptSample 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 ); printf( "result = %d\n", result ); result = funcA( 2.1, 3.5, 4.0 ); printf( "result = %d\n", result ); return 0;}CMSC 104, Version 8/06 11L19Functions2.pptSample 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 );}CMSC 104, Version 8/06 12L19Functions2.pptSample Program Output[burt@linux3 ~]$ a.out a = 3 b = 4 c = 5result = 12 a = 10 b = 6 c = 9result = 25 a = 2 b = 3 c = 4result = 9CMSC 104, Version 8/06 13L19Functions2.pptSample 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 = 5result = 12===================Looks good!CMSC 104, Version 8/06 14L19Functions2.pptSample Program Analysis - 2 result = funcA( 6 + 4, y + 2, 9 ); printf( "result = %d\n", result );===================a = 10 b = 6 c = 9result = 25===================6 + 4 is 10, OK.y is 4 + 2 is 6, OK.CMSC 104, Version 8/06 15L19Functions2.pptSample Program Analysis - 3 result = funcA( 2.1, 3.5, 4.0 ); printf( "result = %d\n", result );=================== a = 2 b = 3 c = 4result = 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!CMSC 104, Version 8/06 16L19Functions2.pptAnother Example#include <stdio.h>#include <stdlib.h>int main( void ){ int


View Full Document

UMBC CMSC 104 - Functions, Part 2 of 42

Download Functions, Part 2 of 42
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Functions, Part 2 of 42 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Functions, Part 2 of 42 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?