C++ Functions C-PLUS-PLUS
- C++ Introduction
- C++ Syntax
- C++ Output (cout <<)
- C++ Comments
- C++ Variables
- C++ User Input
- C++ Data Types
- C++ Operators
- C++ Strings
- C++ Math
- C++ Booleans
- C++ If Else
- C++ Switch
- C++ While Loop
- C++ Do/While Loop
- C++ For Loop
- C++ Break and Continue
- C++ Arrays
- C++ References
- C++ Pointers
- C++ Functions
- C++ Functions Parameters
- C++ Function Overloading
- C++ OOP
- C++ Classes and Objects
- C++ Class Methods
- C++ Constructors
- Destructors in C++
- C++ Access Specifiers
- C++ Encapsulation
- C++ Inheritance
- C++ Polymorphism
- C++ Files
- C++ Exception Handling
C++ Functions
C++ Functions
A function is a block of code that only runs when it is called. You can pass data, known as parameters, into a function. Functions are used to perform certain actions, and they are important for reusing code: Define the code once and use it many times.
Create a Function
C++ provides some pre-defined functions, such as main()
, which is used to execute code. But you can also create your own functions to perform certain actions.
To create (often referred to as declare) a function, specify the name of the function, followed by parentheses ():
Syntax
// code to be executed
}
myFunction()
is the name of the functionvoid
means that the function does not have a return value. You will learn more about return values later in the next chapter- inside the function (the body), add code that defines what the function should do
Calling a Function
To call a function, write the function's name followed by two parentheses ()
and a semicolon ;
Example: Inside main, call myFunction():
#include <iostream>
using namespace std;void myFunction() {
cout << "This is my first Program";
}int main() {
myFunction();
return 0;
}
Output:
This is my first Program
A function can be called multiple times also.
cout << "This is my first Program\n";
}
int main() {
myFunction();
myFunction();
myFunction();
return 0;
}
// This is my first Program
// This is my first Program
// This is my first Program
Function Declaration and Definition
- Declaration: the function's name, return type, and parameters (if any)
- Definition: the body of the function (code to be executed)
// the body of the function (definition)
}
Note: If a user-defined function, such as myFunction()
is declared after the main()
function, an error will occur. It is because C++ works from top to bottom; which means that if the function is not declared above main()
, the program is unaware of it:
myFunction();
return 0;
}
void myFunction() {
cout << "This is my first Program";
}
// Error
5:3: error: 'myFunction' was not declared in this scope
You will often see C++ programs that have function declaration above main()
, and function definition below main()
. This will make the code better organized and easier to read:
Example
#include <iostream>
using namespace std;// Function declaration
void myFunction();// The main method
int main() {
myFunction(); // call the function
return 0;
}// Function definition
void myFunction() {
cout << "This is my first program";
}
Output:
This is my first program