Using Functions in C

Discussion in 'C' started by lionaneesh, Aug 9, 2011.

  1. In the previous tutorial we talked about functions, What are they, How to declare them etc. Now in this article we’ll be looking at

    In this tutorial we’ll be looking at a basic sample program, how it works and some basic rules/tips to keep in mind.

    Simple Program using Functions


    Function.c

    Code:
    #include<stdio.h>
    
    void function1()
    {
        printf("Hello i am a Void Function! :D\n");
    }
    
    int function2( int input )
    {
        printf("Hello I am a input/output function , I take int and input and give int as output\n");
        printf("Input Recieved : %d\n",input);
        return ( input + 1);
    }
    
    int main()
    {
            int output = 0;
            printf("Calling Function 1 , Type : Void !\n");
            function1();
            printf("------------------\n\n");
            printf("Calling Function 2 , Type : Int (Output) , Int (Input)!\n");
            output = function2(22);
            printf("Output : %d\n",output);
            printf("------------------\n");
    
    }
    
    Explanation:-


    In the above program we declared 2 functions, first functions is a void function with no return (output). And the second function is an int type function with int as input and int as output. After that we declare a variable named output , This variable is to store the output received from function 2.

    We provide 22 as input to function 2 and store the output in the variable named output.

    The program calls 2 functions separately and Gives us the output :-

    Code:
    Calling Function 1 , Type : Void !
    Hello i am a Void Function! :D
    ------------------
    
    Calling Function 2 , Type : Int (Output) , Int (Input)!
    Hello I am a input/output function , I take int and input and give int as output
    
    Input Recieved : 22
    Output : 23
    ------------------
    
    Return Statement (In functions):-


    The return statement specifies the output to be returned to the caller , Eg : In our case we are calling the function from the main() function so main() is the caller.

    Some Tips/Facts Regarding Functions



    1. A function can be called multiple number of times.
    2. The Variable declared or initialized in a function will not be applicable for other functions unless the variable is globally declared.
    3. Any function can be called from any other function.
    4. The Function can also call itself , These type of functions are called recursive functions
    5. After each function has done its job the control returns to main thereafter if the main has done its job the program execution stops.
    6. If a function is not called it does not execute.
     
  2. gpk kishore

    gpk kishore New Member

    Joined:
    Jun 30, 2011
    Messages:
    82
    Likes Received:
    0
    Trophy Points:
    0
    good one
    sir
    i want tracing for towers for hanoi program(recursive function)

    Also how recursive programs would be traced by the compiler
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice