What is Function/Method in C/C++ Programming

What is function in c/c++ programming?

Function is a block of code that perform some specific action when it is called in main function

Before diving into function we should define types of programming. There are two types of programming i.e.

  1. Structured Programming
  2. Un-Structured Programming

If You have knowledge about functions then practice to next lesson OOP classes.

Un-Structured Programming

In Un-Structured Programming whole logic of program is defined only in one function. For example, if we are programming a calculator. Calculator have four functions i.e. sum, subtraction, multiplication division. All four parts are defined in the same main() function.

What are the problems in Un-Structured Programming?

It is difficult to debug because complete logic of program is defined in one function.

It is difficult to read.

To solve above problem, we have structured programming

Structured Programming

In this type of programming whole logic of program is break down in smaller parts or modules.

Debugging is very easy in this type of programming. It is easy to understand or read.

For structured programming we have learn functions.

Now let’s formally define functions.

If You Already have idea about function please practice exercise.

What is Function?

Function is a block of code that perform some specific action. For example, if we have a function named like sum. It should calculate sum.

There are two types of functions.

  1. Built in Function.
  2. User Defined Function.

Built in Function

These functions are the part of language. They are pre-defined meaning in language.

For example, sqrt(number) function is used to find square root of any number.

User defined Function

These functions are defined by user. User define these function according to his needs. In this article we are discussing user defined function.

In C++ Programming a function must have three parts.

  1. Function declaration.
  2. Function Definition.
  3. Function Call.

How to declare a Function?

The process of specifying the function name and return type is known as function declaration.

It tells the compiler about the function name, return type and parameters.


Return-type function-name ( parameters );
void display();

Above function have a return type void, function name display and function have no parameters. Braces after function tells the complier it is a function.

How to define a function?

The process of specifying the function return type, function name as well as its working is known as function definition.


void display()
  cout<<"Function Called";


The above function display a message when it is called in main() function.

How to call a function?

Function is called in main() function.

We call function using its name.

display(); //Function Call.


Write a program that declare and defines a function display which prints your name when it is called in main function.

using namespace std;
void display();
void display()
cout<<”Your Name”;

We have three type of user defined function

  1. Function with no parameter and null/void return type.
  2. Function with parameters but null/void return type.
  3. Function with parameters and some return type like int, float etc.

We will understand all these types using a program.

We have defined a function with no parameters and null/void return type above.

Let’s see function with no parameters but no return type.

Write a program the defines a function sum which inputs two number and display their sum when it is called in main function

using namespace std;
void sum();
void sum()//Function Definition
	int a, b, sum = 0;
cout<<”Enter Two Numbers = “;
sum = a+b;
cout<<”Sum= “<<sum;
sum();//Function Call

Leave a Reply