Why Use Functions in C Programming

Why compose separate functions by any means? Why not press the whole logic into one function, main( )? because there are two reasons:

  • Writing functions abstains from reworking a similar code again and again. Assume you have a segment of code in your program that figures a region of a triangle. In the event that later in the program you need to figure the region of an alternate triangle, you won’t care for it on the off chance that you are required to compose similar directions once more. Rather, you would like to hop to a ‘segment of code’ that ascertains zone and after that hop back to the place from the last known point of interest. This area of code is only a function.
  • Using functions it winds up less demanding to compose projects and monitor what they are doing. On the off chance that the task of a program can be isolated into discrete exercises, and every movement put in an alternate function, at that point each could be composed and checked pretty much autonomously. Isolating the code into particular functions likewise makes the program less demanding to outline and get it to understand.

Try not to endeavor to pack the whole logic in one function. It is an awful style of programming. Rather, break a program into little units and compose functions for every one of these confined subdivisions. Try not to dither to compose functions that are called just once. What is critical is that these functions play out some intelligently secluded assignment task.

Passing Values between Functions in C Programming

The functions that we have utilized so far haven’t been extremely adaptable. We call them and they do what they are intended to do. Like our mechanic who dependably benefits the motorbike in the very same way, we haven’t possessed the function to impact them functions in the manner in which they do their undertakings tasks. It is pleasant to have somewhat more authority or control over what functions do, similarly, it is decent to have them function to tell the mechanic and additionally change the motor oil. Now we need to convey between the ‘calling’ and the ‘called’ functions.

The component used to pass on data to the function is the ‘argument’. You have unwittingly utilized the arguments in the printf( ) and scanf( ) functions; the organization string and the rundown of variables utilized inside the enclosures in these functions are parentheses. The argument is some of the time likewise called ‘parameters‘.

Think about the accompanying project. In this program, in main( ) we get the estimations of a, b and c through the console and after that output the aggregate of a, b and c. Be that as it may, the figuring of the sum is done in an alternate function called calsum( ). In the event that total is to be ascertained in calsum( ) and estimations of a, b and c are gotten in main( ), then we should pass on these qualities to calsum( ), and once calsum( ) figures the sum we should return it from calsum( ) back to main( ).

main( )
int a, b, c, sum ;
printf ("\nEnter any three numbers ");
scanf ("%d %d %d", &a, &b, &c);
sum = calsum( a, b, c);
printf ("\nSum = %d", sum);
calsum (x, y, z)
int x, y, z;
int d ;
d = x + y + z;
return (d);

               Here is the output...
         Enter any three numbers 10 10 30
                   Sum = 50

Post Author: Base Read

Leave a Reply

Your email address will not be published. Required fields are marked *