-3

Lets say that i have three .cpp files and 2 header files.

1st file:

it main.cpp, it calls all function and stuff. the 2 header files are included.


2nd file:

Contains two functions:

int print(int num2, int num1)

and

int update(int num1)

3rd file:

Contains three functions:

int calculate(int num2, int num3, int num4)

and

int update(int num2, char random)

and

int divied(int all)

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

As you can tell that 2nd and third file have one same named function "int update" but in one it has 1 parameter and in the other it has two.

Will I be able to call the one i want? will i get compile errors? I can't test it right now cause i am using the libraries computer.

hank99
  • 185
  • 1
  • 2
  • 11
  • 3
    Then why not wait until you get home, rather than posting a question? – Oliver Charlesworth Jun 01 '13 at 18:11
  • 1
    Search for [internal linkage and external linkage](http://stackoverflow.com/questions/1358400/what-is-external-linkage-and-internal-linkage-in-c) – dyp Jun 01 '13 at 18:11
  • 3
    There are a bunch of online compilers, e.g. http://gcc.godbolt.org/ or http://coliru.stacked-crooked.com/ – dyp Jun 01 '13 at 18:13
  • oh Thanks DyP. I never knew there are online compilers. – hank99 Jun 01 '13 at 18:49
  • @OliCharlesworth i cant get home any time soon... i have a group project to do. will be stuck here for 4 hrs... i am waiting on them to come. so i just asked a question... u dont need to be soo angry! – hank99 Jun 01 '13 at 18:53

4 Answers4

5

Technically, yes, you will. Functions with different signature form an overload set, and on call the proper one will be selected by matching the number and type of actual parameters passed.

OTOH it is an awful practice to have such name sitting around in the global namespace. It's just accident waiting to happen -- someone will call the unintended function by a typo.

Real overload sets are not formed randomly but by design: that is the a function having the same semantics, just using a different kind of ammo. They are meant to work in coalition, and work best when answer to "which one is called" is "I don't care". As whichever the compiler selects will do the proper job.

Balog Pal
  • 16,195
  • 2
  • 23
  • 37
0

The name wil be mangled in C++ and therefore you can have those functions with different parameters. TGhe compiler creates a name for them, which is based on the parameters.

That's why you can have in the same class something like:

foo(int a);
foo(int a, int b);
foo(std:string s),
etc. ...

And this also applies to normal functions.

Devolus
  • 21,661
  • 13
  • 66
  • 113
  • 2
    IMO this has nothing to do with name mangling, but rather with linkage (and/or overloading). – dyp Jun 01 '13 at 18:12
0

Yes this is permissible in C++ and is called function overloading. From C13 p1 of The C++ Language Standard

Overloading

When two or more different declarations are specified for a single name in the same scope, that name is said to be overloaded. By extension, two declarations in the same scope that declare the same name but with different types are called overloaded declarations. Only function and function template declarations can be overloaded; variable and type declarations cannot be overloaded.

As long as you call the function with the correct parameters the compiler will use the correct function.

Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74
0

Yes, that's called function overloading. And in fact, it would work in the same file. Just make sure that the functions don't take the same parameters, or the compiler wouldn't be able to distinguish them.

Nathan
  • 73,987
  • 14
  • 40
  • 69