confused about static function vs static variable

Please anyone help me understand static function and static variable in an implementation file for example

1
2
3
4
5
6
7
8
9
include "calculator.h"

static int result

static void add(int a, int b){

 result=a+b;

} 


how it would be different if I used

1
2
3
4
5
6
7
8
9
include "calculator.h"

static int result

void add(int a, int b){

 result=a+b;

} 


Last edited on
Using static makes the function have internal linkage which means it will only be available in the current translation unit.

If you don't use static the function will have external linkage which means the function is available to your whole program. All you need to do is to declare the function and then use it.

1
2
3
4
5
6
void add(int, int);

void someOtherPlace()
{
	add(1, 2);
}
so static var and static func is different?
so static var and static func is different?

Yes, in the same sense that non-static variables and non-static functions are two different things.

As Peter87 said, the static keyword only changes the linkage.



thanks..btw can u pls look at this?

http://www.cplusplus.com/forum/general/140931/
Topic archived. No new replies allowed.