Assigning function to a variable in C++

In C++, assigning a function to a variable and using that variable for calling the function as many times as the user wants, increases the code reusability. Below is the syntax for the same:

Syntax:

C++

// Below function is assigned to // the variable fun cout << "inside function"

Program 1: Below is the C++ program to implement a function assigned to a variable:

C++

// C++ program to implement function // assigned to a variable using namespace std; // Driver Code // Below function i.e., is // assigned to the variable fun cout << "Inside Function Variable" ; // Call the function using variable Output
Inside Function Variable

Program 2: Below is the C++ program to implement a parameterized function assigned to a variable:

C++

// C++ program to implement parameterized // function assigned to a variable using namespace std; // Driver Code // Passing i and j as 2 parameters auto fun = [&]( int i, int j) < cout << "Parameterized Function" ; // Call the function using variable

Output:

Parameterized Function

Program 3: Below is the C++ program to implement a function assigned to a variable that returns a value:

C++

// C++ program to implement the function // assigned to a variable returning // some values using namespace std; // Driver Code // Function taking 2 parameters // and returning sum auto sum = [&]( int a, int b) < return a + b; // Call the function using variables cout << "The sum is: " Output
The sum is: 9
Like Article -->

Please Login to comment.

Similar Reads

Difference between Instance Variable and Local Variable

A variable is a name given to a memory location. It is the basic unit of storage in a program. The value stored in a variable can be changed during program execution.A variable is only a name given to a memory location. All the operations are done on the variable effects of a memory location.In Java, all the variables must be declared before use.In

3 min read How to Access Global Variable if there is a Local Variable with Same Name in C/ C++?

Local Variable: The variable whose scope lies inside a function or a block in which they are declared. Global Variable: The variable that exists outside of all functions. It is the variable that is visible from all other scopes. We can access global variable if there is a local variable with same name in C and C++ through Extern and Scope resolutio

2 min read How to Access a Local Variable from a Different Function in C++?

In C++, local variables defined within a function in C++ are only available inside the scope of that particular function. In some situations, we may need to access these local variables in some other function. In this article, we will discuss how to access a local variable from a different function in C++. Access a Local Variable from a Different F

3 min read How variable length argument works?

In this article, we will discuss how the variable-length argument works. Variadic functionCalling conventionMemory layout of C/C++ programNegative subscript Variadic Function: A variadic function are the templates that take a variable-length argument. A variable-length argument is a feature that allows a function to receive any number of arguments.

6 min read Installing MinGW Tools for C/C++ and Changing Environment Variable

MinGW is a native C/C++ compiler(GCC) which have free distributable import libraries and header files for building native Windows applications. In this tutorial, we are going to install MinGW for C/C++. These are a few steps that we have to perform to successfully install MinGW on our device. How to Install the MinGW Tools for C/C++? Step 1: Go to

3 min read Variable Shadowing in C++

Variable shadowing is a common programming concept where a variable declared in some specific scope takes precedence over a variable with the same name declared in an outer scope. In C++, this can happen when you define a new variable within the function or block that has the same name as a variable declared in the outer scope. Both global and loca

3 min read C++ Variable Templates

Over the years C++ has undergone advancements to improve its capabilities and coding efficiency. One notable addition in C++14 is the introduction of variable templates. These variable templates allow you to create a group of variables or static data members that can be customized providing flexibility for your code. Variable TemplatesVariable temp

4 min read Why Variable Length Array were Removed in C++?

While historically a feature in the C language, the variable length array is not part of standard C++. This article discusses the C++'s approach to dynamic memory allocation, showing an alternative strategy employed to achieve a similar outcome. Variable Length ArrayThe Array is said to be a variable length array if the size is defined at the runti

3 min read Cyclic Iterator for K variable length vectors

Given K vectors, the task is to design a cyclic iterator that prints the elements of these vectors in a cyclic manner. For example: v1 = <1, 2, 3>, v2 = and v3 = then the output should be 1, 4, 7, 2, 5, 8, 3, 6 and 9. Examples: Input: v1 = <1, 2>, v2 = , v3 = Output: 1 3 6 2 4 5 Input: v1 = <1, 2>, v2 = Outpu

10 min read Using a variable as format specifier in C

It is known that, printf() function is an inbuilt library function in C programming language in the header file stdio.h. It is used to print a character, string, float, integer etc. onto the output screen. However, while printing the float values, the number of digits following the decimal point can be controlled by the user. One of the ways to do

1 min read Swap Two Numbers Without Third Variable in C++

In C++, swapping two numbers means we need to exchange the value of two numbers. In this article, we will learn how to swap two numbers without using the third variable in C++. Example Input: a=10b=20Output:After swapping:a=20b=10Swap Two Numbers Without Using a Third VariableIn C++ we can swap two numbers without using a temporary variable by usin

2 min read How to Declare a Static Variable in a Class in C++?

In C++, a static variable is initialized only once and exists independently of any class objects so they can be accessed without creating an instance of the class. In this article, we will learn how to declare a static variable in a class in C++. Static Variable in a Class in C++To declare a static variable within a class we can use the static keyw

