Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions Dilshad M. Shahid New York

Similar presentations


Presentation on theme: "Functions Dilshad M. Shahid New York"— Presentation transcript:

1 Functions Dilshad M. Shahid New York University @1999

2 Today Header files Call by value vs Call by Reference Examples

3 Header files Each standard library has a corresponding header file The header file contains: –function prototypes for all the functions in that library –definitions of various data types and constants needed by those functions

4 Header files Examples These and more in table on page 159 of your text

5 Header files You can create custom header files Write up a program with all your variables and functions (including prototypes) but leave out the main() ) Name it so that it ends in.h, e.g. stuff.h If you want to use the functions in the.h file, in your primary program (i.e. your.c program) put the.h file as an #include at the top

6 Header files So for example: Your.h file is called stuff.h Your primary program is first.c In first.c, right after #include, put in #include “stuff.h” <> tells you that the.h file is a standard header “ “ tells you that the.h file is a custom header

7 Call by value vs Call by Reference These are 2 ways to invoke functions In call by value, when arguments are passed, a copy of the argument’s value is made and passed to the called function Changes to the copy don’t affect the original variable’s value in the caller

8 Call by value vs Call by Reference If you change the value of a variable inside a function and want the caller of the function, usually main(), to see the new value, you must return it Remember you can only return one value per function The return statement must be the last statement before the closing } of the function

9 Call by value vs Call by Reference Example on webpage

10 Call by value vs Call by Reference In call by reference, the caller allows the called function to modify the original variable’s value In C, all calls are call by value However, in C, it is possible to simulate call by reference by using address operators and indirection pointers


Download ppt "Functions Dilshad M. Shahid New York"

Similar presentations


Ads by Google