Functions in C++

In a C++ programming language a function is a group of statements that together perform a special task.

Every C++ program has at least one function, which is main().

In a C++ library has a numerous predefined or built-in functions.

For example, strcat() to concatenate two strings, main() from where program execution starts. 

In a C++  programming function has a 3 part:

  • Function declaration
  • Function definition
  • Function call

Note: In a C++ program we can remove the function definition but we must place the function definition  before the main() function.

Function declaration : function declaration tells the compiler about a function’s name, return type, and parameters.

A syntax of function declaration –

Example of  the function declaration is as follows −

here int is a return type of function which tells function will return integer value and max is a name of the function.

“ int num1, int num2” is a parameter list which tells max function will receive two integer arguments.

Arguments are optional which depends on program requirements.

Function definition : function definition is an actual body of the function.

Syntax of function definition:

function definition consist a return type, function name, parameter list and body of the function.

Function call : whenever  program calls a function, the flow of control is transferred to the called function definition.

A called function performs a defined task and when flow of control reached to function-ending closing brace, flow of control returns to back to the main program.

Example of a user defined function.

Output

Description: Program execution start from main() function.

In main() when DemoFunction();  executes it calls and transfer control to the function definition.

Then statements of DemoFunction() execute and print “Hello Aditya”.

Note: In a C++ function can be called multiple times.

Output

Note: In a C++ program we can remove the function definition but we must place the function definition  before the main() function.

Output

User-defined function can’t declared after the main()

In a C++ program we can remove the function definition but we must place the function definition  before the main() function. 

If we place the function definition after main() then we will get the error message.

Because in a C++ execution occurs from top-to-bottom and if the function is not declared above main(), the program is unaware of it.

Categories of function

We can categories the function as:

  • Function with no argument, no return value
  • Function with  argument but  no return value
  • Function with  argument and  return value
  • Function with no argument, but  return value

Function  with  no  argument  & no return value

Example: Write a program to find the area of circle using function with no argument, no return value.

The formula for the area of the circle is :

Area_circle = Π * r * r

Program: To calculate Area of Circle

Output

Function  with  argument  but  no return value

Example: Write a program to find the area of circle using function with argument but no return value.

Output

Function  with  argument  &  return value

Example: Write a program to find the area of circle using function with argument but return value.

Output

Function with no argument but  return value

Example: Write a program to find the area of circle using function with no argument but return value.

Output

Call by value & Call by reference

Call by value : Whenever we call a function and passes the value of variable to the called function is called call by value.

Output

Call by reference : Whenever we call a function and passes the reference of variable to the called function is called call by reference.

In the call by reference, to pass the reference of the variable we use pointer.

Output