1 / 7

142 E -1

Functions. Chapter 3 of the text. Within a program, may have to perform the same computation over and over. Many programs share the same computation (e.g. sorting). To break a program into smaller pieces easier to write reusable for other problems.

chynna
Download Presentation

142 E -1

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. Functions Chapter 3 of the text Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) • To break a program into smaller pieces • easier to write • reusable for other problems • It may take some input parameters on which it computes (e.g. printf("7 squared is %i",7*7);) • it may compute and return a value (e.g. sqrt(windspeed);) Motivation: What is a function? • It is a named piece of code (e.g. printf) How is it used? • It is executed by giving its name 142 E -1

  2. Examples already seen 1 int main(void) { … return 0; } Function definition of the function main 2 printf(“Temperature is %.2f”,celsius); 3 scanf(“%i”,&age); #include<stdio.h> tells the compiler to use the standard input/output library (where printf and scanf are) a call to the function printf a call to the function scanf Prewritten functions like scanf and printf are packaged in libraries. You can use as many libraries as you want (check appendix B for a list of the libraries available with any C compiler) 142 E -2

  3. Useful libraries printf, scanf, ... contains many common functions, e.g. random number generators… also contains #define constants e.g. EXIT_SUCCESS see the list on page AP13 y x is pow(x,y) x is sqrt(x) ... • stdio.h • stdlib.h • math.h You can ALSO define your own functions within a program. 142 E -3

  4. Your own functions function header function name function parameter function type (here double) the functionMUST return a double function definition Recall: C is strongly typed functions have a type To write your own function: _ give it a name _ write the code it is to execute Example: write a function to compute the cube of a number. double cube(double var) { /*compute and return the cube of var*/ return (var*var*var); } 142 E -4

  5. Calling a function a function can be anywhere an expression (of the same type) can be. A function can be called from within main or any other function (note that main is just a particular function) int main(void) { double x,y,z; ... x=cube(y/2.0) ... z=cube(cube(x)); ... printf(“%f cubed is %f”,y,cube(y)); … return EXIT_SUCCESS; } 142 E-5

  6. Declaring your own functions For functions, we need to provide a prototype _ after the preprocessor commands (# lines) _ before the first function (the first piece of code between braces {} ), which is generally main just a rule of style (but follow it!) In C, identifiers (=names of things) MUST be declared BEFORE they are used. e.g. need to write int i, before using the variable i 142E-6

  7. An Example prototype for cube call to cube definition of cube #include <stdio.h> double cube(double value); int main(void) { double x,y; … y=cube(x); … return 0; } double cube(double var) { return (var*var*var); } 142E-7

More Related