0

I am reading a chapter about functions in a beginner's book for C++ and have two questions.

1) Should a function be declared at the beginning of the code and defined at the end or should the function be completely defined at the beginning?

#include <iostream>
void myfunction(); // function declaration
int main(){}
// function definition
void myfunction() {std::cout << "Hello World from a function.";}

or

#include <iostream>
void myfunction() {std::cout << "Hello World from a function.";} 
int main(){}

Intuitively, i would have used the last variant because its more compact. Is there a good reason why one should use the first (longer) variant?

2) When should an argument of a function be used as value or reference? I do not see what advantage there should be in using the argument as reference.

#include <iostream>
void myfunction(int& byreference) 
{
    byreference++; // we can modify the value of the argument
    std::cout << "Argument passed by reference: " << byreference;
}
int main()
{
    int x = 123;
    myfunction(x);
}

If i would have used myfunction(int byreference) instead, i would get the exact same result. So if i pass the argument as a value, i can still modify it.

  • 2
    The answer to your first question is, annoyingly enough, "that depends". Do what you find works best for you. You're going to change your mind many times over time. – molbdnilo Aug 31 '20 at 15:59
  • 2
    Regarding your second question, print the value of `x` in `main`, with both variants. – molbdnilo Aug 31 '20 at 16:00
  • 1
    ^^^^^ before *and* after the function call. – WhozCraig Aug 31 '20 at 16:01
  • 3
    I would suggest splitting this into two questions. WRT value vs reference, when using the value, the argument is copied which can be expensive. For integer there is no significant difference but for complex types this can be significant. Additionally, passing the reference allows you to modify the original object. – Zdeslav Vojkovic Aug 31 '20 at 16:01
  • 1
    Big structs or variables, that own data on the heap (e.g. `std::string` or `std::vector`) are usually not passed by value, due to the unneeded work (copies, allocating on the heap, ...). Let's say you write a function to sum up the values of a vector. Since you only need to read values from the vector and not write to it, it is best to pass it as a `const std::vector&` (`const` meaning the argument is passed by reference, but cannot be modified in the function). – Niklas Mohrin Aug 31 '20 at 16:06
  • 1
    If you have two distinct questions you have to ask them in two questions and not one. – t.niese Aug 31 '20 at 16:10
  • 1
    [What's the difference between passing by reference vs. passing by value?](https://stackoverflow.com/questions/373419), [What exactly is the difference between “pass by reference” in C and in C++?](https://stackoverflow.com/questions/13654138), [Pass by value vs Pass by reference(difference in space allocation of memory between the two)](https://stackoverflow.com/questions/18050223) – t.niese Aug 31 '20 at 16:11
  • Thx for all your answers. Next time i will ask to separate questions, sry :( – Martin Schmitz Aug 31 '20 at 16:21

1 Answers1

1

About when do you define the function, it depends. I like to declare at the top and define at the bottom because if the definition is long, you dont have to scroll a lot to get to the main function. But it really depends on your preference

When you pass by value, you give the function a copy of the object. When you pass by reference, you give a reference to the object.

// The original x wont chance
void foo(int x) {
   x++;
}

// Will increment x by 1
void bar(int &x) {
  x++;
}

So if you want to modify the object, you can pass it by reference.

If you dont want to copy it, (stuff like vectors, strings, etc) you can pass it by const reference.

void baz(const SomeBigObject& o) {
/*
    Define your function here
*/
}
Pablochaches
  • 968
  • 10
  • 25