Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions Chapter 3 of the text Motivation:

Similar presentations


Presentation on theme: "Functions Chapter 3 of the text Motivation:"— Presentation transcript:

1 Functions Chapter 3 of the text Motivation:
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 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 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);) 142 E -1

2 Examples already seen int main(void) { … Function definition of
return 0; } 1 Function definition of the function main 2 printf(“Temperature is %.2f”,celsius); a call to the function printf 3 scanf(“%i”,&age); a call to the function scanf Prewritten functions like scanf and printf are packaged in libraries. #include<stdio.h> tells the compiler to use the standard input/output library (where printf and scanf are) 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 stdio.h printf, scanf, ... stdlib.h
contains many common functions, e.g. random number generators… also contains #define constants e.g. EXIT_SUCCESS math.h x see the list on page AP13 y is pow(x,y) is sqrt(x) ... You can ALSO define your own functions within a program. 142 E -3

4 Your own functions 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. function header function name function parameter function definition double cube(double var) { /*compute and return the cube of var*/ return (var*var*var); } function type (here double) the function MUST return a double Recall: C is strongly typed functions have a type 142 E -4

5 Calling a function 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; } a function can be anywhere an expression (of the same type) can be. 142 E-5

6 Declaring your own functions
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 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!) 142E-6

7 An Example prototype for 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); call to cube definition of cube 142E-7


Download ppt "Functions Chapter 3 of the text Motivation:"

Similar presentations


Ads by Google