New version page

UMBC CMSC 104 - Functions, Part 2 of 3

Upgrade to remove ads
Upgrade to remove ads
Unformatted text preview:

Functions, Part 2 of 3Slide 2Using 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 NameHeader FilesCommonly Used Header FilesUsing Header FilesCMSC 104, Version 9/01 1Functions, Part 2 of 3Topics•Functions That Return a Value•Parameter Passing•Local Variables•Header FilesReading•Sections 5.1 - 5.7CMSC 104, Version 9/01 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 ;}CMSC 104, Version 9/01 3Using 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 9/01 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.CMSC 104, Version 9/01 5Parameter 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 9/01 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.CMSC 104, Version 9/01 7#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 9/01 8#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 9/01 9Changes 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 9/01 10Header Files•Header files contain function prototypes for all of the functions found in the specified library.•They also contain definitions of constants and data types used in that library.CMSC 104, Version 9/01 11Commonly Used Header FilesHeader File Contains Function Prototypes for:<stdio.h> standard input/output library functions and information used by them<math.h> math library functions<stdlib.h> conversion of numbers to text, text to numbers, memory allocation, random numbers, and other utility functions<time.h> manipulating the time and date<ctype.h> functions that test characters for certain properties and that can convert case<string.h> functions that manipulate character stringsothers see Chapter 5 of textCMSC 104, Version 9/01 12Using Header Files#include <stdio.h>#include <stdlib.h>#include <math.h>int main ( ){ float side1, side2, hypotenuse ; printf(“Enter the lengths of the right triangle sides: “) ; scanf(“%f%f”, &side1, &side2) ; if ( (side1 <= 0) || (side2 <= 0) { exit (1) ; } hypotenuse = sqrt ( (side1 * side1) + (side2 * side2) ) ; printf(“The hypotenuse = %f\n”, hypotenuse) ; return 0


View Full Document
Download Functions, Part 2 of 3
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 3 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 3 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?