Functions
Functions are the reusable block of a set of statements that performs a specific task. Functions can be used to organize the logic of the program.
Syntax for function declaration
return_type function_name(parameters);
Syntax for function definition
return_type function_name(parameters) {
// function body
// code to be executed
// return statement (if applicable)
}
return_type: It is the data type of the value that a function returns.
function_name: It is the name of the function.
parameters: parameters are the input values provided when the function is called. parameters are optional.
Example
Program to add two numbers.
C++
#include<bits/stdc++.h> using namespace std; // Function declaration int sum( int a, int b); // Function definition int sum( int a, int b) { return a + b; } int main() { // Function call int result = sum(3, 4); cout << result; } |
7
Explanation: The function sum takes two integers as parameters and returns the sum of the two numbers. The return type of the function is int. The parameters of the function are two integers that are 3 and 4. The returned value 7 is stored in the variable result.
C++ Cheatsheet
This is a C++ programming cheat sheet. It is useful for beginners and intermediates looking to learn or revise the concepts of C++ programming. While learning a new language, it feels annoying to switch pages and find different websites for different concepts that are easily understandable. You can learn C++ concepts very easily using this cheat sheet.
C++ is a high-level programming language. It was developed in 1983 by Bjarne Stroustrup at Bell Labs. It is used for developing various applications.
Contact Us