Introduction Functions in C

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

  1. What is a Function?

    A Function is a statement or a rule which relates a variable quantity with other variable quantities. The function can often be related to a machine , it has an input and a output , The output is somehow dependent on the input. For more clarity lets take an example of a mathematical function.

    eg : f(x) = x + 2

    Now if we give 2 as input to the above function then the function returns a output of 4 . This is a classic example of how the output was dependent on the input.

    In terms of programming , A function is a special block of code that has its name, property and its use. It can be executed from any point in a program and any number of times. The calls like printf() , scanf() etc etc. Which we used in our previous tutorials are all calls to specific functions which C gives us in its immense standard library.

    Now you may be thinking that why do we need functions, basically functions are used when a similar piece of code is to be executed repeatedly , to avoid tedious task of writing similar code again and again we can declare functions and simply call them for easier access. Another reason for using functions is that using functions can greatly make the code more readable , You can construct different functions for each logical part of your program.

    In a C Program a function is declared out of the main() function. And follows the following basic syntax:-

    return_type nameOfTheFunction(argument_type arg1 ,argument_type arg3 , ... , argument_type arg4 )

    The return-type is the type of output the function will be returning, like char , int , float etc. The function that doesn’t return a value is known as a void function. Thus , in these functions the return-type have to setup to void.

    argument_type is the type of argument , like int , float etc. It’s the same as if you are declaring a variable.

    The arg1 , arg2 etc. are basically the names given to the arguments , These names may not be the same as the names of variables given as input.
    The ‘…’ means that there can be n number of arguments to the function, The different arguments to the function must be separated by a comma (,).

    Constructing functions

    Now that we know quite a bit about functions lets construct some function prototypes.

    1. Construct a function named ‘testFunction’ that takes 2 integer inputs and returns a char
    char testFunction(int first , int second)
    2. Construct a function named ‘testFunction2’ that takes 1 char , 2 integers and 1 float and returns a float.
    float testFunction2(char first , int second , int third , float fourth)
    That’s all for this tutorial stay tuned for more.
  2. gpk kishore

    gpk kishore New Member

    Jun 30, 2011
    Likes Received:
    Trophy Points:
    thanks sir
    I want information regarding recursive functions
    and tracing of it in normal compilers
    please help me out

    example:-Towers of hanoi

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