[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
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.

  1. include <iostream>
  2. include <vector>
//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; }


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

C++ standard functions

From [2].

  1. std::abort
  2. std::abs
  3. std::acos
  4. std::asctime
  5. std::asin
  6. std::atan
  7. std::atan2
  8. std::atexit
  9. std::atof
  10. std::atoi
  11. std::atol
  12. std::bsearch
  13. std::btowc
  14. std::calloc
  15. std::ceil
  16. std::clearerr
  17. std::clock
  18. std::cos
  19. std::cosh
  20. std::ctime
  21. std::difftime
  22. std::div
  23. std::exit
  24. std::exp
  25. std::fabs
  26. std::fclose
  27. std::feof
  28. std::ferror
  29. std::fflush
  30. std::fgetc
  31. std::fgetpos
  32. std::fgets
  33. std::fgetwc
  34. std::fgetws
  35. std::floor
  36. std::fmod
  37. std::fopen
  38. std::fprintf
  39. std::fputc
  40. std::fputs
  41. std::fputwc
  42. std::fputws
  43. std::fread
  44. std::free
  45. std::freopen
  46. std::frexp
  47. std::fscanf
  48. std::fseek
  49. std::fsetpos
  50. std::ftell
  51. std::fwide
  52. std::fwprintf
  53. std::fwrite
  54. std::fwscanf
  55. std::getc
  56. std::getchar
  57. std::getenv
  58. std::get
  59. std::getwc
  60. std::getwchar
  61. std::gmtime
  62. std::isalnum
  63. std::isalpha
  64. std::iscntrl
  65. std::isdigit
  66. std::isgraph
  67. std::islower
  68. std::isprint
  69. std::ispunct
  70. std::isspace
  71. std::isupper
  72. std::iswalnum
  73. std::iswalpha
  74. std::iswcntrl
  75. std::iswctype
  76. std::iswdigit
  77. std::iswgraph
  78. std::iswlower
  79. std::iswprin
  80. std::iswpunct
  81. std::iswspace
  82. std::iswupper
  83. std::iswxdigit
  84. std::isxdigit
  85. std::labs
  86. std::ldexp
  87. std::ldiv
  88. std::localeconv
  89. std::localtime
  90. std::log
  91. std::log10
  92. std::longjmp
  93. std::malloc
  94. std::mblen
  95. std::mbrlen
  96. std::mbrtowc
  97. std::mbsinit
  98. std::mbsrtowcs
  99. std::mbstowcs
  100. std::mbtowc
  101. std::memchr
  102. std::memcmp
  103. std::memcpy
  104. std::memmove
  105. std::memset
  106. std::mktime
  107. std::modf
  108. std::perror
  109. std::pow
  110. std::printf
  111. std::putc
  112. std::putchar
  113. std::puts
  114. std::putwc
  115. std::putwchar
  116. std::qsort
  117. std::raise
  118. std::rand
  119. std::realloc
  120. std::remove
  121. std::rename
  122. std::rewind
  123. std::scanf
  124. std::setbuf
  125. std::setlocale
  126. std::setvbuf
  127. std::signal
  128. std::sin
  129. std::sinh
  130. std::sprintf
  131. std::sqrt
  132. std::srand
  133. std::sscanf
  134. std::strcat
  135. std::strchr
  136. std::strcmp
  137. std::strcoll
  138. std::strcpy
  139. std::strcspn
  140. std::strerror
  141. std::strftime
  142. std::strlen
  143. std::strncat
  144. std::strncmp
  145. std::strncpy
  146. std::strpbrk
  147. std::strrchr
  148. std::strspn
  149. std::strstr
  150. std::strtod
  151. std::strtok
  152. std::strtol
  153. std::strtoul
  154. std::strxfrm
  155. std::swprintf
  156. std::swscanf
  157. std::system
  158. std::tan
  159. std::tanh
  160. std::time
  161. std::tmpfile
  162. std::tmpnam
  163. std::tolower
  164. std::toupper
  165. std::towctrans
  166. std::towlower
  167. std::towupper
  168. std::ungetc
  169. std::ungetwc
  170. std::vfprintf
  171. std::vfwprint
  172. std::vprintf
  173. std::vsprintf
  174. std::vswprintf
  175. std::vwprint
  176. std::wcrtom
  177. std::wcsca
  178. std::wcsch
  179. std::wcscm
  180. std::wcscol
  181. std::wcscp
  182. std::wcscsp
  183. std::wcsftim
  184. std::wcsle
  185. std::wcsnca
  186. std::wcsncm
  187. std::wcsncp
  188. std::wcspbr
  189. std::wcsrch
  190. std::wcsrtomb
  191. std::wcssp
  192. std::wcsst
  193. std::wcsto
  194. std::wcsto
  195. std::wcsto
  196. std::wcstomb
  197. std::wcstou
  198. std::wcsxfr
  199. std::wcto
  200. std::wctom
  201. std::wctran
  202. std::wctyp
  203. std::wmemch
  204. std::wmemcm
  205. std::wmemcp
  206. std::wmemmov
  207. std::wmemse
  208. std::wprint
  209. std::wscan

'Function' links

Code links

References



last edited (March 18, 2007) by bilderbikkel, Number of views: 8299, Current Rev: 22 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.