Overloading functions??? What is it?

Oct 22, 2017 at 1:59pm
G'day all, so I am having difficulty understanding the concept of overloading functions, don't really know what it is in the first place and can't seem to find a nice elegant definition. Can anyone explain it in layman terms?

I have an exam coming up on C++ programming and the following came up as a practise question:

Question 34) Choose the correct statement:
A) Delcaring a function with a default arguments: void f(int a = 1, int b);
B) When overloading a function: the type and or number of parameters in each version must be different
C) When overloading a function: the return type in each version must be different
D) When overloading a function f() three times: The compiler provides prototypes and declarations for the 3 different versions but the programmer decides which one to use
E) When overloading a function f() three times: The compiler decides which one to use depending on the return type.


Does anyone know the answer??????

Cheers!
Oct 22, 2017 at 2:11pm
Overloaded functions are functions with the same name but different parameters.
Oct 22, 2017 at 2:11pm
Overloading is having multiple copies of the same function with different parameters.

The most common is the constructor of a class:

class foo

foo::foo(); //default constructor
foo::foo(foo x); //'copy' constructor, idea is the new one is a copy of 'x'
foo::foo(int a, double b); //presumably initializes the data in the class to a and b

those are 3 overloads of the constructor.

B is true.
A actually looks to be true as well. But it has nothing to do with overloading; I don't see anything wrong with it.

C is not true. You can have the same return type on each version.
D is not true, the compiler does not generate code
E may be true as some sort of internal optimization, but because of C, it is not always true nor can that be the ONLY way to choose which version.

Oct 22, 2017 at 2:31pm
jonnin wrote:
A actually looks to be true as well. But it has nothing to do with overloading; I don't see anything wrong with it.

If you specify a default argument for a parameter all parameters that comes after must also have default arguments.

jonnin wrote:
E may be true as some sort of internal optimization, but because of C, it is not always true nor can that be the ONLY way to choose which version.

Only the arguments (their type and the number of them) are used to decide which of the overloaded functions to call.
Last edited on Oct 22, 2017 at 2:32pm
Oct 22, 2017 at 2:38pm
A actually looks to be true as well. But it has nothing to do with overloading; I don't see anything wrong with it.

A is incorrect as well. Look closely at the definition: void f(int a = 1, int b); . This function is incorrectly trying to use default parameters. You can't have a parameter with a default value followed by an argument with no default value. Every parameter following a parameter with a default value must also have default values.

1
2
3
void f(int a, int b = 1); // Okay
void f(int a = 1, int b = 4); // Okay
void f(int a = 1, int b); // Error 


E may be true as some sort of internal optimization, but because of C, it is not always true nor can that be the ONLY way to choose which version.

E is also always incorrect.
E) When overloading a function f() three times: The compiler decides which one to use depending on the return type.

The return type is never evaluated in the selection of the proper function to use, it is only the type and number of parameters that are evaluated.


Last edited on Oct 22, 2017 at 2:41pm
Oct 22, 2017 at 2:50pm
The declaration void f( int a = 1, int b ); is well-formed provided that the function had been declared earlier, in the same scope, with a default for the second argument.

For example:
1
2
3
void f( int a, int b = 22 ) ;

void f( int a = 1, int b ) ; // fine  
Oct 22, 2017 at 6:06pm
B is the only correct answers here. A is talking about default arguments - arguments you are not supposed to provide/pass to the function next time you call it. C can't be "completely" correct because when overloading functions the major difference between the overloaded functions is the type and/or number of arguments not the return type. If you want different return types you have to make sure that the parameters passed are different. D is not correct because it's you who provides function prototypes and declarations not the compiler. Finally, for E to be correct, it must satisfy the conditions I mentioned for why C is not correct (different return types is not sufficient for the compiler to identify which function you are trying to refer to). I hope that helps you.

P.S. I said D was correct first, but thanks to "Duthomhas" who helped us realize that
D is one sneaky wrong answer. You're the man @Duthomhas!
Last edited on Oct 25, 2017 at 7:59am
Oct 22, 2017 at 6:36pm
Wow, it is amazing the plethora of incorrect information being added-on to the already correct information listed here.

A: “Declaring a function with default arguments”
--- NO.
--- Overloaded functions are more than one function with the same name.
--- While default argument processing can affect overload selection, it is not the sole
--- criterion for creating an overloaded function.

B: “When overloading a function: the type and or number of parameters in each version must be different”
--- YES.
--- Multiple functions with the same name may exist ONLY IF the compiler can distinguish
--- them by type and/or number of formal arguments.

C: “When overloading a function: the return type in each version must be different”
--- NO.
--- Return type plays no part in function overload resolution.
--- (It opens too big a can of worms to try to figure out which function is meant by return type.)

D: “When overloading a function f() three times: The compiler provides prototypes and declarations for the 3 different versions but the programmer decides which one to use”
--- NO.
--- The compiler never provides prototypes or declarations. That’s the programmer’s job.
--- (The compiler is permitted to assume some information about non-prototyped functions,
--- but this is a carryover from old C stuff and is best avoided.)

E: “When overloading a function f() three times: The compiler decides which one to use depending on the return type.”
--- NO.
--- See C.

tl;dr
ONLY ‘B’ IS CORRECT.
- A is not correct.
- C is not correct.
- D is not correct!
- E is not correct!
Oct 23, 2017 at 1:20am
Repeat:

The declaration void f( int a = 1, int b ); is well-formed provided that the function had been declared earlier, in the same scope, with a default for the second argument.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

void f( int a, int b = 22 ) ;

void f( int a = 1, int b ) ; // fine

int main()
{
    f() ; // f( 1, 22 )

    void f( int a, int b = -8 ) ;
    void f( int a = 99, int b ) ; // fine
    f() ; // f( 99, -8 )
}

void f( int a, int b ) { std::cout << "f( " << a << ", " << b << " )\n" ; }

http://coliru.stacked-crooked.com/a/840d8daa37769b55
http://rextester.com/XWGZ80831
Last edited on Oct 23, 2017 at 1:41am
Oct 23, 2017 at 2:47am
> Question 34) Choose the correct statement:

A) Delcaring a function with a default arguments: void f(int a = 1, int b);

Correct only if the function had been declared earlier, in the same scope, with a default for the second argument.


B) When overloading a function: the type and or number of parameters in each version must be different

Correct only if none of the overloaded function are explicit instantiations of function templates.
For instance:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

template < typename T > void foo( T ) { std::cout << "template\n" ; }
void foo( int ) { std::cout << "non-template\n" ; }

template void foo<int>( int ) ; // fine: explicit instantiation of void foo(int)

int main()
{
    foo(7) ; // non-template
    foo<int>(7) ; // template
}



C) When overloading a function: the return type in each version must be different

Incorrect


D) 1. When overloading a function f() three times: The compiler provides prototypes and declarations for the 3 different versions

Correct only if the the overloads are generated by implicit instantiations of a function template


D) 2. but the programmer decides which one to use

Correct only if the template arguments are explicitly specified; other wise the compiler chooses the function by using the rules of overload resolution.


E) When overloading a function f() three times: The compiler decides which one to use depending on the return type.

I would say incorrect. Though if the overloads are generated by implicit instantiations of a function template with SFINAE being applied on the return type., this is subject to interpretation.
Last edited on Oct 23, 2017 at 2:48am
Topic archived. No new replies allowed.