***Welcome to ashrafedu.blogspot.com * * * This website is maintained by ASHRAF***

Posts

    Showing posts with label function. Show all posts
    Showing posts with label function. Show all posts

    Tuesday, November 24, 2020

    Inline Function

    C language provides a mechanism called inline function. The property of inline function is, compiler inserts the entire body of the function in the place where inline function call is used in the program.

     Using inline functions passing of control between function caller and function callee is avoided.

     A normal function becomes inline function when function prototype of the function is prepended with keyword “inline”.

     Inline function mechanism increases execution performance.

     Disadvantage of inline function is, it increases file size as same function code is copied again and again in the program wherever it is called.

     #include <stdio.h>

      // Inline function in C

     inline void sum(int x, int y)

    {

        int z;

        z=x + y;

        printf(“sum is %d”,z);

    }

    void main()

    {

     int x=5,y=6;

    sum(x,y); 

    }

     

    Following are situations where inline function may not work

    1. if the function is recursive.

    2. if the function contain static variables.

    3. if the functions contains control structure statements.

    4. and the function main cannot work as inline.

    Wednesday, November 18, 2020

    Passing Array to Function in C

    In C, there are various general problems which requires passing more than one variable of the same type to a function.

    Declaring Function with array as a parameter

    There are two possible ways to do:

     

    1. Have an array as a parameter.

    int sum (int arr[ ]);

     

    1. Or, have a pointer to hold the base address of array.

    int sum (int* a);



    When a formal parameter is declared is declared in a function as an array, it is interpreted as a pointer variable not as an array.

    Passing arrays as parameter to function

     1. Passing a single array element to a function

     Declare and define an array in main( ) function and pass one of the array element to a function

     Ex:

    #include<stdio.h>

     void printArray(int a);

     int main( )

    {

        int myArray[] = { 2, 3, 4 };

        printArray(myArray[2]);        //Passing array element myArray[2] only.

        return 0;

    }

     void printArray(int a)

    {

        printf("%d", a);

    }

     2. Passing a complete One-dimensional array to a function

     Only send in the name of the array as argument, which is nothing but the address of the starting element of the array, or say the starting memory address.

    Example:

     #include<stdio.h>

     float findAverage(int marks[ ]);

     void main()

    {

        float avg;

        int marks[] = {99, 90, 96, 93, 95};

        avg = findAverage(marks);       // name of the array is passed as argument.

        printf("Average marks = %.1f", avg);

    }

    float findAverage(int marks[])

    {

        int i, sum = 0;

        float avg;

        for (i = 0; i <= 4; i++)

     {

            sum += marks[i];

        }

        avg = (sum / 5);

        return avg;

    }

     3. Passing a Multi-dimensional array to a function

     Only send in the name of the array as argument, which is nothing but the address of the starting element of the array.

    Example:

     #include<stdio.h>

     void displayArray(int arr[3][3]);

     int main()

    {

        int arr[3][3], i, j;

        printf("Please enter 9 numbers for the array: \n");

        for (i = 0; i < 3; ++i)

        {

            for (j = 0; j < 3; ++j)

            {   

                scanf("%d", &arr[i][j]);

            }

        }

        // passing the array as argument

        displayArray(arr);

        return 0;

    }

     void displayArray(int arr[3][3])

    {

        int i, j;

        printf("The complete array is: \n");

        for (i = 0; i < 3; ++i)

        {

            // getting cursor to new line

            printf("\n");

            for (j = 0; j < 3; ++j)

            {      

                // \t is used to provide tab space

                printf("%d\t", arr[i][j]);

            }

        }

    }

    Monday, November 16, 2020

    Functions - Introduction

    A function is a block of code that performs a specific task. In C, we can divide a large program into the basic building blocks known as function.

    A function can be called multiple times to provide reusability and modularity to the C program.

    Reusability is the main achievement of C functions. By using functions, we can avoid rewriting same logic/code again and again in a program.

    There are two types of functions in C programming:

    •      Library Functions: are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc.

    •      User-defined functions: are the functions which are created by the C programmer. It reduces the complexity of a big program and optimizes the code.

    A function will have three aspects

    1. Function Declaration / function prototype

    2. Function Call

    3. Function Definition.

    1. Function Declaration / function prototype:

    A function must be declared in a C program to tell the compiler about the function name, function parameters, and return type.

    A function prototype is simply the declaration of a function that specifies function's name, parameters and return type. It doesn't contain function body.

    Syntax of function prototype

    returnType      functionName(type1 argument1, type2 argument2, ...);

    Ex:

    void     sum(int   x, int   y);

    The function prototype is not needed if the user-defined function is defined before the main() function.

    2. Function call: 

    Function can be called from anywhere in the program. Control of the program is transferred to the user-defined function by calling it.

    Syntax of function call

    functionName(argument1, argument2, ...);

    Ex:

    sum(a,b);

    3. Function definition:

    Function definition contains the block of code to perform a specific task. When a function is called, the control of the program is transferred to the function definition. And, the compiler starts executing the codes inside the body of a function.

    Syntax of function definition

    returnType    functionName(type1 argument1, type2 argument2, ...)

    {

        //body of the function

    }

    Ex:

    void    sum(int x, int  y)

    {

    int  sum=x+y;

    printf(“ sum is;%d”. sum);

    }

    Different aspects of function calling

    There are four different aspects of function calls:

    •      function without arguments and without return value

    Ex: void sum( );

    •      function without arguments and with return value

    Ex: int sum( );

    •      function with arguments and without return value

    void   sum(int a,int b);

    •      function with arguments and with return value

    int   sum(int a,int  b);

    Top