1 / 17

Math 130 Introduction to Computing Functions–(II) Lecture # 8

B Smith: Didn’t really use much Fa05; worked more extemporaneuosly in an emergent learning mode. Did hand out however. Math 130 Introduction to Computing Functions–(II) Lecture # 8. Learning Objectives. Discuss and identify the main components of a function: Review Functions:

hop
Download Presentation

Math 130 Introduction to Computing Functions–(II) Lecture # 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. B Smith: Didn’t really use much Fa05; worked more extemporaneuosly in an emergent learning mode. Did hand out however. Math 130Introduction to ComputingFunctions–(II)Lecture # 8

  2. Learning Objectives • Discuss and identify the main components of a function: • Review Functions: • header, body, arguments, returned variables • Use C’s returnstatement to return information to the calling function • Define a function prototype and illustrate how it should be be used • Use various library functions by incorporating the library via the proper header files • Use language references to understand the data types a function has been designed to receive and return • Create and use your own functions

  3. Functions Review (I) • Functions • return a single value • The returned value is substituted for the called value • Format for calling functions • FunctionName( argument ) ; • Example: printf( “%f” , sqrt( 16.0 ) ); • the function sqrt() is called, which returns the square root of its argument • all C library math functions return data type double • Arguments may be constants, variables, or expressions

  4. Function header identifies the function. provides the return type function name the parameters Function body The body is the function’s code It defines the function and value to be returned Functions Review (II) float myAvg(float a, float b) { float answer; answer = (a + b)/2; return answer; }

  5. B Smith: Show how every main() returns a value to the OS. Explore this using batch files, or simply run a function using system(“”), eg. int a = system(“hello.exe”) Returning Values • Functions should • contain at least one return statement • terminate after the return statement is executed • Parentheses after a return statement are optional • Valid usage of the return statement: return;/* no value returned */ return(answer); return answer;

  6. Mathematical Library Functions • Use #include <math.h> to get access to various math functions • Data-types of arguments and return values can be found in Appendix D, Bronson • The arguments passed to functions • can be any expression that evaluates to the function’s required data-type • If c2 = 10.0, a3=13.0, and d=2.0, what’s printed? printf( “%.2f”, sqrt( c2 + a3 * d ) );

  7. Math Library Functions

  8. Standard Library Functions • Access to many routines, ex, sin(2.64), sqrt(9.2), tan(PI/2) • <stdio.h> is what we typically #include to use printf() and scanf() • Two stdio.h functions we’ll look at are getchar()and putchar() • See Bronson, Appendix D, p. 619 for a full list of the fifteen header files

  9. Standard Library Functions • getchar() • typically used for single character input • it takes no argument • used as follows: char inChar;inChar = getchar(); • this is equivalent to the now familiar statement: scanf(“%c”, &inChar);

  10. Standard Library Functions • putchar() • typically used to output a single character to the display • it takes a single character argument • used as follows char inChar;putchar(‘F’); • this is equivalent to the now familiar statement printf(“%c”, ‘F’);

  11. B Smith: Consider using a do-while looop?? Example eg3.c #include<stdio.h> /* copy input to output; from K&R */ int main() { int c; c = getchar(); while (c != EOF) { putchar(c); c = getchar(); } }

  12. Example eg4.c #include<stdio.h> #include<stdlib.h> int main() { int nc; /* number of characters */ nc = 0; while ( getchar() != EOF ) ++nc; printf("%d\n", nc); system(“pause”); }

  13. B Smith: Stopped here on 02/11/05. putchar() and getchar() for text processing #define YES 1 #define NO 0 int main() /* count lines, words, chars in input */ { int c, nl, nw, nc, inword; inword = NO; nl = nw = nc = 0; while ( (c = getchar()) != EOF) { ++nc; if (c == '\n') ++nl; if(c == ' ' || c == '\n' || c == '\t') inword = NO; else if (inword == NO) { inword = YES; ++nw; } } //end while printf ("%d %d %d\n", nl, nw, nc); } B Smith: The most effective slide was this one. Generates discussion and interaction

  14. User Defined Functions • A function must be declared before being called • The function declaration is called a function prototype • A function prototype tells the calling function: • the data type of the returned value • the data type (and number of values) to send to the called function

  15. Function Prototypes • Function Prototypes • typically placed beforemain() • a prototype is unnecessary if you decide to place the “function definition” before int main(){…} • Convention • place main() before all functions • (at top of file)

  16. #include <stdio.h> double myAvg(float, float); int main() { . . . printf("The avg is %lf",myAvg(a, b) ); } double myAvg(float num1, float num2) { double answer; answer = (num1 + num2)/2; return answer; } Function Prototype Function Header Function Prototype vs Function Header

  17. Summary • Include function prototypebefore its use • The return statement can return either an expression, a literal value, or nothing • #include<math.h> defines the prototypes for the math functions • #include<stdio.h> defines the prototypes for the standard input/ouput functions

More Related