Wednesday, March 2, 2016

Concepts Associated with Functions in C

A function is not executed until it is called (invoked) from the program. By function call we mean, the program provides the function with input data in the form of arguments. The value of the argument is passed to the function and copied in the formal arguments. If the formal arguments are modified inside the function it will not affect the value of the actual arguments in the program unless the arguments are passed as pointers.
  1. #include<stdio.h>
  2. void function1(int a);
  3.  
  4. void main()
  5. {
  6. int i = 3;
  7. function1(i);
  8. printf("Value of i : %d", i);
  9. }
  10.  
  11. void function1(int a)
  12. {
  13. a++;
  14. printf("Value of a:%d\n", a);
  15. }
In the above example, value of variable 'a' will be printed as 4 but the value of 'i' will be printed as 3. Because, even if value of the argument is incremented inside the function, it would not affect the value of variable 'i' in 'main'. When the function is called, the program execution goes to the function and after executing the statements in the function, it returns to the same position.

Function parameters

The function parameter can be of any data type, charintfloatstring or even an array/user defined data type. The arguments passed from the caller (from where the function is called), must be of the same data type as of function parameters. If there is a mismatch between those two, it will generate a compiler error. Even sometimes the compiler may fail to spot the difference, in which case your program may act erroneously.

Variable Argument Functions in C

C function can also have variable number of arguments. For example functions like 'printf'and 'scanf' can take any number of arguments. The variable argument function has a function prototype like the one given below
data_type function_name(int, data_type argument1, …);
The three dots (…) in function prototype defines that it is variable argument. Note that the first argument is an integer which represents the number of arguments. Some utility methods are required to make use of variable arguments in C and it is defined inside header file 'stdarg.h'.

How to Use Variable Argument Function in C

First step in making use of variable arguments in the function definition is to use the macro,'va_start', which is defined in header file 'stdarg.h'. This macro initializes a variable of type'va_list' with the argument list. Then we need to use 'va_arg' macro and 'va_list'variable to access each individual parameters. When the processing is finished, use the macro'va_end' to release the memory used by 'va_list'.

Variable Argument Functions Example

  1. #include <stdio.h>
  2. #include <stdarg.h>
  3.  
  4. int calculateSum(int count,...)
  5. {
  6. va_list valist;
  7. int sum = 0.0;
  8. int i;
  9. va_start(valist, count);
  10. for (i = 0; i < count; i++)
  11. {
  12. sum += va_arg(valist, int);
  13. }
  14. va_end(valist);
  15. return sum;
  16. }
  17.  
  18. int main()
  19. {
  20. printf("Sum of 1, 2, 3 = %d\n", calculateSum(3, 1, 2, 3));
  21. printf("Sum of 1, 2, 3, 4 = %d\n", calculateSum(4, 1, 2, 3, 4));
  22. }

No comments: