[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppFunction
A function has a declaration and a definition. The compiler needs to have read the function declaration before this function is used. If the definition is nowhere provided, this results in a linking error.
Functions can also be static. This means that it can only be called from its unit/module.
For example, consider a C-style function that takes three pointers as an argument:
A const-correct programmer immediatly assumes that all three structures will be modified.
Looking further in the code, however:
Using a comment does not enforce the compiler to give a compile error when the 'unchanged' structures ARE changed.
Using const will make the compiler complain. Also, it makes the comments obsolete:
Still, this function is not perfect. The programmer of it chose to use pointers to prevent copying of his/her structures. Still, this code will compile:
In C++, however, you can use references instead.
Note that there is no more pointer needed!
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppFunction
(C++) Function
A function is "a named sequence of statements that can be invoked/called given arguments and that might return a value. The type of the function includes the number and types of argument and the type of the value returned, if any" [1].
[return type] [function name]( [arguments] )
{
[statements]
}
A function has a declaration and a definition. The compiler needs to have read the function declaration before this function is used. If the definition is nowhere provided, this results in a linking error.
//Declaration of calculateSum double calculateSum(const std::vector<double>&); int main() { std::vector<double> myVector; myVector.push_back(1.1); myVector.push_back(2.2); myVector.push_back(3.3); myVector.push_back(4.4); //Compiler trusts that calculateSume exists somewhere... std::cout << "myVector has sum: " << calculateSum(myVector) << std::endl; } //Definition of the function //Removing this part returns in a linking error double calculateSum(const std::vector<double>& anyVector) { double sum = 0.0; const int size = anyVector.size(); for (int i=0; i<size; ++i) { sum+=anyVector[i]; } return sum; }
- include <iostream>
- include <vector>
Functions can also be static. This means that it can only be called from its unit/module.
static void sayHello()
{
std::cout << "Hello" << std::endl;
}
Reading and using functions
The keyword const can be used to specify in the declaration what the function does.For example, consider a C-style function that takes three pointers as an argument:
void SomeFunction(MyStruct1 * p1, MyStruct2 * p2, MyStruct3 * p3);
A const-correct programmer immediatly assumes that all three structures will be modified.
Looking further in the code, however:
void SomeFunction(MyStruct1 * p1, MyStruct2 * p2, MyStruct3 * p3)
{
//In : p1 and p2, will not be changed
//Out: p3
}
Using a comment does not enforce the compiler to give a compile error when the 'unchanged' structures ARE changed.
Using const will make the compiler complain. Also, it makes the comments obsolete:
void SomeFunction(const MyStruct1 * p1, const MyStruct2 * p2, MyStruct3 * p3);
Still, this function is not perfect. The programmer of it chose to use pointers to prevent copying of his/her structures. Still, this code will compile:
void SomeFunction(const MyStruct1 * p1, const MyStruct2 * p2, MyStruct3 * p3)
{
delete p1;
delete p2;
delete p3;
//The rest of the function
}
In C++, however, you can use references instead.
void SomeFunction(const MyStruct1& s1, const MyStruct2& s2, MyStruct3& p3)
Note that there is no more pointer needed!
Related topics
Function examples on CodePedia
- Bubble sort
- DoubleToStr
- FileExists
- FileToVector
- GetExtension
- Insertion sort
- IntToStr
- Selection sort
- SeperateString
- Stretch (on a vector)
- StripExtension
- StrIsDouble
- StrIsInt
- StrToDouble
- StrToInt
- swap
- ToLowerCase
- ToUpperCase
C++ standard functions
From [2].- std::abort
- std::abs
- std::acos
- std::asctime
- std::asin
- std::atan
- std::atan2
- std::atexit
- std::atof
- std::atoi
- std::atol
- std::bsearch
- std::btowc
- std::calloc
- std::ceil
- std::clearerr
- std::clock
- std::cos
- std::cosh
- std::ctime
- std::difftime
- std::div
- std::exit
- std::exp
- std::fabs
- std::fclose
- std::feof
- std::ferror
- std::fflush
- std::fgetc
- std::fgetpos
- std::fgets
- std::fgetwc
- std::fgetws
- std::floor
- std::fmod
- std::fopen
- std::fprintf
- std::fputc
- std::fputs
- std::fputwc
- std::fputws
- std::fread
- std::free
- std::freopen
- std::frexp
- std::fscanf
- std::fseek
- std::fsetpos
- std::ftell
- std::fwide
- std::fwprintf
- std::fwrite
- std::fwscanf
- std::getc
- std::getchar
- std::getenv
- std::get
- std::getwc
- std::getwchar
- std::gmtime
- std::isalnum
- std::isalpha
- std::iscntrl
- std::isdigit
- std::isgraph
- std::islower
- std::isprint
- std::ispunct
- std::isspace
- std::isupper
- std::iswalnum
- std::iswalpha
- std::iswcntrl
- std::iswctype
- std::iswdigit
- std::iswgraph
- std::iswlower
- std::iswprin
- std::iswpunct
- std::iswspace
- std::iswupper
- std::iswxdigit
- std::isxdigit
- std::labs
- std::ldexp
- std::ldiv
- std::localeconv
- std::localtime
- std::log
- std::log10
- std::longjmp
- std::malloc
- std::mblen
- std::mbrlen
- std::mbrtowc
- std::mbsinit
- std::mbsrtowcs
- std::mbstowcs
- std::mbtowc
- std::memchr
- std::memcmp
- std::memcpy
- std::memmove
- std::memset
- std::mktime
- std::modf
- std::perror
- std::pow
- std::printf
- std::putc
- std::putchar
- std::puts
- std::putwc
- std::putwchar
- std::qsort
- std::raise
- std::rand
- std::realloc
- std::remove
- std::rename
- std::rewind
- std::scanf
- std::setbuf
- std::setlocale
- std::setvbuf
- std::signal
- std::sin
- std::sinh
- std::sprintf
- std::sqrt
- std::srand
- std::sscanf
- std::strcat
- std::strchr
- std::strcmp
- std::strcoll
- std::strcpy
- std::strcspn
- std::strerror
- std::strftime
- std::strlen
- std::strncat
- std::strncmp
- std::strncpy
- std::strpbrk
- std::strrchr
- std::strspn
- std::strstr
- std::strtod
- std::strtok
- std::strtol
- std::strtoul
- std::strxfrm
- std::swprintf
- std::swscanf
- std::system
- std::tan
- std::tanh
- std::time
- std::tmpfile
- std::tmpnam
- std::tolower
- std::toupper
- std::towctrans
- std::towlower
- std::towupper
- std::ungetc
- std::ungetwc
- std::vfprintf
- std::vfwprint
- std::vprintf
- std::vsprintf
- std::vswprintf
- std::vwprint
- std::wcrtom
- std::wcsca
- std::wcsch
- std::wcscm
- std::wcscol
- std::wcscp
- std::wcscsp
- std::wcsftim
- std::wcsle
- std::wcsnca
- std::wcsncm
- std::wcsncp
- std::wcspbr
- std::wcsrch
- std::wcsrtomb
- std::wcssp
- std::wcsst
- std::wcsto
- std::wcsto
- std::wcsto
- std::wcstomb
- std::wcstou
- std::wcsxfr
- std::wcto
- std::wctom
- std::wctran
- std::wctyp
- std::wmemch
- std::wmemcm
- std::wmemcp
- std::wmemmov
- std::wmemse
- std::wprint
- std::wscan
'Function' links
Code links
- #include
- iostream
- vector
- main
- std
- scope operator'::'
- cout
- stream out operator '<<'
- endl
- return
- const
References
- 1) Bjarne Stroustrup's C++ Glossary. http://www.research.att.com/~bs/glossary.html
- 2) C++ International Standard. ISO/IEC 14882. Second edition. Table 99.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
