C Programming + Tutorials in C for Beginners

C Functions

Home  »  C Programming  »  C Functions


     Subprogram is the independent and complete program. It is also called function program or function sub-program. In other words sub-programs are complete because, these are the programs, which have the global as well as local declaration statement, executable statement and function calling statement like main program. Subprograms in the c-language are called user-defined functions.

Contents


Advantages of a function sub-program (Why we need function)


     As a Function is a self contained module of some c statements having specific objective. But the declaration and use of function depends upon its need. So due to number of features in it, it becomes more important than the main program to solve some complex problems. Following are the some advantages of functions :

  1. Function makes the lengthly and complex program easy and in short forms. It means large program can be sub-divided into self-contained and convenient small modules having unique name.

  2. The length of source program can be reduced by using function by using it at different places in the program accroding to the user's requirement.

  3. By using function, memory space can be properly utilized. Also less memory is required to run program if funciton is used.

  4. A function can be used by many programs.

  5. Function increases the execution speed of the program and makes the programming simple.

  6. By using function, portability of the program is very easy.

  7. Debugging (removing error) becomes very eaiser and fast using the function sub-programming.

  8. It removes the redundancy i.e. avoids the repetition and saves the time and space.

  9. Functions are more flexible than library functions.

  10. Testing (verification and validation) is very easy by using functions.





Output is as :
I am a Good Boy

Return Statement

     Return statement is the last statement of a function. But in some cases, it can be used anywhere within the subprogram. When return statement execute, the control moves to the calling statement. Whether a fucntion send back any value to the calling function or not, but there must be the use of return statment with or without return value. The main purpose of the return function is to communicate the result of the operation of the called function to the place where the call is made. The general syntax and the procedure used for the return statement is :

return;
or

return (expression);
or

return (variable);






Categories of Functions

     Sometimes function program takes values from the main program and sometimes it does not. Similarly function sub-program sometimes-return values and sometimes it does not, return any value to the main program. we categorize the function subprogram in four sections:

  1. Function with no argument and no return value.
  2. Function with argument and no return value.
  3. Function with no argument and return value.
  4. Function with argument and return value.


Function with no argument and no return value.

     In this category of the function sub-program, main program will not send any argument to the function and also function subprogram will not send (return) any value to the main program. For example, the program procedure to display the processing of the function with no argument and no return value is as:





Output is as :
Enter the Two No. for Sum :
10
10
Sum= 20




Function with argument and no return value.

     In this category of the function sub-program, main program or the calling program will send argument value(s), but called program or the function subprogram will not return any value. For example, the program procedure to display the processing of the function with argument and no return value is as:





Output is as :
Enter the Two No. for Sum :
10
10
Sum= 20




Function with no argument and return value.

     In this category of the function sub-program, main program or the calling program will not send argument value(s), but called program or the function subprogram will return any value. For example, the program procedure to display the processing of the Function with no argument and return value is as:





Output is as :
Enter the Two No. for Sum :
10
10
Sum= 20




Function with argument and return value.

     In this category of the function sub-program, main program or the calling program will send argument value(s), but called program or the function subprogram will return any value. For example, the program procedure to display the processing of the Function with argument and return value is as:





Output is as :
Enter the Two No. for Sum :
10
10
Sum= 20




Recursion

     When a called function in turn calls another function, then a process of 'chaining' occurs. Recursion is a special case of this process or chain, So when a function calls itself, then it is called recursion. This chain continues till a specific condtion met. If it has not any stop condition, then it will create an indefinite loop. Recursion is also called self-reference loop.
     Recursion is used to solve for the problems, which can't be solved by the iterative procedure for while and do loops. It is useful to solve repetitive problems, where input of one sub-program can be used as starting value having the previous output.







Void Statement


     When a void statement is used in function program, then it returns nothing. In other words, when we want to return no value to the calling program, then void function or void statement is used. The general syntax is as:

void function name(); /*during function declaration*/
or
void function-name() /*during function in use*/



<< Previous Topic
Next Topic >>



Previous
Next Post »
Thanks for your comment