In this C++ article, i want to show you How to Create Functions in C++, Functions are used to group codes for performing specific tasks. For defining a function first you need to write the return type of the function, after that you need to write the name of the function. So the return type is just the data type of the value that function returns. so if your function returns an integer you need to use the return_type as int, or if returns a float you need to use the return type as float. If our functions is not returning any value we need to use void.
1 2 3 |
returnType functionName(parameterList) { function body } |
Also you can check more articles on c++ programming language
1: C++ Introduction & Program Structure
2: C++ Variables And Data Types
8: C++ Arrays And Multi Dimensional Array
So now let’s create our function in C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include<iostream> using namespace std; void greeting(); int main() { greeting(); greeting(); greeting(); return 0; } void greeting() { cout << "Hello World" << endl; } |
Function Prototype
If a user-defined function is defined after main() function, compiler will show error. It is because compiler is unaware of user-defined function, types of argument passed to function and return type. in c++ function is prototype is declaration of function without its body. as in the above example we have used function prototype.
If you run the code this will be the result
1 2 3 4 |
Hello World Hello World Hello World Press any key to continue . . . |
This is the second way of creating functions in C++ .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include<iostream> using namespace std; void greeting() { cout << "Hello World" << endl; } int main() { greeting(); greeting(); greeting(); return 0; } |
So you can see in the above example we have created our function at the top, when you have created your function at the top, you do not need to use function prototype.
If you run the result will be the same.
1 2 3 4 |
Hello World Hello World Hello World Press any key to continue . . . |
Function Parameters and Return Type
As we have said the you can use parameters in function, also you can return a value in a function.
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 32 33 |
#include<iostream> using namespace std; int add(int a, int b) { int c = a + b; return c; } int main() { int num1, num2, sum; cout << "Please enter first number : "; cin >> num1; cout << "Please enter second number : "; cin >> num2; sum = add(num1, num2); cout << num1 << " + " << num2 << endl; cout << "The sum is " << sum << endl; return 0; } |
So you can see that in the above example, our functions is returning an integer value, and also i have given two parameters for my function, basically our function is returning the addition of two value based on user input.
If you run the code, this will be the result
1 2 3 4 5 |
Please enter first number : 6 Please enter second number : 5 6 + 5 The sum is 11 Press any key to continue . . . |
Default Parameter
Also you can give default parameter to a function like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include<iostream> using namespace std; int add(int a, int b = 10) { return a + b; } int main() { int result = add(3); cout << result << endl; return 0; } |
Also you can watch the complete video for C++ How to Create Functions
Subscribe and Get Free Video Courses & Articles in your Email