WHAT IS FUNCTION

In C programming, a function is a self-contained block of code that performs a specific task. Functions provide a way to organize code into modular and reusable components, making it easier to manage and understand large programs. Here's the basic syntax of a function in C:

return_type function_name(parameters) {

    // function body

    // ...

    return value; // optional return statement

}

Ex function for calculating sum of 2 value.

#include<stdio.h>

void show()

{

  printf(“hello I am rahul”);

}

int main()

{

  show();

  return 0;

}

Output: hello I am Rahul

when we create a new function we use that function name 3 times in the code.

1.Prototyping: prototyping refers the code that we are going to use show() function whose return type is void.

2.Declaration: this refer to declaration of body of new function.

3.Calling: this refer to the point where we are calling this new declared function.

In this example flow of code is from main() method.

The it calls show method at the point main() method get sleep and code inside show() method start executing.

After compiling the logic from show() method it come back to main() method and perform remain task.

#include <stdio.h>

// Function declaration

int sum(int a, int b);

int main() {

    // Variables

    int x = 5, y = 3, result;

 

    // Function call

    result = sum(x, y);

 

    // Display the result

    printf("Sum: %d\n", result);

 

    // End of program

    return 0;

}

// Function definition

int sum(int a, int b) {

    // Variables

    int result;

    // Calculate the sum

    result = a + b;

    // Return the result

    return result;

}

+---------------------+

|  Start Program      |

+---------------------+

|

v

+-----------------+

|     main()      |

+-----------------+

|

v

+-----------------+

|  Initialize x, y|

+-----------------+

|

v

+-----------------+

|  Function call  |

|    sum(x, y)    |

+-----------------+

|

v

+-----------------+

|      sum()      |

+-----------------+

|

v

+-----------------+

|  Initialize a, b|

|      a=5, b=3   |

+-----------------+

|

v

+-----------------+

|   Calculate     |

|   result = a+b  |

+-----------------+

|

v

+-----------------+

|   Return result |

+-----------------+

|

v

+-----------------+

|     main()      |

+-----------------+

|

v

+-----------------+

|   Display result|

+-----------------+

|

v

+-----------------+

|  End of program |

+-----------------+

Write a program to call every function by one function

#include<stdio.h>

void itlay()

{

  printf(“I am in itlay”);

}

void brazil(){

  printf(“I am in brazil ”);

  itlay();

}

void india(){

  printf(“I am in india”);

  brazil();

}

int main()

{

  india();

  return 0;

}

Output

I am in india

I am in brazil

I am in itlay

 

Summary of using a function

1.a function name is called when the function name is followed with semicolon.

        int main(){

        india();       

        }

2. a function is defined when function name is followed by curly brackets.

         void itlay(){

          printf(“I am in itlay”);

          }

3.any function can call from another function.

          void india(){

           brazil();

            }

4.a function  can called more then one times.

          void main(){

           india();

            india();

            india();

            }

5.the order in which function is defined and the order in which a function is called can not necessary to be the same.

#include<stdio.h>

void show1(){

statement

}

void show2(){

statement

}

int main(){

show2();

show1();

return 0;

}

6.a function can call itself any number of times.

7.a function can call another function but can not defined another function.

8. there are basically 2 types of functions.

          a. pre defined function / library function.

                printf

                scanf

          b.user define function

                india();

why we need to use functions

1.it makes code simple to read.

2.it separate the logic for specific task.

3.it stops code rewriting .

4.using function we can stop duplicacy in code .