7 Replies - 1020 Views - Last Post: 09 July 2011 - 12:29 PM Rate Topic: -----

#1 Ashu2912   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-November 10

Scope of Functions

Posted 08 July 2011 - 04:00 AM

I have read that a function can only be called in it's scope, i.e. in which it is declared. However in the code below, I have declared func () in main () but it is also accessible in func2 (). Plwase help!!!

#include <iostream.h>
#include <conio.h>

int func()
{
    return 5;
}
int func2()
{
     return 2*func();
}
void main ()
{
     int func();
     clrscr ();
     cout << func ();
     cout << func2();
     getch ();
}



Is This A Good Question/Topic? 0
  • +

Replies To: Scope of Functions

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Scope of Functions

Posted 08 July 2011 - 04:07 AM

You've declared both functions outside of main, and in the global scope, so they're available anywhere. You call them from main.
Was This Post Helpful? 0
  • +
  • -

#3 Ashu2912   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-November 10

Re: Scope of Functions

Posted 08 July 2011 - 04:23 AM

Quote

You've declared both functions outside of main, and in the global scope, so they're available anywhere


Isn't that the function definition, which is different from declaration/prototype??? As per what I have been taught :

int func();



in main () is called declaration....
Was This Post Helpful? 0
  • +
  • -

#4 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Scope of Functions

Posted 08 July 2011 - 04:56 AM

You've been taught badly.
1. void main is WRONG. main returns int.
2. getch () to hold the window open is bad style; use cin.get.
3. int func () in main is a local declaration.
Was This Post Helpful? 1
  • +
  • -

#5 Bench   User is offline

  • D.I.C Lover
  • member icon

Reputation: 945
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: Scope of Functions

Posted 08 July 2011 - 01:18 PM

View PostAshu2912, on 08 July 2011 - 12:23 PM, said:

Quote

You've declared both functions outside of main, and in the global scope, so they're available anywhere


Isn't that the function definition, which is different from declaration/prototype??? As per what I have been taught :

int func();



in main () is called declaration....

Its a declaration regardless of whether its in main or anywhere else, however, defining a function in a source file before its used negates the need to declare it later; but that's not usually the way people do it - putting a declaration inside another function is a fairly uncommon practice.

Typically, you would put declarations inside a header file (a file which ends in .h or .hpp), then you would put the definitions inside a source file (a file which ends in .c or .cpp or maybe .cc). Once you've done that, the usual thing would be to #include the header file at the top of wherever you want to use the function.

In short, the way you've written your functions, they're visible anywhere after their definition, but not before.

This post has been edited by Bench: 08 July 2011 - 01:20 PM

Was This Post Helpful? 3
  • +
  • -

#6 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Scope of Functions

Posted 08 July 2011 - 03:06 PM

It turns out the a function definition works as a function declaration. So by defining your functions at the top of the program you are "declaring and defining" then before main...


Take a look at some examples that give errors:
#include <iostream>

using namespace std;

//func2 uses func() but func() has not be declared or defined yet...
int func2()
{
    return 2 * func(); //ERROR -- "Call to undefined function 'func' in function func2()"
}

int func()
{
    return 5;
}


int main ()
{
    int func();
    cout << func();
    cout << func2();
    return 0;
}


next declaring in main:
#include <iostream>

using namespace std;

int main ()
{
    int func2();
    cout << func2();
    return 0;
}

int func()
{
    return 5;
}

int func2()
{
    return 2 * func(); 
}



we can use func2 in main because it is declared at the top of main()... we can use func() inside of func2() because it was defined before func2 -- not we can not use func2 within func:
#include <iostream>

using namespace std;

int main ()
{
    int func2();
    cout << func2();
    return 0;
}

int func()
{
    return 5 * func2(); //ERROR: declared in main but not in scope for func...
    //also if it did work that would create an infinite loop...bad!
}

int func2()
{
    return 2 * func(); 
}


Was This Post Helpful? 3
  • +
  • -

#7 Ashu2912   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-November 10

Re: Scope of Functions

Posted 09 July 2011 - 10:28 AM

Thanks a lot Nick and Bench...Your replies helped me a lot. Let me summarize what I have understood. Please tell me if I go wrong....
(1) A function definition is always a declaration.
(2) A function's scope is determined by it's declaration. It is accessible in it's scope appearing below it's declaration.
(3) A prototype (i.e. like int func ();) serves as a declaration if it is appears above the definition. Else, the definition serves as a declaration.
CHEERS!!!
Ashu
Was This Post Helpful? 0
  • +
  • -

#8 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Scope of Functions

Posted 09 July 2011 - 12:29 PM

1. true - a definition works as a declaration.
3. Prototypes are declarations. The two things are the same.
2. Not really sure that is 100% true... its kind of difficult for me to suss out exactly because I don't really have a model for "function scope" the way you are trying for.

Most C/C++ programs don't attempt to "limit the scope" of a function be declaring it inside of another function. So while that does "work" it is not terribly helpful. Functions actually exist at the "global" scope and so they can be "seen" anywhere, the trick is to tell the compiler they are there.

When I write a program like this:
#include <iostream>
using namespace std;

int value = 10;

int foo();

int main() {
    int value = 100;
    
    cout << "local: " << value << endl;
    cout << "global: " << ::value << endl;
    cout << "foo: " << foo() << endl;
    int foo2();
    cout << "foo2: " << foo2() << endl;
    return 0;
}

int foo() {
    return value;
}

int foo2() {
    return 99;
}


The function foo does not have access to main's local variable "value". because that variable only exists within main. I could pass a reference or pointer to it from within main, that that gives foo a different local variable though which it can access main's variable.

But since there is a value at global scope foo can access that...

So question. Can foo() access foo2? -- it is declared in main() and defined after foo() so...

sure it can!!! all it has to do is declare it before it uses it. just like main() did. The reason is because it exists at the global scope.
int foo() {
    int foo2();
    return value * foo2();
}


So unlike a variable where you can declare a local, functions are not really able to be "local" (although you can have member functions but that is a whole other box of bananas).


Simply put: You MUST declare a function before you can use it. You can not define a function within a function. a definition works as a declaration.

you can limit the "scope" of a function declaration but not of its definition (i.e. it can be declared anywhere else it is needed).

It is best to follow the standard layout for C/C++ programs:

<includes>
<declare data types>
<declare functions>
<declare constants>

main() { }

<define functions>


you will very very rarely see functions declared inside of other functions because it is not terrible useful or logical.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1