Perhaps someone can show me how to fix the following example code so either the function outputx() or outputy() is passed to the function print_it().

void do_it(); // prototype
void outputx(); // prototype
void outputy(); // prototype
void print_it( print_character() ); // prototype

void outputx()
{
printf("x\n")
}

void outputy()
{
printf("y\n")
}

void print_it( print_character() )
{
//effectively becomes either outputx(); or outputy();
print_character();
}

void do_it()
{
// pass the function outputx() to print_it()
print_it( outputx() );

// pass the function outputy() to print_it()
print_it( outputy() );
}

Regards, MaaleaRogers

#include <stdio.h>

void outputx();
void outputy();
void print_it( void(*)() ); // prototype

void outputx()
{
printf("x\n");
}

void outputy()
{
printf("y\n");
}

void print_it( void(*func)() )
{
//effectively becomes either outputx(); or outputy();
	func();
}

int main()
{
// pass the function outputx() to print_it()
print_it( outputx );

// pass the function outputy() to print_it()
print_it( outputy );
}
#include <iostream>


void Function1() { std::cout << "Function1\n"; }
void Function2(void aFunction()) { aFunction(); } 
// make a param that describes the function the will be given
// as a param (return type, identifier, paramtypes)

int main()
{
	Function1();
	std::cout << "\n";
	Function2(Function1); 
	// Function1 is a legal param, it has the same returntype and params as the param in
	// function declaration
	return 0;
}
#include <stdio.h>

void outputx();
void outputy();
void print_it( void(*)() ); // prototype

void outputx()
{
printf("x\n");
}

void outputy()
{
printf("y\n");
}

void print_it( void(*func)() )
{
//effectively becomes either outputx(); or outputy();
	func();
}

int main()
{
// pass the function outputx() to print_it()
print_it( outputx );

// pass the function outputy() to print_it()
print_it( outputy );
}

Thank you, seems like I never get the syntax right.


Regards, MaaleaRogers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.