2 min read Can We Access Local Variable's Memory Outside its Scope?

In C++, local variables are those variables that are declared inside a function or a block. Generally, we cannot access these variables outside its scope using the variable name but there are some exploits that we can use to do that. In this article, we will see how to access a local variable memory outside its function or scope. Access Local Varia

2 min read Representation of Int Variable in Memory in C++

To understand the Representation of int variable in memory in C/C++ below is the basic program to create a variable and its size. Usually, the value of an int variable is stored in a series of bytes that are represented as the variable's representation as memory. Depending on the hardware and compiler being used, an int variable may be represented

5 min read How to Access Private Variable in C++?

In C++, Private members of a class are accessible only within the class they are declared and by friend functions, they are not accessible outside the class not even by derived classes. In this article, we will learn how to access private variables in C++. Accessing Private Variables From Class in C++In C++, we can access private variables through

2 min read How to Declare a Global Variable in C++?

In C++, global variables are like normal variables but are declared outside of all functions and are accessible by all parts of the function. In this article, we will learn how to declare a global variable in C++. Global Variable in C++ We can declare a global variable, by defining it outside of all functions at the top after the header files. Once

2 min read How to Get Environment Variable in C++?

Environment variables are globally accessible named values that store information about the system environment where your code is executed. They are also used to store configuration settings, paths to important directories, and other system-specific data as well. In this article, we will learn how to get environment variables in C++. Accessing Envi

2 min read Variable Length Arrays (VLAs) in C

In C, variable length arrays (VLAs) are also known as runtime-sized or variable-sized arrays. The size of such arrays is defined at run-time. Variably modified types include variable-length arrays and pointers to variable-length arrays. Variably changed types must be declared at either block scope or function prototype scope. Variable length arrays

2 min read Efficient ways to compare a variable with multiple values

In this article, we will discuss the ways to compare a variable with values. Method 1: The idea is to compare each variable individually to all the multiple values at a time. Program 1: [GFGTABS] C++ // C++ program to compare one variable // with multiple variable #include <iostream> using namespace std; // Driver Code int main() < // Variabl

15 min read Iteration Over std::vector: Unsigned vs Signed Index Variable

When we iterate over a std::vector in C++, we need to choose the right type for the index variable to maintain both the correctness and clarity of our code. A common question arises: Should we use an unsigned or signed integer for the index variable? In this article, we will learn the differences between using unsigned and signed index variables, a

4 min read error: call of overloaded ‘function(x)’ is ambiguous | Ambiguity in Function overloading in C++

Pre-requisite: Function Overloading in C++ Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading. Two or more functions are said to be overloaded if they differ in any of th

9 min read How to call some function before main() function in C++?

Since it is known that main() method is the entry point of the program. Hence it is the first method that will get executed by the compiler. But this article explains how to call some function before the main() method gets executed in C++. How to call some function before main() function? To call some function before main() method in C++, Create a

3 min read Difference between user defined function and library function in C/C++

Library Functions These functions are the built-in functions i.e., they are predefined in the library of the C. These are used to perform the most common operations like calculations, updation, etc. Some of the library functions are printf, scanf, sqrt, etc. To use these functions in the program the user has to use a header file associated with the

3 min read Difference between virtual function and inline function in C++

Virtual function: Virtual function is a member function which is declared within a base class and is redefined by a derived class. Inline function: Inline function is a normal function which is defined by the keyword inline, it is a short function which is expanded by the compiler and its arguments are evaluated only once. The syntax of defining th

2 min read Difference Between Friend Function and Virtual Function in C++

A friend class can access private and protected members of other classes in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other classes. Just likely, a friend function is a function that is declared outside the scope of a class. This function can be invoked like a normal function and

3 min read What happens when a virtual function is called inside a non-virtual function in C++

Predict the output of the following simple C++ program without any virtual function. C/C++ Code #include &lt;iostream&gt; using namespace std; class Base < public: void print() < cout &lt;&lt; &quot;Base class print function \n&quot;; >void invoke() < cout &lt;&lt; &quot;Base class invoke function \n&quot;;

2 min read Function Overloading vs Function Overriding in C++

Function Overloading (achieved at compile time) Function Overloading provides multiple definitions of the function by changing signature i.e. changing number of parameters, change datatype of parameters, return type doesn’t play any role. It can be done in base as well as derived class.Example: void area(int a); void area(int a, int b); C/C++ Code

3 min read Function Pointer to Member Function in C++

In C++, function pointers enable users to treat functions as objects. They provide a way to pass functions as arguments to other functions. A function pointer to a member function is a pointer that points to a non-static member function of a class. In this article, we will learn how to use a function pointer to a member function in C++. Function Po

3 min read Difference between Virtual function and Pure virtual function in C++

Virtual Function in C++ A virtual function is a member function which is declared within a base class and is re-defined(Overridden) by a derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the function. Pu

2 min read Function Overloading vs Function Templates in C++

In C++, both function overloading and function templates allow us to create functions that can operate on different types of data. While they might seem similar, they are used for different purposes. In this article, we will learn the differences between function overloading and function templates, and understand when to use each. Table of Content