Why function declaration in main.cpp?

Untitled


why do we have to write function declaration in main.cpp? how are function definitions accessed from my_functions.cpp?

It’s not necessary, but it is one way to organize it. Sometimes you’ll them have them in header files.

It really depends on the organization style that your team uses. As you see more of other people’s code you’ll get a gist of what seems more organized and sensible (particularly for larger projects).

thank you for answering :grinning:. I wanted to ask if the program can be run without declaring functions in main.cpp

Of course. You just need a header file or something like that.

hello.hpp

#include <iostream>

void hello_world()
{
    std::cout << "Hello, world!";
}

hello.cpp

#include "hello.hpp"

int main()
{
    hello_world();
    return 0;
}


okay, but i get error message if i did not declare functions

double average(double num1, double num2);
int tenth_power(int num);
bool is_palindrome(std::string text);

Look at my example though. What are the file names? What are the includes?
Also, codecademy environment is meant to teach you specific things in a specific way, it might not be open to every approach. If you’re going to do it in codecademy, definitely try to get it right in the lesson’s way first before trying different styles.

1 Like

Hey thank you so much! looking over the example you gave and understanding it, made it much more clear! cheers :grinning:

1 Like