r/learnprogramming • • Apr 05 '22

Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them

Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.

Someone else who reads yours and does understand it, try to explain it to them to help them learn

1.6k Upvotes

738 comments sorted by

View all comments

Show parent comments

1

u/gabrielsfarias Apr 05 '22

So overloading is just having the 2 or more functions with repeated arguments? Can I even have 2 functions with the same name?

If I have a function

sayMyName(name)
  { return name }

and another exact same function with (name, age), is this overloading?

1

u/plastikmissile Apr 06 '22

Yes. You can have any number of functions share the same name, as long as their signatures are different. A function's signature is what we call the function's arguments. So you can have all the following functions share the same name, since their signatures are different:

foo(int x) { }
foo(int x, int y) { }
foo(string x) { }

1

u/gabrielsfarias Apr 06 '22

But in your example only the repeating arguments would be the overload right? So these 3 functions have 1 overload (int x)?

2

u/plastikmissile Apr 06 '22

It doesn't matter if the arguments repeat or not. As long as the signatures are different. So you can't have:

foo(int x) {}
foo(int y) {}

Since they both use one int argument (what it's called doesn't matter).