03-09-2014, 12:57 PM
Functions in C
Functions.ppt (Size: 131.5 KB / Downloads: 13)
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
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);
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
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
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
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);