Functions in C Language

 Introduction of function

  • A function is  a block of code, which only runs when it is called.
  • It is used to perform certain actions & this actions are imp for running code :define the code once, and call it many times.
  • When we use function, program size will be short.
  • It is  a piece of code which take some input do some computation ,some processing on those inputs and gives u output.
  • Definition of function in any other function is not allowed.
  • Function consist of two parts : Declaration-function name, return type, parameter(if any)
                                                          : Definition-the body of the function(code)
      Example
    code
          #include<stdio.h>
          void myfun();       ----------------------------------> declaration
          void main()
         {
            myfun();
            myfun();             ---------------------------------->function calling
            myfun();
             return 0;
          }
        void myfun()
         {
              printf("I am an Indian");   ---------------------->function definition
          }
      

   output
      I am an Indian
      I am an Indian
      I am an Indian

  

Types of Function

1.with return type, with arguments.: int fun(int x, int y) { }

Example

#include<stdio.h>

int sum(int x, int y);

int main()

{

int a, b, c;

printf("enter value for a , b ");

scanf("%d %d",&a , &b);

c=a+b;

printf("sum is: %d ",c);

}

int sum(int x, int y)

{

int z=x+y;

return z;

}

output: sum is : 10..............................................(if m entering value for a=5 & for b=5)


2.with return type, without arguments.: int fun() { }

Example

#include<stdio.h>

int sum();

int main()

{

int a, b, c;

printf("enter value for a , b ");

scanf("%d %d",&a , &b);

c=a+b;

printf("sum is: %d ",c);

}

int sum()

{

int z=x+y;

return z;

}

output: sum is : 10..............................................(if m entering value for a=5 & for b=5)


3.without return type, with arguments.: void fun(int x, int y) { }

Example

#include<stdio.h>

void sum(int x, int y);

int main()

{

int a, b, c;

printf("enter value for a , b ");

scanf("%d %d",&a , &b);

c=a+b;

printf("sum is: %d ",c);

}

void sum(int x, int y)

{

int z=x+y;

return z;

}

output: sum is : 10..............................................(if m entering value for a=5 & for b=5)


4.without return type, without arguments.: void fun() { }

Example

#include<stdio.h>

void sum();

int main()

{

int a, b, c;

printf("enter value for a , b ");

scanf("%d %d",&a , &b);

c=a+b;

printf("sum is: %d ",c);

}

void sum(int x, int y)

{

int z=x+y;

return z;

}

output: sum is : 10..............................................(if m entering value for a=5 & for b=5)






No comments:

Post a Comment