Function In C

A function is a group of statements that together perform a specific task. Function is a block or part of program. A function definition specgifies the name of the function, the types and number of parameters, it expects to receive, and its return type, when any program is very long or same code is repeating many times then we try to cut the program in different parts(blocks) so that whole program become more understandable, easier to debug and size of code will be shorter. A repeated group of instruction in a program can be organized as a function. Every C program has at least one function, which is known as main() function.

Why to use function?

Functions are used for driving a large code into module, due to this we can easily debug andd maintain code. For example if we write a calculator programs at that time we can write every logic in a separate function(for example sum(), for subtraction sub()). Any function can be called many times.

Advantages of Function

  • Code Re-usability
  • Develop an application in module format
  • Easily debug the program.
  • Code optimization: No need to write lot code.
  • Program testing becomes easy.
  • Compiled executable is smaller.
  • You don't have to make comments because function name make sense.
  •  Program can be developed in short period of time.
  • There is no chance of code duplication.
Defining a function

Defining of function is nothing but give body of function that means write logic inside function body.

Syntax
     
          return_type_function(parameter)
           {
              function body;
             }
 
 Return type : A function may return a value. The return type is the data type of the value the function returns. Return type parameters and return statement are optional.


Function name : Function name is the name of function. It is decided by programmer or you.

 Parameters :  This is a value which is pass in function at the time of calling of function. A parameter is like a placeholder. It is optional.

Function body : Function body is the collection of statements.

Function Declarations 

A function declaration is the process that tells the computer about a function name. The actual body of the function can be defined separately.

Syntax  : 
               return_type function_name();

Calling a function
When we call any function, control goes to function body and execute entire code. For call of any function, just write name of function and if any parameter is required then pass parameter.

 Syntax:

     function_name();   or
      Varriable=function_name(argurment);

Post a Comment

2 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

If you have any doubt, let me know...