Saturday, December 24, 2011

FUNCTIONS

Introduction

Each functions have their own nature of work like the task of pow(x,y) function is to take two parameters or arguments (x,y) and return a value that is x to the power y, pow() is defined under  math.h header file. Function of sqrt(x) is to take a value and return its square root. The function name should be unique.
main() itself is a function, the program execution starts from main(). We can write it as void main(), int main() etc.
The keyword void means it will not return a value and the keyword return is used to pass a value from function body to the main() or any other calling program.
printf() and scanf() are standard library functions. Here we will be creating user defined functions.

Usually structure of functions looks like this :
  • no return type   function_name   ( no parameter )
  • no return type   function_name   ( with parameter)
  • return type        function_name   ( with parameter)
return type or parameters can be a int, float, char, array etc.

Syntax for Function is :

#include"stdio.h"

void function_name(void);   //  Function Prototype

main()
{
    statement 1;
    statement 2;

    function_name();  //  Call of Function

    statement 3;      //  After the execution of the function next statement will be executed.
}

void function_name()   // Function Definition or Body, here the function is defined.
{
    statement 1;
    statement 2;
    statement 3;
}


Example :

#include"stdio.h"
void test(void);
main()
{
    printf("I am inside main function ! \n");
    test();
    printf("I am inside main function ! \n");;
    test();
    printf("I am inside main function ! \n");
    test();
    printf("DONE ! \n");
}

void test()
{
    printf("I am inside test function ! \n");
}

Another example :

#include"stdio.h"
void printline(void);
main()
{
    printf("1st line:\n");
    printline();
    printf("2nd line:\n");
    printline();
    printf("3rd line:\n");
    printline();
}

void printline()
{   
    int i;

    for( i=0 ; i<60 ; i++ )
        printf("_");

    printf("\n");
}

We can also send a single value or multiple values to a function as a parameter or argument.
For example :

#include"stdio.h"
void sum(int,int);
void main()
{
    int a,b;

    printf("Enter 2 Numbers : ");
    scanf("%d%d",&a,&b);

    sum(a,b);
}

void sum(int a,int b)
{   
    int i;
    i=a+b;
    printf("The sum of %d and %d (which came from the main()) is %d \n",a,b,i);
}

    The keyword return passes the result to the calling program.
    For example :

    #include"stdio.h"
    int sum(int,int);
    void main()
    {
        int a,b,c;

        printf("Enter 2 Numbers : ");
        scanf("%d%d",&a,&b);

        c=sum(a,b);

        printf("The sum of %d and %d is %d \n",a,b,c);
        printf("The sum of %d and %d is %d \n",a,b,sum(a,b));
    }

    int sum(int a,int b)
    {
        int i;
        i=a+b;
        return (i); // or we can write it as return(a+b);
    }

    Multiple Functions in a Program :

    #include"stdio.h"
    int sum(int,int);
    int sub(int,int);
    int mult(int,int);
    int div(int,int);
    void printline();
    void main()
    {
        int a,b;

        printf("Enter 2 Numbers:");
        scanf("%d%d",&a,&b);

            printline();
            printf("ADD = %d\n",sum(a,b));
            printline();
            printf("SUB = %d\n",sub(a,b));
            printline();
            printf("MULT = %d\n",mult(a,b));
            printline();
            printf("DIV = %d\n",div(a,b));
            printline();
    }

    int sum(int a,int b)
    {
        return (a+b);
    }

    int sub(int a,int b)
    {
        return (a-b);
    }

    int mult(int a,int b)
    {
        return (a*b);
    }

    int div(int a,int b)
    {
        return (float(a/b));
    }

    void printline()
    {
        int i;
        for( i=0 ; i<60 ; i++ )
            printf("_");
        printf("\n");
    }

    Demo of getchar, getche, getch Functions :

    #include"stdio.h"
    #include"conio.h"  //  To use this functions we have to include conio.h file
    void main()
    {
        char ch;

        printf("Enter a Letter : ");

        ch=getchar();

        printf("\nCh = %c \n",ch);

        ch=getche();

        printf("\nCh= %c \n",ch);

        ch=getch();

        printf("\nCh= %c \n\n",ch);
    }

    Recursion

    A function calling itself is called Recursion.

    Call By Value

    Till now we had been passing the value of a variable to a function, which can be said as call by value.
    For example :
    sum ( a, b ) ;  //  Values of a and b are passed to the function sum().

    Call By Reference

    Each variable are stored in memory and holds a value. We can also pass the address of that memory location to a function which can be said as call by reference.
    For example :
    sum ( address of a, address of b ) ; /* Address of a and b are passed to the function sum().*/

    To know more about this we have to understand Pointer.


    3 comments:

    1. In short you are the best !
      i need your help in making a program with C. Can you pls give me some time for that.

      ReplyDelete
    2. Thanks for the feedback. Send me the details.

      ReplyDelete
    3. This comment has been removed by the author.

      ReplyDelete

    Followers