Jump to content

declaration and definition of user-defined function, and declaration of structure

- - - - -

  • Please log in to reply
4 replies to this topic

#1
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Hi

As far as my knowledge and experience goes, the declaration and definition of a user-defined function is given outside the int main. One can directly define the function instead of first declaring it at the top of int main. Why is this so? Can't we declare and define the user-define function(s) inside the int main?

Almost the same goes for the structure where the structure type is declared at the top of int main. Why is so? Can't we declare the structure inside the int main. Perhaps, the answers to both haven't much to do with the common sense rather how the syntax has been defined.

Please guide me. Thanks a lot.
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
The compiler requires a list of symbols (internal names) to refer to user defined functions, if you define a function within a function this would be very hard to keep track of, and also generate confusing assemblies (machine code). For example, one function may run a loop, if another function is declared within it may break the loop's instructions, so you must always declare them outside.

As per your second question, the structure can most certainly be defined within int main. This will suffer the problem of scope though, if you define a function that accepts the struct as a parameter, the function will not know what the struct is as it is defined in main not globally.

void doSomething(struct foo* param) { //what is struct foo? it is not defined globally!
  foo->a = 1;
  foo->b = 2;
}

int main() {
  struct foo { //it is defined here however
     int a;
     int b;
  };
  struct foo* my_foo;
  doSomething(my_foo); //and means nothing when passed
}
error: cannot convert `main()::foo*' to `foo*' for argument `1' to `void doSomething(foo*)'
The struct outside of main is defined in the global scope, so that every function can access it and would fix this.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Thank you, Alexander.

So, the structure could be defined within the int main if there is no user defined function which takes it as a parameter or argument.

Unfortunately I wasn't completely able to understand the rationale behind defining the user defined function outside the int main.

Consider this:

int main()
{
//definition for the function1
void function1()
{
//statements
}


//definition for the function2
void function2()
{
//statements
}


// onward during the rest of the code I can use function calls to these two functions

// the statements for the int main()
}

Do you get my point? Please help me if you understand what I'm trying to say. In this case we aren't even concerned about the scope because we want to use the defined function within the int main.
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
You must realize that C is a very low level language, it does not re-arrange things for you, when you build the application the generated assemblies and symbols must be very clear to the compiler. If you wrote this in assembly, you could surely place labels of subroutines within other subroutines, although you will very quickly realize why that is not worth the confusion.

You could wrap functions in a class with C++ however, in nearly the same method you are wrapping them in int main, if you so were to wish to use this functionality.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Thanks a lot, Alexander. It has been really kind of you that you have taught so much about the programming.
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users