Functions in C

function is a group of statements that together perform a task. Every C program has at least one function, which is main().


LOGO


But Why do we use Function ?
There are several number of advantages of using a function in a program. Some of them  are:-

  • It breaks the code into smaller Functions and keeps the program organized
  • It makes the program easier to understand to the end_user.
  • It makes easy to detect any "error" if any present in the program i.e,reduces chances of error.
  • It avoid repetition of the code.
These above are the some of the advantages of the function in a program.

A function is divided into three parts:-
Function Declaration
Function Definition
Function Call

Flow Chart
Flow Chart


There are total four types of Syntax for a function.

  1. No return type with No argument

    Syntax: #include<stdio.h>
                   void fun( ); //here fun() is a function name
                   void main( )
                   {
                    ........................................
                    ........................................
                    ........................................
                    fun( ); //function call         
                    ........................................
                    }
                 
                   void fun( )
                   {
                    ........................................
                    ........................................
                   }
  2. No return type with argument         

    Syntax: #include<stdio.h>
                   void fun(datatype, datatype); //datatype can be int, float, char, etc
                   void main( )                                   
                   {
                    ........................................
                    ........................................
                    ........................................
                    fun( ); //function call
                    }
                 
                   void fun(datatype_variable, datatype_variable)
                   {
                    ........................................
                    ........................................
                   }
                  
  3. Return type with argument
    Syntax: #include<stdio.h>
                   int fun(datatype, datatype); //fun( ) is the name of function
                    int main( )
                   {
                    ........................................
                    ........................................
                    ........................................
                    fun( ); //function call
                    ........................................
                    return 0;
                    }
                 
                    int fun(datatype_variable, datatype_variable)
                   {
                    ........................................
                    ........................................
                    return 0;
                   }
  4. Return type with No argument
    Syntax:
    #include<stdio.h>
                 int fun( ); //here fun( ) is a function name
                 int main( )
                 {
                 ........................................
                 ........................................
                 ........................................
                  fun( );
                  .......................................
                  return 0;
                 }

                 int fun( ) //here the datatype is int
                 {
                 ........................................
                 ........................................
                 return 0;
                 }
So, here are the syntax used in a function as per the requirement.

ACTUAL ARGUMENT :- 
Is somewhat giving values to the formal argument by writing the values.
for example- fun(5,2);


FORMAL ARGUMENT :-
Are the argument used to/for the declaration of the function.
for example- fun(int x, int y);


Function can be called by the value or by the reference and the most common way to call a function is by call by value.

# Call by Value

  • In this the value of actual argument is called by formal argument.
  • Whatever changes made in the function definition are not "reflected back" in the main( ).
    for example:-
    Example 1
Call by Reference

  • Address of actual argument is passed to formal argument.
  • Whatever changes made in the function definition are "reflected back" in the main().
    for example:-
    Example 2
    Example 2
    In call by reference Pointers were used. Pointer are those variables that can store another variable's address.

Things to Remember
  • main( ) function is compulsory for any C program.
  • Anything written before " ( ) " is the name of the function.
  • It doesn't matter whether you first write the different function and then write about the main( ) function, the compiler will first go to main( ) function.
  • A variable used in a function has the scope and lifetime up to that function only i.e, that variable can't be used as same as(value & behavior) in the original functions.


    To read our post on Recursion, please click on Rec( ).
    To read our post on Array (Part-1) please click on Arr1.

    For giving some suggestion please comment us or check our "Contact us" page. Also more update related to coding will be added in this Blog later on, so please get attached with this Blog. Thank you for your support and time...

Post a Comment

0 Comments