Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.

Similar presentations


Presentation on theme: "C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file."— Presentation transcript:

1 C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file with other functions, or separately and then loaded as desired –Function prototypes must appear in any file where functions are to be used the prototype describes the function’s header (name, return type, number and type of params) – although you can also place these in header files for simplicity –Parameters are always passed by copy this means that the value of the parameter is copied into the parameter in the function’s header, and the parameter then becomes a local variable no values are passed back from the function via the parameter list returning a single value is done by the function’s return returning multiple values must be done by passing pointers (or returning a structure like an array)

2 Function Format If no return type, it defaults to int (not void) –void is used if the function is to not return any value, otherwise at least one return statement is expected Functions can be compiled separately –So information about the function must be made known explicitly either declare the function, as you would a variable –example: double afunction(int, int); or place a prototype prior to the function that uses it –example: void anotherFunction(int x, int y); Note: prototypes do not need variable names, so it could also be specified as –void anotherFunction(int, int); –this makes the prototype look just like the declaration, the only difference is placement – the prototype goes before the function that calls it, the declaration goes with the var and const declarations inside the function that calls it the prototype approach is more common but either is acceptable return-type name(param list) { var and const declarations executable statements return statement(s) } NOTE: in C, declarations must precede executable statements, you cannot mix them up as you do in Java

3 Pass By Copy This is the only form of parameter passing in C –The value stored in the parameter in the function call is copied into the parameter in the function header that is, the formal parameter is initialized with the value in the actual parameter from that point forward, the formal parameter is completely independent of the actual parameter changing the formal parameter does nothing to the actual parameter’s value –If you pass a pointer: then the formal parameter is a pointer, pointing at the same location as the actual parameter by using the pointer, you can change the value being pointed to so changing the value that the pointer points to changes the actual parameter changing the formal parameter means that you are changing the pointer, or the memory location being pointed at, not the value being pointed at, so changing the formal parameter does nothing to the actual parameter and will now cause your formal parameter to point somewhere else in memory –this could lead to run-time and/or logical errors! Using parameters appropriately can be very tricky –This is one of the biggest sources of logical errors in C programming but passing pointers can result in all kinds of problem – be very careful!

4 Example: Swapping As you are aware, swapping 2 values is a common routine – for instance, when sorting Here, we look at the wrong way and the right way to swap two values in a function void swap(int a, int b) { int temp = a; a = b; b = temp; } The wrong way to swap When called with swap(x, y); x and y will remain the same void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } The right way to swap When called with swap(&x, &y); where x and y are int values, then a points to x and b points to y, so the values pointed to are swapped, not the pointers themselves

5 extern Global variables are available within a file by declaring variables prior to the functions –Global variables are available between files using the reserved word extern to define them The extern declaration appears in the files that are using the externally declared variables, not in the file where they were originally declared –So, if file 1 declares variables and file 2 wants to use those variables, you use an #include statement to load file 1 and extern statements to declare any of file 1’s global variables to be used in file 2 –Take care when using global variables, they are not encouraged and lead to hard-to-read and hard-to-debug code In fact, I would recommend that you avoid them if at all possible! NOTE that extern defines an external variable, but does not declare it, a separate declaration is required although they can be combined into a single statement as in: –extern int x;

6 Scope Rules Scope for a variable (or a function) are the locations within a program where that variable (or function) can be accessed There are generally two types of scope: local and global –A local variable is defined within the local environment: the current block or the current function Variables declared within { } are local to that block, whether the block is a block within a function or the function itself Variables declared within a for-loop are local to that for-loop Parameters in a function header are local to that function Note that if two variables share the same name but are in different blocks or functions, then the variable declared in this environment will be the one used in a reference –A global variable is defined outside of the local environment and available anywhere within the file, or in other files by using extern

7 Scope Example with Blocks #include void func1(int, int); void func2(int, int); void main() { int x = 5, y = 10; printf("Values before first function call: %d %d\n", x, y); func1(x, y); { int x = 15, y = 20; printf("Values inside block: %d %d\n", x, y); func2(x, y); } printf("Values before after block: %d %d\n", x, y); } void func1(int x, int y) { x++; y--; printf("Values inside func1: %d %d\n", x, y); } void func2(int x, int y) { x+=2; y-=2; printf("Values inside func2: %d %d\n", x, y); { int x = 0, y = 1; printf("Values inside func2's block: %d %d\n", x, y); }

8 Header Files In order to call upon functions compiled in separate files, you need to include their definition as a declaration or a prototype –for simplicity, if you have functions in several files, each of which call upon some of the same functions, you can place the prototypes in a single file, called a header file all other shared definitions and declarations can go here as well A header files typically only contain definitions and declarations, not executable code –consider as an example a calculator program that has its’ functions split into multiple files: a main function in one file which calls upon stack operations in a file stack.c a parsing operation to get tokens from a string in the file getop.c a function to get char input in the file getch.c a header file contains prototypes and common declarations called calc.h

9 Static and Register The reserved word static is used to define a variable as being static, typically used with a variable defined using extern –Static means that the storage of that variable remains in existence Local variables are removed from memory once the function terminates, a static variable remains in memory so that it can be accessed later The register reserved word is used to suggest to the compiler that a particular variable should be moved to and kept in a register –The idea is to give the compiler some advice –The register declaration is only permitted for local variables and parameters and the advice is not necessarily taken by the compiler Just because you place register in front of a variable’s declaration does not mean that the variable will be placed into a register

10 Macro Substitution We saw #define can be used for constants –Form: #define name substitution-text Example: #define MAX 10 –But unlike a constant in other languages as a variable whose value does not change here the definition is a form of macro substitution –all instances of the name are replaced by the value this can be used in some interesting ways, such as in the following: –#define forever for( ; ; ) /* infinite loop */ –#define max(A, B) ((A) > (B) ? (A) : (B)) /* defines a max operation */ #define is actually a preprocessing directive – that is, an activity to be performed by the compiler prior to attempting compilation

11 Optional Parameters C permits functions to have optional parameters –The format is returntype name(params, …) That is, the … indicates that further parameters can be passed The … must be listed only after the required parameters –Notice that, since you specify the parameters as …, you do not know their names! How then can you use these additional parameters (if they were passed)? –The stdarg.h file contains the definition of va_list (variable argument list) so that you can step through the optional parameters Declare a variable of type va_list Use the macro va_start which initializes your variable to the first of the optional params The function va_arg returns the next argument –We will skip further coverage of this topic, but if you are interested, consult your C textbook which should talk about it


Download ppt "C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file."

Similar presentations


Ads by Google