Types of Functions


There are several function subprograms that are used in computation. C++ and many other languages use mainly two types of function.

  • Built-in Functions OR System-Supplied Functions
  • User-Defined Functions

The first one provides the ability to the programmer to instruct the system to accomplish specific operations. The programmer is free in using the system-supplied functions because the code is supplied by the system. This type is also known as the intrinsic functions. User-defined functions are those, which are defined by the programmer as part of the source program. The programmer uses  this type of function, if the availability of the system supplied function is inadequate.

System-supplied functions

These are available to anyone who writes a C++ program. This saves the programmer’s time from writing own functions. They are completely debugged, efficient and always produce a precise output. The important contribution of the system-supplied functions is that they provide clarity to the program because they do not have to be redefined. It reduces the source code, which saves time of the programmer. When using this type of function, the following should be noted.

A system–supplied function involves two data types.

  • The data type of actual parameter
  • Tthe data type of the return value

The above data types may or may not be the same. Some functions accept and return the same data type, but some do not. A system-supplied function can be incorporated within program. for example,


It is used to calculate the square root of a number.

User-defined functions

C++ language allows additional functions besides the built-in functions called the user-defined function. It allows programmers to define their own functions. The programmer must code the logic of this type. In order to do so, a declaration is needed.

The general format is:

Result-type user-defined-name (parameters)

The result-type is the data type (void or predefined data type) of the function returned value and the user-defined name specifies the name of the function which allows the rules for variable names.

Functions are pieces of code which perform common tasks. We will discuss later how a function is invoked in a main function or subprogram. The main use of the function is that it always returns only one value but in C++, if there is a return value. Functions may either return a value or execute silently without returning a value. In order to illustrate how it works, consider the following program, retlarge.cpp that finds the larger of tow numbers.

Program Example:

  1. //program name: retlarge.cpp
  2. #include
  3. int retlarge (int n1, int n2);
  4. void main()
  5. {
  6. int num1, num2;
  7. cout<
  8. cin>>num1>>num2;
  9. cout<
  10. cout<
  11. }
  12. int retlarge(int n1, int n2)
  13. {
  14. If (n1 > n2)
  15. return(n1);
  16. else
  17. return (n2);
  18. }

Run output:

Enter 2 integer values

The largest of 10 and 20 is: 20

The identifiers enclosed in parentheses in the function heading are called the formal parameters. These describe both the variables that constitute input to the function and their type. The parameter names may or may not be the same. They sometimes called the dummy arguments. A function may or may not use it. A statement may either be a single statement or a compound statement and must be preceded by opening curly bracket ({) which an ending curly bracket must balance (}). The statement enclosed in {end} is called the function body. Let us examine a function that a character value upon the completion of function execution.

  1. //program name: pmsign.cpp
  2. #include
  3. char pmsign(int val)
  4. void main()
  5. {
  6. char s, sign;
  7. s  = 10;
  8. cout<
  9. s = -10;
  10. cout<
  11. }
  12. char pmsign(int val)
  13. {
  14. if (val >= 0)
  15. return (‘+’);
  16. else
  17. return (‘-‘);
  18. }

Run output:

sign = +

sign = –

As mentioned earlier, functions are separate subprogram unit which perform some special tasks. In the assignment statement a constant value has been used as an actual parameter. At the execution time, the argument 10 or -10 is substituted for the formal parameter val. The result would be (+) or plus(-).

Functions parameters are values that the function uses to produce its result. The parentheses are necessary, even if the function has no parameters.

Function return value with no parameters

Functions that return a value are one of the most useful forms of function. Let us consider a program, retupper.cpp that returns an uppercase letter if it is a lowercase letter otherwise.

Program Example:

  1. //program name: retupper.cpp
  2. #include
  3. #include
  4. char retupper(void);
  5. void main ()
  6. {
  7. char ch;
  8. clrscr();
  9. cout<
  10. }
  11. char retupper(void)
  12. {
  13. char ch;
  14. cout<
  15. cin>>ch;
  16. cout<
  17. if(ch >= 97 && ch <= 122)
  18. ch = ch-32;
  19. return(ch);
  20. }

Run output:

Type lower case letter


You typed A

Functions with no parameters are rarely used. Usually this type of function will not return a value but carry out some operation.

For example:

void message ()



The new feature, which has been given to the previous function is the return value that is the type char. whenever it encounter the return statement, it will transfers control back to the caller. C and C++ do not follow the rule known as one-entry one-exit whereas some languages return the value of a function by assigning a value to the name of the function. The rule one-entry one-exit is a very useful convention, program are more readable, understandable and less prone to bugs and also easier to maintain and modify.

Leave a Comment