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++ variable arguments function

template<class... Args>
void print(Args... args)      {(cout << ... << args ) <<endl;}
Comment

c++ variable arguments function

template <typename T>
void db(T t)
{
    cout << t <<" ";
}

template<typename T, typename... Args>
void db(T t, Args... args)      {  cout << t <<" ";db(args...); }
int main() {
     db(1,"a",20,"Hey);
}
Comment

c++ function parameters

void myFunction(string fname) {
  cout << fname << " Refsnes
";
}

int main() {
  myFunction("Liam");
  myFunction("Jenny");
  myFunction("Anja");
  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

C++ Function Parameters

void myFunction(string fname) {
  cout << fname << " Refsnes
";
}

int main() {
  myFunction("Liam");
  myFunction("Jenny");
  myFunction("Anja");
  return 0;
}

// Liam Refsnes
// Jenny Refsnes
// Anja Refsnes
Comment

PREVIOUS NEXT
Code Example
Cpp :: how to sort array in c++ stockoverflow 
Cpp :: print in c ++ 
Cpp :: stack class implementation to file unix-style in c++ 
Cpp :: C++ Pi 4 Decimal 
Cpp :: sfml keyboard events cpp 
Cpp :: c++ loop trhought object 
Cpp :: c++ fill two dimensional array 
Cpp :: C++ fibo 
Cpp :: how to access a vector member by its index 
Cpp :: constrain function in arduino 
Cpp :: vector<intv[] 
Cpp :: c++ unittest in ros 
Cpp :: char array declaration c++ 
Cpp :: three way comparison operator c++ 
Cpp :: remove something from stringstream 
Cpp :: c++ call by value 
Cpp :: c++ linked list delete node 
Cpp :: initialize a vector with same values 
Cpp :: size of unordered_set 
Cpp :: compare values within within a vector c++ 
Cpp :: queue operations c++ 
Cpp :: How to generate all the possible subsets of a set ? 
Cpp :: know what the input data is whether integer or not 
Cpp :: c++ structs 
Cpp :: c++ virtual function 
Cpp :: cpp algorithm iota 
Cpp :: c++ string replace 
Cpp :: c++ friend keyword 
Cpp :: c shortest path dijkstra 
Cpp :: turbo c++ easy programs 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =