C++ what is function?
In C++ a function is a sub-program.It can be made a part of the larger program by calling it in the program.Every function has a name,a return type,a type with an identifier(known as parameter) which the function will make use of and a body with codes and statements inside a braces(‘{ }‘).The syntax of a function is shown below.
void function1( ) { //function body starts here //Code is written here } //function ends
The above function has a name function1 and returns void which means it returns back nothing.
The syntax of a function accepting an int value and returning an int type is shown below.
Link : C++ integer data type
int function2(int i) { return i++ ; }
The variable ‘i’ is a name given to the value which the function receives when it is called from another program.And the function returns a value ‘i++’ to the program which calls it.The ‘return’ statement signofy that we are returning something from the function.
Calling a function
If you want to use a function in your program then you have to call it first.Calling a function in a program is pretty straightforward..To call a function,simply write it’s name and if the function accepts a value(known as argument) then pass a value of the same type when it is called.If the function returns a value then the return value must be assigned to some variable where the function is called.
The function1() below accepts nothing and returns nothing,so to call it we simply write it’s name.The function2() accepts an integer so a value 9 is pass to the function by writing it inside the bracket and the return value is assigned to ii.
#include <iostream> using namespace std; void function1( ) { cout<< “A function1 is called\n” ; } int function2( int i) { cout<< “A function2 is called\n” ; return (i++) ; //An incremented value of i is return here } int main( ) { function1( ) ; //function1 is called and int ii ; ii=function2( 9 ) ; /*function2 is call,9 is passed and the returned value assign to ii */ cout<< ii ; cin.get( ) ; return 0 ; }
Output,
A function1 is called
A function2 is called
10
Note:whenever we pass a value to a function,it’s type must match with the type mentioned in the function else it may give rise to an error or you may get some unexpected value.
For instance,while calling the function function2 if we pass a string you will get an error but if you pass a float the decimal point value gets truncated or if a char is passed then it gets converted to it’s corresponding int value.In this way different type value can produce different result which can be very hard to rectify sometimes.
Another code example is given below.
string concatenateString( string st , string st1 ) { return st+st1 ; } int main() { string s="New" , s1="World" , result; result=concatenateString( s , s1); cout<< result ; cin.get( ); return 0; }
Output,
New World
What function ‘concatenateString’ does is it receive two strings and add them and then returns it.
The next post discuss what what is function declaration and definition and also their difference.