Syntax:
//C++ Example of a function #include<iostream> using namespace std; //function definition to perform addition int fun(int x, int y){ int sum = x + y; return sum; } int main(){ int a = 10, b = 20; //calling the function fun perform addition int sum = fun(a, b); cout<<"Sum: "<<sum; return 0; }
Sum: 30
Note: A function without input parameters can also perform computation and return some output. (alert-success)
Why functions are used?
There are two important reasons why we are using functions:
- Reusability: Once the function is defined, it can be reused over and over again. You can simply call the function everywhere in your program to perform that specific task.
- Abstraction: If you are just using the function in your program then you don't have to worry about how it works inside.
There are two types of functions:
- Standard Library Functions: These are built-in functions that are already available in C++ programming language and you can directly use them in your code without worrying about the implementation part.
- User-defined Functions: These are defined by the user based on their requirement to perform some specific task.
Here in this post, we are going to discuss user-defined functions in detail.
Function Declaration in C++.
A function declaration (also called function prototype) means declaring the properties of a function to the compiler. This property includes the name of the function, the return type of the function, the number of parameters, and its type.
Note: It is not necessary to put the name of the parameter in function prototype. (alert-success)
Example:
/*function taking two integers as parameter and returning an integer*/ int sum(int x, int y); /*function taking one character as parameter and returning a char*/ char fun(char ch); /*function taking two-pointer variable as a parameter and no return type*/ void sum(int*, int*); /*function taking one integer and one integer type pointer as a parameter and return an integer type pointer*/ int* greater(int, int*);
Function Definition in C++.
A function definition consists of a block of code that is capable of performing some specific task. Let's understand this with an example:
Example:
//C++ Function example to add numbers #include<iostream> using namespace std; //function declaration int add(int, int); int main(){ int m = 5, n = 15, sum; //function call sum = add(m, n); cout<<"Sum = "<<sum; return 0; } //function definition int add(int x, int y){ return (x + y); }
Output:
Sum: 20
Working of above function:
- At first, we have declared the function "add" to have two integer type parameters and the function is going to return an integer type value. There is no need to mention the name of the variable.
- Inside the main function, we are calling the function "add" and pass the two arguments. You should not mention the data types of the argument or the return type of the function.
- After the main function, we have defined our "add" function and here it is important to mention both the data type and the name of the parameters.
Error: Implicit Declaration of function
It is not necessary to declare the function before using but it is preferred to declare the function before using it. If you do not declare before using it then you will get an error "implicit declaration of function" because the compiler implicitly assumes something about the function.
Error message:
../src/main.c:48:9: error: implicit declaration of function 'add' [-Werror=implicit-function-declaration]
add();
You can resolve this error in two ways, declare the function before using it else define the function above the main function before calling it.
No comments:
Post a Comment