This blog is under construction

Saturday, 12 May 2012

Void function in C

What is void function?
If a function returns nothing to the calling function, then it is called void function.  But still, we can indirectly return values to the calling function using global variables and pointers.  The return type of the function would be void instead of any other data type.

Void function example in C:
void func(int a, int b);

Here, void is the return type which means the function func() won't return anything.

Example C program to illustrate void function:
 
  #include <stdio.h>
  void update();
  int num1, num2;
  int main() {
        num1 = 100;
        num2 = 200;
        printf("Before update() function call:\n");
        printf("Value of num1:%d\n", num1);
        printf("Value of num2:%d\n\n", num2);
        update();
        printf("After update() function call:\n");
        printf("Value of num1:%d\n", num1);
        printf("Value of num2:%d\n", num2);
        return 0;
  }

  void update() {
        num1 = num1 + 100;
        num2 = num2 + 100;
  }

  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Before update() function call:
  Value of num1:100
  Value of num2:200

  After update() function call:
  Value of num1:200
  Value of num2:300



No comments:

Post a Comment