






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







C++ Function Declaration
returnType functionName (parameter1, parameter2,...) { // function body }
// function declaration void greet() { cout << "Hello World"; }
void greet() { cout << "Hello there!"; } int main() { // calling the function greet(); return 0 ; }
Hello there! Function Parameters
void printNum(int num) { cout << num; }
int main() {
int n = 7 ; // calling the function // n is passed to the function as argument printNum(n); return 0 ; } Example 2: Function with Parameters // program to print a text #include
int add (int a, int b) { return (a + b); }
Example 3: Add Two Numbers // program to add two numbers using a function #include
int sum; // calling the function and storing // the returned value in sum sum = add( 100 , 78 ); cout << "100 + 78 = " << sum << endl; return 0 ; }
returnType functionName(dataType1, dataType2, ...); Example 4: C++ Function Prototype // using function definition after main() function // function prototype is declared before main() #include
Benefits of Using User-Defined Functions
C++ Library Functions