# Category Archives: Function

## Creating Simple Function in C++

Function: A function is a group of statements that together perform a task.

There are two major types of functions:

1. System inbuilt function.

2. User defined function.

System built in functions are the one which are by default being provided to us like the Math.sqrt() function is built in function. User defined functions are the one which are created by the users as per their requirement. In this session we are going to learn how to create the user defined functions.

Different types of User defined Simple function in C++.

• No argument and no return type function.
• Argument and no return type function.
• Argument and return type function.

``` /* Developed By:Prof. Vinod Pillai vinodthebest@gmail.com Simple Function in C++ */```

``` #include using namespace std; //--------Function Declaration--------- void msgFunction(); void sumFunction(int val1,int val2); int subFunction(int val1,int val2); int main() { //--------Function Calling--------- msgFunction(); sumFunction(100,200); int ans=subFunction(2000,1000); cout<<"\n The return value from subFunction(2000,1000) is:"<<ans<<"\n"; return 0; } //--------Function Definition--------- //Simple Function with No Argument & No Return Type void msgFunction() { cout<<"\n Welcome Message with Simple Function with no Argument & no Return type\n"; } //Simple Function with Argument & No Return Type void sumFunction(int val1,int val2) { int rel; rel=val1+val2; cout<<"\n The result of sumFunction(int,int) the result is:"<<rel<<"\n"; } ```

```//Simple Function with Argument & Return Type int subFunction(int val1,int val2) { int rel; rel=val1-val2; cout<<"\n The result of int subFunction(int,int) the result is:"<<rel<<"\n"; return rel; } ```

Filed under C++, Function

## Friend Fuction in C++

Friend Function:

A friend function is special function with the help of which different classes can access private data of each other. A friend function will be friendly with a class even though it is not a member of that class. By the term friendly we mean that the friend function can access the private members of the class. Check out the example below:

``` #include```

``` using namespace std; class Second; class First { int val1; public: void setValue(int tval1) { val1=tval1; } void display() { cout<<"\n The Value of First Class is:"<<val1<<"\n"; } friend Second sum(First fobj,Second sobj); }; class Second { int val2; public: void setValue(int tval2) { val2=tval2; } void display() { cout<<"\n The Value of Second Class is:"<<val2<<"\n"; } friend Second sum(First fobj,Second sobj); }; Second sum(First fobj,Second sobj) { Second rel; rel.val2=fobj.val1+sobj.val2; return(rel); } int main() { First f1; Second s1; f1.setValue(100); s1.setValue(200); f1.display(); s1.display(); Second rel=sum(f1,s1); ```

``` rel.display(); return 0; } ```

1 Comment

Filed under C++, Function

## Static Variables & Methods in C++

Static Variables & Methods:

``` #include```

``` using namespace std; class Student { int id; int marks; static int count; public: void setData(int tid,int tmarks,int tcount) { id=tid; marks=tmarks; count=tcount; } void display(); static void getCount() { cout<<"\n The Static count value is:"<<count; } }; int Student::count=10; void Student::display() { cout<<"\n The ID is:"<<id; cout<<"\n The marks is:"<<marks; cout<<"\n The Static Count value is:"<<count; } int main() { Student s1,s2; s1.setData(1,100,11); s2.setData(2,200,20); s1.display(); s2.display(); ```

``` Student::getCount(); return 0; } ```

Filed under C++, Function

## Function with Default Argument – C++

Function with Default Argument:

In many cases, functions have arguments that are used so infrequently that a default value would suffice. To address this, the default-argument facility allows for specifying only those arguments to a function that are meaningful in a given call. To illustrate this concept, consider the example presented in :

``` #include using namespace std; void sum(int a,int b=10); int main() { sum(10,20); sum(10); return 0; } void sum(int a,int b) { cout<<"\nThe Result from sum(int,int) function is:"<<a+b<<"\n"; } ```

Filed under C++, Function

## Inline Function in C++

Inline Function:

C++ inline function is powerful concept if a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time. To inline a function, place the keyword inline before the function name and define the function before any calls are made to the function. The compiler can ignore the inline qualifier in case defined function is more than a line.

``` #include using namespace std; inline void sum(int a,int b) { cout<<"\nThe Result from sum(int,int) function is:"<<a+b<<"\n"; } int main() { ```

``` sum(10,20); return 0; } ```

Filed under C++, Function

``` #include using namespace std; void sum(); void sum(int a,int b); int main() { sum(); sum(10,20); return 0; } void sum() { int a,b; cout<>a; cout<>b; cout<<"\n Result from sum() function is:"<<a+b<<"\n"; } void sum(int a,int b) { cout<<"\nThe Result from sum(int,int) function is:"<<a+b<<"\n"; } ```