/
Lecture [7][0] Functions Lecture [7][0] Functions

Lecture [7][0] Functions - PowerPoint Presentation

tatiana-dople
tatiana-dople . @tatiana-dople
Follow
342 views
Uploaded On 2019-12-02

Lecture [7][0] Functions - PPT Presentation

Lecture 70 Functions Dr Siming Liu Had a baby boy at 1100 am yesterday CS 135 Lecture 70 1 Lecture 70 Functions Dr Sushil Louis CS 135 Lecture 70 2 quick quiz 1 Write a c program that prints a symmetric Pascals matrix ID: 768880

average function return lecture function average lecture return 135 double int printf type variables argument arguments variable definition sum

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Lecture [7][0] Functions" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Lecture [7][0]Functions Dr. Siming LiuHad a baby boy at 11:00 a.m. yesterday CS 135 - Lecture [7][0] 1

Lecture [7][0]Functions Dr. Sushil LouisCS 135 - Lecture [7][0] 2

quick quiz 1. Write a c program that prints a symmetric Pascal’s matrix:The Pascal’s matrix has the following rules:An element of the matrix s[i ][j] = if the row is 0, or the column is 0, then it is equal to 1 else, s[ i ][j] is equal to the element above plus the element to the left in the matrix. CS 135 - Lecture [6][1] 3

Do you have a working program? if ( yes ) good for you!else{ this will be the topic of next week’s workshop please, attend }* this quiz will not be graded... this time CS 135 - Lecture [6][1] 4

Average Average of 10.0, 20.0  (10.0 + 20.0)/2.0  15.0Average of 75.0, 85.0  (75.0 + 85.0)/2.0  80.0 Average of 10.0, 20.0, 30.0  (10.0 + 15.0 + 30.0)/3.0  18.3333333 …Average of n numbers is sum of the n numbers divided by n CS 135 - Lecture [7][0] 5

Example: Computing Averages double average ( double a , double b ) { /* computes the average of two double values */ return ( a + b) / 2; }The parameters represent the numbers that will be supplied when average is called. The type of the value returned the function’s parameters (and their type) The value returned by the function CS 135 - Lecture [7][0] 6

Calling the Function Arguments can be:Variables, orAny expression of a compatible typeCS 135 - Lecture [7][0] 7 avg = average ( x , y ) ; avg = average ( 5.1 , 8.9 );avg = average(x /2, y /3); These are arguments to the function

What Happens When the Function is Called? The values of the arguments are copied into the parameters a and b: pass by value CS 135 - Lecture [7][0] 8 double average ( double a , double b ) { /* computes the average of two double values */ return (a + b) / 2; } 5.1 a 8.9 b avg = average ( 5.1 , 8.9 ) ; When the body of the function is executed, a and b will have the values passed on by the arguments:

Arguments: Passed by Value /* Demonstrates pass by value concept */ #include < stdio.h > double average ( double a , double b ) { double avg ; avg = (a + b) / 2; a = b = 0 ; /* clear a and b */ return avg ; } int main () { double avg , x = 5.1 , y = 8.9 ; /* the return value is discarded */ avg = average(x, y); printf("x = %.2f y = %.2f", x, y); return 0;} CS 135 - Lecture [7][0] 9 a b 5.1 x 8.9 y 5.1 8.9 0 0 Argument values are assigned to the parameters : Changes made to parameters inside the function do not affect the arguments

Use of Return Values The return value could just be used and discarded:printf(" Avg.: %g \n " , average ( x , y )) ; The return value could be saved in a variable: avg = average ( x , y ) ;

Use of Return Values Ignoring the return value of average is an odd thing to do, but for some functions it makes sense.Example: printf returns the number of characters that it prints (9 in this case): num_chars = printf ( " Hi, Mom! \n " ) ; We normally discard printf ’s return value: printf ( " Hi, Mom!\n"); /* discards return value */CS 135 - Lecture [7][0]11

Program: Computing AveragesThe average.c program reads three numbers and uses the average function to compute their averages, one pair at a time: Enter three numbers: 3.5 9.6 10.2 Average of 3.5 and 9.6: 6.55 Average of 9.6 and 10.2: 9.9 Average of 3.5 and 10.2: 6.85 CS 135 - Lecture [7][0] 12

average.c /* Computes pairwise averages of three numbers */ #include < stdio.h > double average ( double a , double b ) { return ( a + b) / 2; } int main(void) { double x, y, z; printf ( " Enter three numbers: " ) ; scanf ( " % lf%lf%lf " , & x , & y, &z); printf("Average of %g and %g: %g\n", x, y, average(x, y)); printf("Average of %g and %g: %g\n", y , z , average ( y, z)); printf("Average of %g and %g : %g \n", x, z, average(x, z )); return 0 ; } CS 135 - Lecture [7][0] 13

Functions with no Return Values void print_count ( int n ) { printf ( " T minus %d and counting \n " , n ) ; } The return value is voidCS 135 - Lecture [7][0]14

Functions with no Return Values Call print_count 10 times in a loop: for ( i = 10 ; i > 0 ; -- i ) print_count ( i);No return value, so we cannot assign the result of print_count to a variable!CS 135 - Lecture [7][0] 15

