#include <iostream> using namespace std; fn1(int Arg) { int a = 10; } int main() { cout << "fn1(): " << fn1(22) << endl; return 0; } 

I call the function and pass the number 22 for example, which is one argument passed, but the function that takes one argument must convert it to 10 , and for some reason it outputs 1 , although it must print 10 , Why is that?

  • 2
    Your function does n't return anything at all :) How did it compile at all for you? ... See ideone.com/xUqSTH - Harry
  • @Harry in sjah implicit int assumed. It would be necessary to clarify where the TS compiled and how. - 伪位蔚蠂慰位蠀蟿
  • 2
    @alexolut assumed. But in the cout << not supposed, is not it? ... - Harry
  • one
    @Harry I do not argue that the code is positive, but I admit that somewhere there are options that allow compatibility with the old style. It seems that in C ++ Builder there was something like that. - 伪位蔚蠂慰位蠀蟿
  • It is quite possible that the example was written on the board as a draft with the amendment that part of the code was deliberately omitted. And as homework - you need to search and do everything according to the condition. - KoVadim

1 answer 1

You have incorrectly recorded a function, you must:

 int fn1(int Arg){ return 10; } 

Since the function does not do any actions with arguments, it is better to return the value immediately, and you didn鈥檛 have return . And you need a type that will return a function - int fn1 .

  • PS Thanks for editing. Although it was said above that int is implicitly implied, but we were so taught that it is better to specify all types explicitly, up to iterators. - Ackbar
  • one
    Apparently you have a teacher of the old school (C ++ 98). I already forgot when the last time I declared an iterator is not via auto. - evilnw
  • On the contrary, a young teacher, but for learning purposes I still would like to first write the types explicitly, we also wrote our containers on pointers) The most difficult thing was to write a binary tree again on pointers, no one mastered it in a given time. But then, of course, everything was taken from std. - Ackbar
  • @evilnw need no iterators through range-for writing! - 伪位蔚蠂慰位蠀蟿