0
What is functions with suitable example??
2 odpowiedzi
0
When you write any code in main method, normally it gets executed only once. But you can reuse your code when you create a function. You just have to call the function.Suppose you have to add numbers.
Example: 10+10 ,20+20,30+30
Normal code:
#include <iostream>
using namespace std;
int main() {
int c;
c=10+10;
cout<<c<<endl;
c=20+20;
cout<<c<<endl;
c=30+30;
cout<<c<<endl;
return 0;
}
Code with Function:
#include <iostream>
using namespace std;
void add(int a,int b) //function
{
int c=a+b;
cout<<c<<endl;
}
int main() {
add(10,10); //call
add(20,20); //call
add(30,30); //call
return 0;
}
0
Function is anything that performs a task or does something.
In c++ a function is defined as
return type(parameters)
{
statements
return ;
}
for example here is a function that add two numbers.
int sum(int i,int j)
{
return i+j;
}
Function can also perform a task and return no value , we use void in that case.
Here is a link if you want to know more.
http://www.cplusplus.com/doc/tutorial/functions/