Functions with no Parameters void print_pun (void ) { printf ( " To C, or not to C: that is the question. \n " ) ; } Calling a function with no arguments: print_pun () ; /* The parentheses must be present*/ print_pun; /*** WRONG ***/Write void in parentheses after the function’s name CS 135 - Lecture [7][0] 16

General Form of a Function Definition return-type function-name ( parameters ) { declarations statements } double average ( double a , double b ) { /* computes the average of two double values */ return ( a + b) / 2; } List of:param1-type param1-name,… paramN-type paramN-name CS 135 - Lecture [7][0] 17

Return Values Rules governing the return type:Functions may not return arraysSpecifying that the return type is void indicates that the function does not return a value. If the return type is omitted in C89, the function is presumed to return a value of type int In C99, omitting the return type is illegal C99 CS 135 - Lecture [7][0] 18

Function Definitions – Style As a matter of style, some programmers put the return type above the function name:double average( double a , double b ) { return ( a + b ) / 2 ; }Putting the return type on a separate line is especially useful if the return type is lengthy, like unsigned long int.CS 135 - Lecture [7][0]19

Variable Use in Functions double average( double a , double b ) { double sum ; /* declaration */ sum = a + b ; /* statement */ return sum / 2 ; /* statement */ }Variable sum can only be used inside the function average In C89, variable declarations must come before all statements in the body of a function (this includes main ). Variable declarations and statements can be mixed: each variable must be declared prior to the first statement that uses it C99 CS 135 - Lecture [7][0] 20

Program: Testing Whether a Number Is Prime The prime.c program tests whether a number is prime: Enter a number: 34 Not primeUse a function named is_prime that returns true if its parameter is a prime number and false if it is not. Strategy: divide the parameter n by each of the numbers between 2 and the square root of n ; if the remainder is ever 0, n is not prime.CS 135 - Lecture [7][0] 21

prime.c /* Tests whether a number is prime */ #include < stdbool.h > /* C99 only */ #include < stdio.h > bool is_prime ( int n ) { int divisor; if (n <= 1) return false; for (divisor = 2 ; divisor * divisor <= n ; divisor ++) if ( n % divisor == 0 ) return false; return true;}int main(void){ int n ; printf ("Enter a number: " ); scanf(" %d" , &n); if (is_prime( n)) printf( " Prime\n ") ; else printf(" Not prime \n " ) ; return 0 ; } CS 135 - Lecture [7][0] 22

Function Declarations CS 135 - Lecture [7][0]23

Function Declarations #include < stdio.h > int main ( void ) { double x , y , z ; printf(" Enter three numbers: "); scanf ("% lf%lf%lf", &x, & y, &z ) ; printf ( " Average of %g and %g : %g \n " , x , y , average ( x , y)); printf("Average of %g and %g: %g\n", y, z, average(y, z)); printf("Average of %g and %g: %g\n", x, z, average( x , z)); return 0 ; } double average (double a , double b ) { return ( a + b ) / 2;} Also legal: function definition is after the call in main the compiler assumes that average returns an int value ( implicit declaration ) CS 135 - Lecture [7][0] 24

Call-Before-Definition Problem avg = average ( x , y ) ; The compiler is unable to check that we are passing the right number of arguments and that the arguments have the proper type . Default argument promotions are performed. When the definition of average is discovered we get an error message The function’s return type is actually double CS 135 - Lecture [7][0] 25

Call-Before-Definition SolutionArrange the program so that the definition of each function precedes all its calls.Unfortunately, such an arrangement does not always exist.Sometimes, this may make the program harder to understand (function definitions are placed in an unnatural order.) CS 135 - Lecture [7][0] 26

Call-Before-Definition Solution Use a function declaration: gives the compiler a brief summary of the function’s return value and parametersGeneral form of a function declaration: return-type function-name ( parameters ) ; The declaration of a function must be consistent with the function’s definition! CS 135 - Lecture [7][0] 27

Function Prototypes #include < stdio.h > double average ( double a , double b ) ; /* DECLARATION */ int main (void){ double x , y, z ; printf ("Enter three numbers: ") ; scanf( " % lf%lf%lf " , & x , & y , & z ) ; printf ( " Average of %g and %g: %g\n", x, y, average(x, y)); printf("Average of %g and %g: %g\n", y, z, average(y, z)); printf (" Average of %g and %g : %g\n ", x, z, average ( x, z)); return 0 ; } double average (double a , double b ) /* DEFINITION */ { return ( a + b ) / 2 ; } This is called a function prototype CS 135 - Lecture [7][0] 28

Function Prototypes C also has an older style of function declaration in which the parentheses are left empty.double average ( double , double ) ; It is usually best not to omit parameter names. Putting parameter names in your prototypes makes the function declaration more readable. Parameter names are not required, as long as their types are present CS 135 - Lecture [7][0] 29

Function Declarations In C99, either a declaration or a definition of a function must be present prior to any call of the function.Calling a function for which the compiler has not yet seen a declaration or definition is an error. C99 CS 135 - Lecture [7][0] 30

