Functions With No Arguments and No Return Value
Functions With No Arguments and No Return Value
A C function without any arguments means you cannot pass data (values like int, char etc) to the called function. Similarly, function with no return type does not pass back data to the calling function. It is one of the simplest types of function in C. This type of function which does not return any value cannot be used in an expression it can be used only as independent statement. Lets have an example to illustrate this. 1. #include<stdio.h> 2. #include<conio.h> 3. void printline() 4. { 5. int i; 6. printf("\n"); 7. for(i=0;i<30;i++) 8. { 9. printf("-"); 10. } 11. printf("\n"); 12. } 13. void main() 14. { 15. clrscr(); 16. printf("Welcome to function in C"); 17. printline(); 18. printf("Function easy to learn."); 19. printline(); 20. getch(); 21. }
Output
Output
Output of above program. Sum of 30 and 15 is 45. Sum of 63 and 49 is 112. Sum of 952 and 321 is 1273.
10. { 11. int z; 12. clrscr(); 13. z = add(952,321); 14. printf("Result %d.\n\n",add(30,55)); 15. printf("Result %d.\n\n",z); 16. getch(); 17. }
Logic of the function with arguments and return value. Output of the above program. Result 85. Result 1273.
our UDF add() returns an integer value on calling. To store that value we have declared an integer value. We have passed 952, 321 to the add() function, which finally return 1273 as result. This value will be stored in z integer variable. Now we can use z to print its value or to other function. You will also notice some strange statement in line no. 14. Actually line no. 14 and 15 does the same job, in line no. 15 we have used an extra variable whereas on line no. 14 we directly printed the value without using any extra variable. This was simply to show you how we can use function in different ways.
2. #include<conio.h> 3. void calc(int x, int y, int *add, int *sub) 4. { 5. *add = x+y; 6. *sub = x-y; 7. } 8. void main() 9. { 10. int a=20, b=11, p,q; 11. clrscr(); 12. calc(a,b,&p,&q); 13. printf("Sum = %d, Sub = %d",p,q); 14. getch(); 15. } Output of the above program. Sum =31 sub=9