Spotting mistakes in functions?

Mar 23, 2019 at 4:50pm
I’m able to see when a bracket or something is misplaced but sometimes I’m unable to spot the bright eyed mistakes. So for the following .. is it valid or is there an error and if so why?

 
  double square(double x) return x* x;

I can’t seem to spot a problem with this one but I’m unfamiliar with doubles.


1
2
3
4
5
  int f() {
      string s;
      // ...
      return s;
  }

I believe this one is correct because it returns the correct string but I’m still not too sure.



 
  f2(int i) { /* ... */ }


For this one I think it’s incorrect because f2 has no return type and that int needs to be placed before that.
Mar 23, 2019 at 4:59pm
Start with making sure it compiles.
1
2
3
4
5
6
7
8
9
10
11
12
13
$ g++ -c bar.cpp
bar.cpp: In function ‘double square(double)’:
bar.cpp:4:25: error: named return values are no longer supported
 double square(double x) return x* x;
                         ^
bar.cpp:9:12: error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘double’ in return
     return s;
            ^
bar.cpp: At global scope:
bar.cpp:12:9: error: ISO C++ forbids declaration of ‘f2’ with no type [-fpermissive]
 f2(int i) { /* ... */ }
         ^
$ 

Mar 23, 2019 at 4:59pm
Okay. Thank you.
Topic archived. No new replies allowed.