Search
 
SCRIPT & CODE EXAMPLE
 

CPP

how to pass function as a parameter in c++

#include <bits/stdc++.h> 
using namespace std;

int add(int x, int y){return x+y;}
int sub(int x, int y){return x-y;}
int operation (int x, int y,int (*function)(int,int)){return function(x,y);}
int operation2(int x, int y,std::function<int(int, int)> function){return function(x,y);}

int main()
{
    std::cout <<"Values 1 & 3. Pointer function: Add:"<<operation (1,3,&add)<<" Sub:"<<operation (1,3,&sub) << std::endl;
    std::cout <<"Values 1 & 3. std::function   : Add:"<<operation2(1,3,&add)<<" Sub:"<<operation2(1,3,&sub) << std::endl;
}
Comment

take a function as an argument in c++

// C++ program to pass function as a
// pointer to any function
  
#include <iostream>
using namespace std;
  
// Function that add two numbers
int add(int x, int y)
{
    return x + y;
}
  
// Function that multiplies two
// numbers
int multiply(int x, int y)
{
    return x * y;
}
  
// Function that takes a pointer
// to a function
int invoke(int x, int y,
           int (*func)(int, int))
{
    return func(x, y);
}
  
// Driver Code
int main()
{
    // Pass pointers to add & multiply
    // function as required
    cout << "Addition of 20 and 10 is ";
    cout << invoke(20, 10, &add)
         << '
';
  
    cout << "Multiplication of 20"
         << " and 10 is ";
    cout << invoke(20, 10, &multiply)
         << '
';
  
    return 0;
}
Comment

c++ pass function as argument

class MyClass
{
    public:
        MyClass();

        // Note: No longer marked `static`, and only takes the actual argument
        void Callback(int x);
    private:
        int private_x;
};

MyClass::MyClass()
{
    using namespace std::placeholders; // for `_1`

    private_x = 5;
    handler->addHandler(std::bind(&MyClass::Callback, this, _1));
}

void MyClass::Callback(int x)
{
    // No longer needs an explicit `instance` argument,
    // as `this` is set up properly
    cout << x + private_x << endl;
}
Comment

C++ Function with Parameters

// program to print a text

#include <iostream>
using namespace std;
// display a number
void displayNum(int n1, float n2) {
    cout << "The int number is " << n1;
    cout << "The double number is " << n2;
}
int main() {
     
     int num1 = 5;
     double num2 = 5.5;

    // calling the function
    displayNum(num1, num2);

    return 0;
}
Comment

C++ Function with Parameters

// program to print a text

#include <iostream>
using namespace std;
// display a number
void displayNum(int n1, float n2) {
    cout << "The int number is " << n1;
    cout << "The double number is " << n2;
}
int main() {
     
     int num1 = 5;
     double num2 = 5.5;

    // calling the function
    displayNum(num1, num2);

    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: print each number of digit c++ 
Cpp :: c++ max of array 
Cpp :: how to remove an element from a vector by value c++ 
Cpp :: c++ simple projects 
Cpp :: sieve of eratosthenes algorithm in c++ 
Cpp :: check uppercase c++ 
Cpp :: how to iterate throguh a string in c++ 
Cpp :: get window position 
Cpp :: c++ first letter of string 
Cpp :: c++ for else 
Cpp :: c++ vector extend vector 
Cpp :: coordinate in 1d array c++ 
Cpp :: comparator for priority queue c++ 
Cpp :: how to get the type of a variable in c++ 
Cpp :: string substr c++ 
Cpp :: run cmd command c++ 
Cpp :: sort vector in reverse order c++ 
Cpp :: int to hexadecimal in c++ 
Cpp :: c++ encapsulation 
Cpp :: how to check a number in string 
Cpp :: initialize dynamic array c++ to 0 
Cpp :: c++ hashmaps 
Cpp :: how to know datatype of something in c++ 
Cpp :: float to int c++ 
Cpp :: c++ string split 
Cpp :: how to get hcf of two number in c++ 
Cpp :: double to float c++ 
Cpp :: binary search in c++ 
Cpp :: stl c++ 
Cpp :: hashmap c++ 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =