We have used functions in every program that we have discussed so far like main(), printf(), scanf (), getchar() , etc., C functions can be classified into two categories namely LIBRARY functions and USER DEFINED functions. printf(), scanf(), sqrt(),etc., belong to the category of library functions. The main distinction between these categories is that library functions are not required to be written by the user, whereas a User defined function has to be developed by the users at the time of writing a program. However, a user-defined function can later become a part of the C program library.
Function definition
A function definition introduces a new function by declaring the type of value it returns and is parameters. and specifying the statements that are executed when the function is called. Consider the following example
Example 5.1
#include <stdio.h>
int main()
{
printline() ;
printf ( "The llustrates the usage of functions \n”);
printline();
}
/* The statement defines a function called printline, which prints a line of 40 characters. This functions has been called from the main(). */
printline()
{
int i;
for (i=0; i<40; i++)
printf("-");
}
The function in which the function call is contained is known as the calling function and the function named in the call is said to be the called function. In the above example, main() is the calling function and printline () is the called function. The function printline() returns no value (void). When a function is required to return a value, the return type has to be specified explicitly. The general form of C function is given below
function_name (argument list)
argument declaration;
{
variable declarations;
function statement
}
Arguments
Consider the simple example given below
Example 5.2
#include <stdio.h>
int main()
{
int num1=3, num2 =334, sum;
sum=nun1+num2 ;
printf ("The sum of two numbers is %d\n", sum) ;
}
When the printf function is called, one or more values are supplied to this function, the first value being the format string and the remaining values the specific program results to be displayed. These values, called arguments. greatly increase the flexibility of a function.
For example, a function such as one given below, can be defined.
copy (name 1, nane2)
Here, copy is a user defined function name, which accepts two arguments - name1 and nane2 respectively.
The following example illustrates how function is declared and how a function returns value to the calling function
Example 5.3
#include <stdio.h>
int main()
{
int number, raise, result;
float power (int, int); /* Function prototyping */
printf("Enter the number \n");
printf("Enter the number to which the number has to be raised\n");
scanf ("%d%d", &number, &raise);
result = power (number, raise);
printf ("The result is %f\n", result);
}
float power (no,r)
int no;
int r;
{
int count;
float res = 1.0;
for (count =0; count < r; count++)
res*no;
return(res);
}
The argument list contains valid variable names separated by commas. The list must be surrounded by parenthesis. Note that no semicolon follows the closing parenthesis. The argument variable receives values from the calling function, thus providing a means for data communication from the calling function to the called function.
Al argument variables must be declared for their types after the function header and before the opening brace of the function body. For example
power (x,n)
float x;
int n;
{
program statements;
}
The same function can also be declared as
power (float x, int n)
{
program statements;
}
Passing values to functions
Arguments to a function are usually passed in two ways. The first is termed as call by value. In call by value, a copy of the variable is made and passed to the function as argument. With this method, changes made to the parameters of the on the function have no effect on the variables which called the function, because the changes are made only to the copies. On the other hand, call by reference is a method in which the address of each argument is passed to the function. By this method, the changes made to the parameters of the function will affect the variables which called the function. An example to demonstrate call by reference and call by value is the swap program. Both the correct and the incorrect versions are listed below.
Example 5.4
#include <stdio.h>
/* Incorrect Version */
main()
{
int a=1, b2;
swap (a,b);
printf("a=%d and b=%d\n", a,b) ;
}
swap (int a, int b)
{
int temp;
temp=a;
a=b;
b=temp;
}
The output will be
a= 1 and b=2 (The values of variables are not exchanged)
/* Correct Version */
main()
{
int a=1, b=2;
swap (&a, &b);
printf("a=%d and b=%d\n",a, b);
}
swap (int *a, int b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
The output of the program will
a=2 and b=1 (The values are exchanged)
In the incorrect version, swap is done only on the copies of the variables a and b, But in the correct version, swap is done on the original variables.
When a pointer is passed as an argument to a function, the pointer itself is copied but the object it is pointing to is not copied. Hence, any change made to the pointer parameter by the called function does not affect the pointer supplied as argument to the function, which is consistent with the call by value parameter passing, However. using the pointer supplied as the argument.
Example 5.5
#include <stdio.h>
void change (int *ip)
{
++(*ip); /* increment what ip is pointing to */
ip = NULL;
}
int main (void)
{
void change (int *); /* function prototyping*/
int I=0, *ip, *pi;
ip = pi = &I;
change (ip);
printf("new value of I= %d\n",I):
if (ip == pi)
printf( "No change in ip\n");
return 0;
}
The output of the program is
New value of I=1;
No change in ip.
The & operator can be applied to a parameter. However, the address of a parameter is not the address of the argument, but the address of the copy of the argument.
Return value
A function may or may not send back any value to the calling function. If it does, it is done through the RETURN statement. While it is possible to pass to the called function any number of values, but the called function can only return one value per call at the most. The return statement can take one of the following forms
return;
or
return(expression);
The first return statement does not return any value: it acts like the closing brace of the function when return is encountered -- the control is immediately passed back to the calling function. For example
if (error)
return;
The following statement returns the value of the variable product
mu1 (number1, number2)
{
int product;
product = number1 * number2;
return (product);
}
The last two statements can be combined into one statement as given blow
return (number1 * number2)
Return value based on conditions
A C function may return a value to its caller using a return statement. This statement terminates the execution of the program and returns control to the computer's operating system. By default, a 0 is returned to indicate normal completion of the program, whereas a nonzero return value indicates abnormal termination. Therefore, the second statement of the program indicates successful completion of the program.
if (x <= 0 )
return (0);
else
return(1);
All functions by default return int type data. If a function is required to return values other than integer data type, it can be forced to return a particular data type by using a type specified in the function header.
double product (number1, number2);
float cube_root(number);
When a value is returned, it is automatically cast to the function type. If a function does computations using double and is declared to return integer type. then, the return value will be truncated to an integer.
Note