Simple c++ function program

Simple c++ function program is simple program to write c++ program using functions.

In this tutorial we will see what is function, how to declare, define and call function and c++ program using function.

What is function : It is a set of statement or block of code.

Function declaration

Declaring of the function means to define the prototype of function. It tells the compiler about function name and how to call function.

Syntax for function declaration

return_type function_name (arguments);

Function definition

Writing block of statement in function is function definition. We define the function that declared at top.

Syntax for defining function

return_type function_name(arguments)


// block of statements


Function call

After declaration and definition of function we need to call the function to execute block of statement.

Syntax for function call

return_type function name(arguments);

Algorithm for functions in c++

  1. Start
  2. Declare function
  3. Next write main function
  4. now Call function in main function
  5. Define function
  6. End

Program for function in c++

1234567891011121314151617181920212223#include <iostream>using namespace std;void fun1();//function declarationint main(){   cout<<“main function\n”;//function callfun1();cout<<“main function\n”;    return 0;}void fun1(){cout<<“function definition\n”;}

Output :

program for function
program for function

Explanation :

1. Execution starts with the main method 

2. Print that we are in main function

cout<<“main function\n”;

3. Next call function 

fun1();  => function get call and function executed.

void fun1() {

cout<<“function definition\n”;  => function definition

} => return to main function

4. Print that we are in main function

cout<<“main function\n”;

5. End with the program