difference between functions and normal statements

could some one exlain me the difference between functions and normal declarations, cause the outcome comes same everytime i try it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// this is with functions
#include <iostream>

using namespace std;

int add(int a,int b){
int f;
f = a + b;
return f;
}

int main(){
int g, h, j, i;
cout<<"enter first number: ";
cin>>g;
cout<<"enter second number:";
cin>>h;

j = add(g,h);
cout<<"sum is:"<<j;
cout<<"\n\n enter another number:";
cin>>g;
cout<<"enter seond number please:";
cin>>h;
i = add(g,h);
cout<<" sum is"<<i<<j;

}

now without funcitons
1
2
3
4
j = g+h;

i =g+h;

and why do we even have this
1
2
int f;
f = a + b;

we are not using f anywhere, or is it possible if we could do
cout<<"answer is:"<<f;

thank you.
Last edited on
The code you provided is just an example of a function, it is not actually useful in real code.

Functions are useful for writing complex code only once and then calling the function multiple times in other places. Then if a change is required, you only have to change the code in one place instead of many.
Also functions can be named to make code more understandable:
1
2
3
4
5
6
7
8
9
10
11
12
13
std::string remove_digits(std::string s)
{
    s.erase(std::remove(s.begin(), s.end(), ::isdigit), s.end());
    return s;
}
//...
std::string clean = remove_digits(dirty);

//VS

std::string clean = dirty;
//WTF is this doing?
clean.erase(std::remove(clean .begin(), clean .end(), ::isdigit), clean .end());


Example of function which do something useful (something I help somebody with):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cctype>
#include <algorithm>

//Moderate function. Gets called 6 times. more than 20 lines of economy
//Lateg got bug fixed. I am glad that I did not had to change it 6 times
//transforms string to ordered set of characters for easy compare
std::string normalize(std::string s)
{
    s.erase(std::remove_if(s.begin(), s.end(), [](char c)
                                                  { return !isalpha(c);}),
            s.end());
    std::transform(s.begin(), s.end(), s.begin(), ::tolower);
    std::sort(s.begin(), s.end());
    return s;
}

//Sensible name
bool are_anagrams(const std::string& lhs, const std::string& rhs)
{
    return normalize(lhs) == normalize(rhs);
}


int main()
{
    std::cout.setf(std::ios::boolalpha);
    //Easy to read and understand code intent
    std::cout << are_anagrams("Forum", "Mruof") << '\n' <<
                 are_anagrams("C plus PLUS ++", "LUSpclusP") << '\n' <<
                 are_anagrams("then", "than") << '\n';
}
and why do we even have this
1
2
3
int f;
f = a + b;
return f;


That is also a contrived example of something useful. When a function returns something complicated, I often store it in a local variable named result before returning it. This is purely to make debugging easier. By storing it somewhere, I can put a breakpoint at that location and see the value that is going to be returned.

In addition to avoiding duplicate code, functions are helpful when designing and coding a complex program. You can break the problem down into smaller pieces, code each as a function, test the function, and move on to the next piece. In real-world settings, the different pieces might be coded by different people, or even entirely different teams.
thank you all, for explaining it to me.
Topic archived. No new replies allowed.