Download Functions in C - C Programming - Lecture Slides and more Slides Computer Science in PDF only on Docsity!
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
Identifier Scope (i.e., visibility)
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
Global scope
for variable a
Local scope for j, k
Local scope for x, y, z
Reading from a global variable
Writing to a global variable
- 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);
Syntax for Function Declaration,
Definition, and Use
Coding Standards for Functions
- Function names follow the same naming standard
as variables
- Explicitly declare all programmer-defined
functions before defining 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
Output Parameter Example
#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
"int *" is a pointer type, so the type of x or y is a pointer, which is a memory address
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 swap ( **int *** x, **int *** y)
{
int temp;
temp = ***** x;
***** x = ***** y;
***** y = temp;
return(0);
} // End swap
"int *" is a pointer type, so the type of x or y is a pointer, which is a memory address
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 in temp Docsity.com
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"
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);