Disclaimer: the following is not legal advice. It's only my own personal opinion as IT professional. For legal advice, you should consult a lawyer or a qualified legal expert.
1.Where to put the notice
The copyright notice shall according to US law:
(...) be affixed to the copies in such manner and location as to give reasonable notice of the claim of copyright.
You could put the notice close to the copyrighted functions as you intended to do. But there is a strong risk that this might not be considered giving a "reasonable notice". Imagine someone would copy your file. Immediately at the top he would see your copyright and give you the credit. But would he scroll down 3000 lines, reading them attentively to find out that there is someone else who deserves credit as well ?
This is why the usual practice is to put the copyright notice right at the beginning of the file, and everyone looks there to find out. (Note also that if you'd formally register your software, the process would require you to deposit a printout (or PDF) with the first and the last 25 pages. This reinforces the start of the file as the place for copyright notice.)
If despite of this you'd pursue the way you intend to, you should at least inform in your notice at the beginning of the file, that some parts are copyrighted by third parties, and that the respective notices can be found within the code.
This being said, another easy way to handle it would be to put sections with different copyrights/licenses in different files. Not only will this ease the copyright attribution, but it will also ease maintenance/synchronisation with the original software (aka bug correction).
2.What is protected ?
Not everything is subject to copyright protection. Only original software is really protected:
(..) it is a programmer’s expression of some
functionality that may be protected by copyright, and not the
functionality itself. If code embodies the only way (or one of very
few ways) to express its underlying functionality, that code will be
considered unoriginal because the expression is inseparable from the
And what is not copyrighted doesn't need a license.
But I guess that if you copied/pasted 5 lines and didn't write them yourself, whereas you wrote the 40 other lines, they must bear some kind of originality, which brings us back to 1.
The MIT license is based on copyright principles:
- It starts with the copyright notice itself, including the name of the author.
- Verbatim: "The above copyright notice and this permission notice shall be included (...)"
So if you use a MIT License, the permission notice is the same. But you still need to attribute copyright to the other author. Not mentioning the other authors, or at least that there are other notices for limited parts would not be so fair.
Some further reading:
- This article gives some idea about how to attribute copyright to other authors
- This article looks at best practice for the copyright notices in open source software projects