Functions
Mao
● Keyword: return ● Functions and how to define them ● How to use arguments and return values ● ANSI C prototypes ● Function types ● Using Header Files ● Local or Global Variables
function f(x) = x2
In mathematics, a function maps an input value of one type (the domain) to an output value of another type (the range).
A function is a self-contained unit of program code designed to accomplish a particular task.
C’s design philosophy is to use functions as building blocks. We’ve already relied on the standard C library for functions such as printf() , scanf() , getchar() , putchar() , and strlen() .
Here is an example of a typical function declaration:
}
return …
the return type void if the method does not return a value.
the parameter list in parentheses
the method body, enclosed between braces
It uses the starbar identifier in three separate contexts:
● 1) a function prototype that tells the compiler what sort of function starbar() is,
void starbar(void); /* prototype the function */
● 2) a function call that causes the function to be executed,
starbar();
● 3) and a function definition that specifies exactly what the function does.
void starbar(void) /* define the function */ {
…
}
Returning a Value from a Function with return
The keyword return causes the value of the following expression to be the return value of the function. In this case, the function returns the value that was assigned to min . Because min is type int , so is the imin() function.
Returning a Value from a Function with return
scanf() returns the number of items successfully read, so input other than two integers will cause the while loop to terminate.
Here is a sample run:
A function prototype or declaration states the return type, the number of arguments, and the types of those arguments. You can declare the imin function the following prototypes:
int imin(int, int); int immx(int a, int b);
The first form uses a comma-separated list of types. The second adds variable names to the types. Remember that the variable names are dummy names and don’t have to match the names used in the function definition. However, it is recommended to match them.
Suppose you give a prototype like this:
void print_name();
An ANSI C compiler will assume that you have decided to forego function prototyping, and it will not check arguments. To indicate that a function really has no arguments, use the void keyword within the parentheses:
void print_name(void);
ANSI C interprets the preceding expression to mean that print_name() takes no arguments. It then checks to see that you, in fact, do not use arguments when calling this function.
Prototypes are a strong addition to the language. They enable the compiler to catch many errors or oversights you might make using a function.
Functions should be declared by type.
● A function with a return value should be declared the same type as
the return value.
● Functions with no return value should be declared as type void . In a void function, you can use a statement like this:
return;
It causes the function to terminate and return control to the calling function. Because no expression follows return , no value is returned, and this form should be used only in a type void function.
For example, the following function heading indicates that you are defining a function that takes two type int arguments but that returns a type double value:
double klink(int a, int b)
The following function heading indicates that it takes two int arguments and returns a type int value:
int imax(int n, int m)
Mismatched arguments
Mismatched arguments can cause errors. Although proto.c gives no error message, the compiler may give a warning to the effect that a double was converted to int and that there was a possible loss of data. For example, the call
imax(3.9, 5.4)
becomes equivalent to the following:
imax(3, 5)
If you put main() in one file and your function definitions in a second file, the first file still needs the function prototypes. Rather than type them in each time you use the function file, you can store the function prototypes in a header file.
That is what the standard C library does, placing I/O function prototypes in stdio.h and math function prototypes in math.h, for example. You can do the same for your function files.
An example of using header files can be referred to the following files: usehotel.c hotel.c hotel.h
In a programming language, each variable has a particular scope attached to them. The scope is either local or global.
Variables that are declared inside a function or block are called local variables. They can be used only by statements that are inside that function or block of code.
Global variables do not stay limited to a specific function, which means that one can use any given function to access and modify the global variables.
#include <stdio.h> int x = 5; // global variable int test(void); int main() {
int y = 10; // local variable
x = 3;
return 0;
} int test(void) {
x = 4; y = 6; // Error: y is not defined in test function. return 0;
}
interchange() does swap the values of u and v. But interchanging the values of u and v has no effect on x and y !
After a function completes its execution, all its local variables are destroyed. These variables are also known as automatic variables.
Static local variables are permanently allocated in the memory for the lifetime of the program. To declare a static variable, use the keyword static.
● Stephen Prata. C Primer Plus (6th Edition). Addison-Wesley
Professional, 2013.