Google Add

Search

Write a C program which explain the concept of function and it's use


What is function

A function is section of program that performs some specific task. Let's take a example int add (int x, int y){ return x+y; } I declared a function add which takes two arguments x and y. It returns the 
addition of x and y.


What's the use of function

Suppose you are writing a program in which you want to perform the same task ten times. Then you have two options

a. Either you write the same piece of code again and again.


b. Second is Make a function and call the function whenever you perform the 
action.



The second advantage of function is if you want to do some changes, you need to do at one place only and it will reflect in all other places.





// Example of function

#include <stdio.h>
int add(int x, int y);  // Declare add function with two arguments 

int main()
{

int a,b,c;

printf("Enter two numbers");
scanf("%d%d",&a,&b);

c = add(a,b);

printf("The addition of two numbers is%d",c);

return 0;
}

int add(int x, int y)
{

return x+y;    // Returns the addition of two numbers

}


No comments:

Post a Comment