Well, you can pass values to functions by parameters.
So a function...
int sum();
means...
return type : Integer
function name : sum;
parameters it takes : none i.e. void.
Let me define a statement here, in C you don't need to mention void explicitly if there's nothing, but in C++ you need to. So you get the thing more comfortable in C++
so int sum() actually means int sum(void)
So lets say a program..
Code:
int a = 10; b = 20;
int sum();
int main()
{
int c = sum();
}
int sum()
{
return a + b;
}
Adds two values in the variables a and b.
But this program is static. Isn't it?
Because whenever you'll run the program you'll get 30 as answer.
If you want to add other values you need change the source code. Also the variables are global. You don't use global variables for this type of things. This is not recommended.
(For now forget scanf() to get the user input)
Now comes function parameters. You can pass the values to the function as parameters upon which it should perform it's task.
This makes the function re-usable. In the above program the function will only add a and b even if you manage to take input using scanf().
What if you want a sum function which can add any values(variables) that you wish to.
So think yourself, what should you NEED NOT DO??
Answer is simple, you don't add that a and b always, instead do something like the function should add the values which you'd like at realtime, dynamically...
So here comes function parameters... Let's take this as example...
Code:
void sum(int, int);
int main()
{
int a = 10; b = 20;
int c = sum(a, b);
int m = 50; n = 100;
int o = sum(m, n);
int i = 200; j = 300;
int k = sum(i, j);
}
int sum(int x, int y)
{
return x + y;
}
So the line void sum(int, int); says, it's a function declaration of function named sum, whose return type is int and which takes two parameters of type int.
Code:
int sum(int x, int y)
{
return x + y;
}
is the function definition, which means, whatever values you pass in the function, it'll store them in x and y and add them and return them the addition result.
So in first line int c = sum(a, b); it passes 10 to x and 20 to y
In 2nd line int o = sum(m, n); it passes 50 to x and 100 to y
In 3rd line int k = sum(i, j); it passes 200 to x and 300 to y.
So see, the function became resuable...
But in first program...if we call the sum function 100 of times, all the times the result will be 30.
One more difference is there...which seems logical...see you are re-using the function and are not writing same code 100's of times for doing same thing...
Now lets compare the first program with this... What if you want to do the same in first function...??
Code:
int a = 10; b = 20;
int m = 50; n = 100;
int i = 200; j = 300;
void sumab();
void summn();
void sumij();
int main()
{
int c = sumab();
int o = summn();
int k = sumij();
}
int sumab()
{
return a + b;
}
int summn()
{
return m + n;
}
int sumij()
{
return i + j;
}
See, if you are not using function parameters... you need to writing same code to add diff. variables.
So it makes the program more prone to bugs and anyone who'll look at it will think it's a bad program...even including you. Why you should write same code for doing same things?
It's for 3...what about if you are adding 100 variables?? You are going to write the function 100 time with diff name??
Of course not...right?
Hope that helps.