180 likes | 349 Views
Functions in C . Function Terminology. Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and type Function prototype Input parameter, output parameter Pass by value, pass by reference. Sample Program. int a = 1;
E N D
Function Terminology • Identifier scope • Function declaration, definition, and use • Parameters and arguments • Parameter order, number, and type • Function prototype • Input parameter, output parameter • Pass by value, pass by reference
Sample Program int a = 1; int findMax(int x, int y); int main(void) { const int j = 10; int k; k = findMax(a, j); return (0); } // End main int findMax(int x, int y) { int z; if (x < y) z = y; else z = x; a = z; return (z); } // End findMax
Identifier Scope (i.e., visibility) Global scope for variable a int a = 1; int findMax(int x, int y); int main(void) { const int j = 10; int k; k = findMax(a, j); return (0); } // End main int findMax(int x, int y) { int z; if (x < y) z = y; else z = x; a = z; return (z); } // End findMax Reading from a global variable Local scope for j, k Writing to a global variable Local scope for x, y, z
Syntax for Function Declaration, Definition, and Use • Declaring a function tells the compiler the function exists. The parameters tell the order, number, and type of arguments the function expects and the type of the value the function returns. The identifier for each parameter is optional but recommended. A function declaration is also called a function prototype float minValue (float x, float y); • Defining a function tells the compiler what the function does float minValue (float x, float y) { if (x < y) return x; else return y; } // End minValue • Using (calling) a function tells the compiler to switch control to this function and pass the argument values to the function c = minValue(a,b);
Example of Declaration, Definition, Use int a = 1; int findMax(int x, int y); int main(void) { const int j = 10; int k; K = findMax(a, j); return (0); } // End main int findMax(int x, int y) { int z; if (x < y) z = y; else z = x; a = z; return (z); } // End findMax Function declaration (prototype) Function definition (and declaration) Function use (and implicit declaration) Function definition (and declaration)
Coding Standards for Functions • Function names follow the same naming standard as variables • Explicitlydeclare all programmer-defined functions beforedefining or using them. This is done by placing all function prototypes just above the definition of the function main • If the function main is defined in a source code file, it's definition should occur before the definition of any other functions in the file
Location of Function Parts in a File // Include files #include <stdio.h> // Global constants and types // Function prototypes int main (void) { // Function calls return (0); } // End main // Function definitions
Function Parameters and Arguments • All arguments in C are "pass by value". This means that the only way to get a value out of a function is by using the return keyword and giving it the value • Consequently, a function can never return more than one value and all function parameters are really only input parameters • However, "pass by reference" can be implemented in C through the use of pointers (i.e., memory addresses) • This allows function parameters to "act like" output parameters
Output Parameter Example "int *" is a pointer type, so the type of x or y is a pointer, which is a memory address #include <stdio.h> int swap (int *x, int *y); int main (void) { int a = 27; int b = 56; // swap(a, b); // Wrong swap (&a, &b); // Right return(0); } // End main The * here is NOT an operator in this case. It is a decoration that goes with the type The & is the "address of" operator. It returns the memory address of the variable This is read as "the address of a"
Output Parameter Example (continued) "int *" is a pointer type, so the type of x or y is a pointer, which is a memory address int swap (int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; return(0); } // End swap The * here is NOT an operator in this case. It is a decoration on the variable name This * here IS an operator. It is called the indirection operator. In the location named temp, store the value pointed to by x In the location pointed to by x, store the value pointed to by y In the location pointed to by y, store the value intemp
Interpreting the & Operator • & is the address operator (among other uses in C) • When applied to a variable, the & returns the memory address of that variable. That memory address value is then passed as an argument to the function swap (&a, &b); • This memory address can be thought of as the address of the "mailbox" with the variable name written on the outside (i.e., this is the "mailbox" of currentAmount) • By passing a variable's "mailbox" address as a parameter to a function, the function can then put a value into the "mailbox" • This is the first step the programmer needs to do to implement "pass by reference" • After the function call is complete, the value of the variable is the value that the function put in the "mailbox"
Interpreting the * Operator • * is the indirection operator (among other uses in C) • When applied to a variable on the right side of an assignment statement, the * operator returns the value located in the "mailbox" of the memory address contained in the variable. temp = *x; • When applied to a variable on the left side of an assignment statement, the * operator stores the value on the right side of the assignment statement into the "mailbox" of the memory address contained in the variable on the left side. *y = temp; • If the * indirection operator occurs on both sides of the assignment statement, it means the following for the example below. The value pointed to by y is stored in the memory location pointed to by x *x = *y;
Interpreting the * Operator (continued) • In the parameter list of a function, an output parameter is created by declaring the parameter as a pointer to the value that the function will "return" int swap (int *x, int *y); • This is the second step the programmer needs to do to implement "pass by reference"
Standard C Library Functions • A program can call on a large number of functions from the Standard C library • These functions perform essential services such as input and output • They also provide efficient implementations of frequently used operations • All the functions in the C library are declared in one of the standard header files. One of these files is stdio.h • Check out references to the Standard C Library for more information
printf and scanf Functions • The printf and scanf functions are declared in the source code file named stdio.h that is located in the standard …\include folder location. Their prototypes are inserted into a program's source code file by using #include <stdio.h> • The printf and scanf functions are defined in the object code library file named libc.a that is located in the standard …\lib folder location • The printf and scanf functions are used by placing them in a program's source code file printf("The value is %d", accountValue); scanf("%f", &salesAmount);