Search
 
SCRIPT & CODE EXAMPLE
 

CPP

c++ function as param

int do_sth(int a, int b, function<int(int, int)> binop) { // C++ - Like
    return binop(a, b);
}
Comment

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

c++ function as paramter

// function ask for void function with int param
void func ( void (*f)(int) );

// void function with int param :)
void print ( int x ) {
  printf("%d
", x);
}

// pass print function to func 
func( print )
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++ 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 :: c++ environment setup 
Cpp :: read from standard input cpp 
Cpp :: flags of open operation c++ 
Cpp :: sinonimo de tratar 
Cpp :: create a bitset of 1024 bits, 
Cpp :: how to shorten code using using c++ in class with typename 
C :: C output color font 
C :: pointer to a structure in c 
C :: calculate distance between 2 points X Y axis 
C :: conio.h linux 
C :: c string is int 
C :: octave square each element matrix 
C :: scanf ignore new line 
C :: vowel or consonant in c 
C :: how to open jupyter notebook in local disk D 
C :: remove element from np array 
C :: factorial in c using recursion 
C :: nested loop in c 
C :: c number to string 
C :: armstrong number in c 
C :: typedef pointer 
C :: go optional parameters 
C :: limit axis in one direction plt 
C :: what is string::npos 
C :: epoch time in c 
C :: c functions example 
C :: fgets c 
C :: highest common factor algorithm in c 
C :: c break statement 
C :: c program to implement mv command 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =