PREV NEXT
What are Functions in C++?
- Functions are used for the structured programming which follows top-down approach.Function is a collection of statements or set of statements that perform the task together.
- In C++, every program consists of a single function that is a main( ) function. The functions take the inputs and perform the task on those inputs and provide output.
- It provides standard to a program and while creating a program we use functions which makes program easier to understand, edit and check the errors, etc.
There are two types of functions in C++:
1) Library Functions in C++:
These functions are the in-built functions provided to a programmer who writes C++ program as it saves the programmer’s time. For example: strcpy( ), strcat( ), sqrt( ), etc. Programmers can invoke the functions directly and they do not need to write it again.
2) User defined Functions in C++:
User defined functions are those functions which are defined by the user. These functions can be created by the user. It allows performing the additional functions besides the in-built functions. Some of the examples of user defied functions are: add( ), multiply( ), divide( ) etc.
Advantages of Functions:
Some of the advantages of using functions in C++ program are:
- It makes the program clear and easy to understand.
- Single functions can be tested easily for errors.
- It saves time from typing the same functions again and again.
- It helps to modify the program easily without changing the structure of a program.
How functions are declared in C++?
- In C++, the function declaration tells the compiler about a function name and how to call the function.
- While declaring the function it also tells about the number of parameters the function takes, data types of the parameters and the return type of the function.
Now, let us have a look at the example to understand the concept better:
int add(int, int); //this function takes two parameters of interger data type and returns an integer
How to define functions in C++?
In the function definition, we provide the body of the function. The statements are mentioned in the function definition that task is performed on those statements.
Let us have a look at the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include < iostream> using namespace std; int add (int x, int y); //declaring function int main() { int a = 5; int b = 20; int c = add (a, b); //calling function cout << c; } int add (int x, int y) //defining function { return (x + y); } |
How to pass Parameters in Function?
There are two types of parameters “actual and formal parameters” that are passed in a function. The actual parameters are passed in a function like a = 20, b = 30 whereas the formal parameters are those parameters which are received by the functions like int m and int n.
You can pass the parameters in the two ways:
1) Pass by value:
In the pass by value, both the actual and formal parameters refer to the same location. So, if you make any changes inside the function, then they are not reflected back in actual parameters.
2) Pass by reference:
In the pass by reference, the values of actual parameters are copied to formal parameters and both the parameters are stored in different memory locations. So, if you make any changes in formal parameters, then they are reflected back in actual parameters.
Let us have a look at the examples of Pass by value and Pass by reference:
Pass by value:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> using namesapce std; void fun(int a, int b); { a+=b; cout<<"\nIn func a="<<a<<"b="<<b; } int main (void) { //passing parameters func(x,y); cout<<"\nIn main,x="<<x<<y="<<y; return 0; } |
Pass by reference:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <iostream> using namespace std; void swapnum(int*x,int*y) { int temp = *x; *x =*y; *y = temp; } int main(void) { int a = 20, b = 30; //passing parameters swapnum(&a, &b); count<<"\na is:"<<a<<"and"<<"b is:"<<b; return 0; } |