Organizing a C ProgramRecommended ordering: #include directives#define directivesType definitionsDeclarations of external variables Prototypes for functions other than mainDefinition of mainDefinitions of other functions CS 135 - Lecture [7][0] 31

Organizing a C ProgramGood practice to have a boxed comment preceding each function definition Information to include in the comment:Name of the functionPurpose of the functionMeaning of each parameterDescription of return value (if any)Description of side effects (such as modifying external variables) CS 135 - Lecture [7][0] 32

More on Arguments CS 135 - Lecture [7][0]33

Arguments We can use parameters as variables within the function, reducing the number of genuine variables needed. /* computes x to power n */ int power ( int x , int n ) { int i , result = 1; for (i = 1; i <= n; i++) result = result * x; return result ; } n is a copy of the original parameter, so we can modify it inside the function CS 135 - Lecture [7][0] 34

Arguments: re-Writing power Removes the need for variable i: int power ( int x , int n ) { int result = 1 ; while (n-- > 0) result = result * x ; return result; } CS 135 - Lecture [7][0]35

Challenges of Pass by Value Requirement Example: a function that decomposes a double value into an integer part and a fractional partA function cannot return two numberstry passing a pair of variables to the function and having it modify them: void decompose ( double x , long int_part , double frac_part ) { int_part = (long) x; frac_part = x - int_part ; } CS 135 - Lecture [7][0] 36

Arguments What happens when we call the function? decompose(3.14159 , i , d ) ; i and d will not be affected by the assignments to int_part and frac_part . Chapter 11 shows how to make decompose work correctly. CS 135 - Lecture [7][0] 37

Argument ConversionsHappen during function calls in which the types of the arguments do not match the types of the parameters. Rules for argument conversions: the compiler has seen a prototype for the function (or the function’s full definition) prior to the call.the compiler has NOT seen a prototype for the function (or the function’s full definition) prior to the call. CS 135 - Lecture [7][0] 38

Argument Conversions If the compiler has encountered a prototype prior to the call:The value of each argument is implicitly converted to the type of the corresponding parameter as if by assignment.Example: If an int argument is passed to a function that was expecting a double, the argument is converted to double automatically. CS 135 - Lecture [7][0] 39

Argument Conversions If the compiler has not encountered a prototype prior to the call:The compiler performs the default argument promotions:float arguments are converted to double.The integral promotions are performed, causing char and short arguments to be converted to int . (In C99, the integer promotions are performed.) CS 135 - Lecture [7][0] 40

Argument Conversions #include < stdio.h> int main ( void ) { double x = 3.0 ; printf ( "Square: %d\n", square( x)); return 0; } int square (int n ) { return n * n ; } Relying on the default argument promotions is dangerous. At the time square is called, the compiler doesn’t know that it expects an argument of type int . The effect of calling square is undefined CS 135 - Lecture [7][0] 41

Argument Conversions - Solution Casting square’s argument to the proper type:printf (" Square: %d \n " , square (( int ) x )) ; Better solution: provide a prototype for square before calling it. In C99, calling square without first providing a declaration or definition of the function is an error. C99 CS 135 - Lecture [7][0] 42

Variable scope and duration CS 135 - Lecture [7][0]43

Key Properties of Variables Storage duration: the portion of program execution during which storage for the variable existsScope: the portion of the program text in which the variable can be referencedVariables can be local or global ( external ) CS 135 - Lecture [7][0] 44

Local Variables – Storage A variable declared in the body of a function is said to be local to the function int sum_digits ( int n ) { int sum = 0 ; while (n > 0) { sum += n % 10; n /= 10 ; } return sum ; } Automatic storage duration: storage is automatically allocated at function call and then deallocated when the function returns sum: CS 135 - Lecture [7][0] 45

Local Variables - Scope A variable declared in the body of a function is said to be local to the function int sum_digits ( int n ) { int sum = 0 ; while (n > 0) { sum += n % 10; n /= 10 ; } return sum ; } Block scope: visible from the point of declaration to the end of the function CS 135 - Lecture [7][0] 46

Static Local Variables Static variables have a permanent storage location: they retain their value throughout the execution of the program.void f( void ) { static int i ; … } A static local variable has block scope In memory contents 00110100 i address: Always at the same location CS 135 - Lecture [7][0] 47

ParametersParameters have the same properties as local variables: automatic storage duration and block scopeEach parameter is initialized automatically when a function is calledby being assigned the value of the corresponding argument CS 135 - Lecture [7][0] 48

External Variables External (global) variables are declared outside the body of any functionStatic storage durationFile scope : visible from its point of declaration to the end of the enclosing fileUse of external variables: Transmit information to functions (other than parameters) CS 135 - Lecture [7][0] 49

Pros and Cons of External Variables Pros: convenient when many functions must share a variable or when a few functions share a large number of variables.Cons:Maintenance: e.g.: changing an external variable’s type Hard to debug: it is difficult to identify the guilty function that incorrectly changes the variableFunctions that rely on external variables are hard to reuse in other programs. CS 135 - Lecture [7][0] 50

Readings CS 135 - Lecture [7][0]51 Chapter 9 Chapter 10 (read ahead)