A function is a self-contained block of program statements that performs a particular task. The concept of C programming is combination of functions and the program execution will begins from the special function called main(). The scanf(), printf() main() etc.. that have been used in programs so far are all functions. They are called library functions and C provides a lot of them.In addition to built in library functions, the programmers can write their own functions and use them.
When there are multiple functions in a program , each function is given different name and a list of parameters. When ever the function is to be called, we refer the function name with suitable list of arguments. The program control will then be transferred to the function and the statements in the function will be executed. After the called function is executed, the control returns to the calling function, the place it was called from.A function can call another function, and that another and so on.
The use of functions provides several benefits.
It makes programs significantly easier to understand and maintain by breaking them up into easily manageable chunks.
Code reuse/Code sharing is the another advantage, where the same code can be used where ever needed. This will avoid the need to have same code replicated at various points and hence make the program small and efficient. A well written function may be reused in multiple programs. C standard library is an example of functions being reused.
Flexible debugging is another advantage because functions make the code shorter and more readable.
Data protection is another feature of functions. The local data described within the functions are not available outside and are used only when they are executed.
Functions form an important part of top-down design and structured programming. Top down design is the act of breaking down a task into manageable parts such that each individual part is relatively simple and the parts can be combined without much difficulty to achieve the original task. Structured programming makes the code more elegant and readable.
The following 3 components are associated with the use of functions
1.The declaration statement ( function prototype declaration) that identifies a function with a name, list of arguments and the type of data returned.
return_data_type function_name(data_type var1,data_type var2,…….);
Eg: int max(int a,int b); // this function max will return the maximum of 2 integers a and b.a and b are called the arguments of the function.
float average(int n,float a); // function average to find average of elements of the array a of size n
Note: if the function does not return any value void has to be mentioned. The default return value of a function is int. Function declaration is needed if the called function is written later. The function name should not match with built in function names.
2.The function definition that consists of a function header identifying the function, followed by the body of the function, which contain the statements to be executed.
int max(int a, int b)
If ( a>b)
Note: The job of return statement is to hand over some value from the function to the point where the call was made.
3.The calling statement that invokes the function by either passing or not passing some parameters.
The name, number of arguments and type must match with the function definition.
Arguments are passed by value in C function calls. This means that the local copies of the values of the arguments are passed.
When an entire array is an argument of a function, only the address of the array is passed and not the copy of the complete array. Therefore, when the function is called with the name of the array as argument, address to the first element is handed over to the function. This implies that during its execution, the function has the ability to modify the contents of the array that is specified as function argument. This is an exception to the rule of passing the function arguments by value.
Sample program to find power x^k using a function
int power(int x, int k)
printf(“Enter x and k \n”);
printf(“%d power %d= %d”,x,k,p);
Note: The variables used in calling the function are called actual arguments and the variables used in function header are called formal arguments.
Try the following programs using functions
1.Finding biggest of 3 numbers.
2.Converting Fahrenheit to Celsius.
3.Finding square of a number.
4.Average of 3 numbers.
5.Çheck whether a given year is leap year or not.
6.Write a function big() to find the biggest of two numbers and use it to find biggest of three numbers.
7.Sum of the digits of a number.
8.Sort array of integers
9.Add a string to the end of another string with out using library function.
10.copy one string to another with out using library function.
11.Write a function to reverse a string and use it to check whether the given string is palindrome or not.
12.Modify a char in a particular position of a string. Use position and char as argument.
13.Find maximum value in an array
14.Average age of students in a class.
15.Write matrix addition of 3 matrices A,B and C. write functions for reading and printing matrices.
16.Write functions to convert feet to inches and inches to centimeters. Write a program which will read feet and convert it into centimeters using the two functions.
17.Factorial of a given number.
18.Use the factorial function to compute nCr.
19.Use the factorial and power function to compute the series 1-x^2/2!+x^4/4!-x^6/6!...n.