Methods in CPLUSPLUS


Functions in CPLUSPLUS

methods in cpp

Image credit:bansalwiki.blogspot.com

A function is a logical group of statements(separated by ;). The main() function is the heart of the C++ programming environment. It is the first executed function in every C++ program.
A function contains a name, a return type and number of arguments. If we are seeing a void before function name, then void simply means “no”. It is mostly used to tell the compiler that the function is not returning any value.
The {} brackets are called block or body of function where we write our statements. simple function may be:

How to define a function?

A function can be defined as


return-type name(parameter(s))
{
//function’s block where we write statements.
}

* Return Type: A function returns the result after executing its block. There is no or a specific return type

* Function name and signature identifies a function and its tasks. Signature is referred to as the function with parameters

* Parameters:Parameters can also be defined as the input to the function. A function may have 0 or more than one parameters

* Function body: Container of statement(s) that get executed whenever we invoke a function

Calling or invoking a function

Whenever we call a function, we have to write the name as well as parameter values or simple () if no values
this is how we call a function:

int main()
{

// calculate function being called
calculate();

}

#include “stdafx.h”
#include <iostream>
using namespace std;

//function named calculateSum taking two parameters a an b
int calculateSum(int a, int b)
{

int c=a+b;
cout<< c;
cout<< endl;
return 0;
}

int main()
{

//function call
calculateSum(1,2);

}

Output

3

Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise
Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.
Syed Adeel Ahmed on FacebookSyed Adeel Ahmed on GoogleSyed Adeel Ahmed on InstagramSyed Adeel Ahmed on Twitter

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.