answersLogoWhite

0


Best Answer

== == Let me correct the Q. Strictly speaking, You never pass a function name to another function, you actually pass function address as argument to another function. However, since the function name automatically resolves into function address, it could be deemed correct to say that you pass function name.

Now, the answer:

If you're talking about function pointers:

void Foo(double (*fptr)(int), int x, int y);

double Bar(int i);

int main()

{

Foo(&Bar, 1, 2);

/*Previous Line passes Bar's address to Foo. The & behind Bar is optional because it's implicit, but I put it there to emphasize that it's the address of Bar being passed.*/

return 0;

}

void Foo(double (*fptr)(int), int x, int y)

{

(*fptr)(10);

/*Previous line will call Bar(10) because main passed it Bar's address. Explicitly declaring the dereference is not required, however I explicitly wrote it in to emphasize that it is a pointer that is getting dereferenced.*/

//do something

}

double Bar(int i)

{

//do something

return 0;

}

Note, the function pointer must have the same parameter list as the function you are trying to set the function pointer equal to.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

By using function pointers we can pass function name as parameter to function.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Yes, qsort in stdlib.h is an example.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can you pass a function name as argument of another function?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you pass enum object as argument to function in c?

You can't pass an enum as an argument to a function. An enum in C isn't an object, it's a type. All you can do is pass a variable that is of the particular enum's type.


Can you pass object as an argument to a method?

if you have a function or a method that takes Object as a parameter, you can call that function or method and pass an Object as follows: Let's say you have a class that extends Object and a function as follows public class A extends Object { ..... } void function ( A arg ){ ..... } To call this function, you need an instance of the class A A objectInstance = new A(); function(objectInstance); The next line shows how to pass an instance of the class A to the function.


In python are actual and formal parameters matched up by position or name?

When we invoke a function, we pass the actual arguments in the same order specified by the function's formal arguments, thus it is the relative position that determines how they are matched. Note that actual parameter names are within the scope of the calling code while formal parameter names are scoped to the function in which they are declared. The calling code has no access to the formal argument names, and the function may or may not have access to the actual argument names. Python uses the pass-by-object paradigm: if the object being passed is immutable, then it is passed by value (the formal parameter is assigned a copy of the object's value), otherwise it is passed by reference (in which case the formal argument becomes an alternative name for the actual argument).


Can a variable name can be passed to a value parameter in c plus plus?

No. Pass by value always receives a copy of the value being passed. Even if it were possible to physically pass a user-defined identifier into a function by value, the compiled code would not recognise the name since all identifiers are stripped out by the compiler and replaced with memory addresses. Strictly speaking, even pass by reference does not pass the variable name, as the function argument is simply an alias, an alternate but informal name, for the formal name you actually pass. In essence you are passing the memory address of the variable, rather the value of the memory address as you would with pass by value.


How is an array name interpretedwhen it is passed to a function?

An array is still an array, regardless of how you pass it into functions. It is still an array-type variable at the beginning of a function. However, the function itself may manipulate the array by "imploding" it into a string with a delimiter, using array values with only specific keys, and such.

Related questions

Why it is not possible to pass a function as an argument to another function in c?

It is quite possible. A well-known example is the fourth parameter of qsort.


Why you pass arguments to function in c plus plus?

You pass arguments to functions because that is how you tell the function what you want it to do. If you had, for instance, a function that calculated the square root of something, you would pass that something as an argument, such as a = sqrt (b). In this case sqrt is the function name, b is passed as its argument, and the return value is assigned to a.


How do you pass enum object as argument to function in c?

You can't pass an enum as an argument to a function. An enum in C isn't an object, it's a type. All you can do is pass a variable that is of the particular enum's type.


Can you pass address of the structure member as a function argument?

Yes.


Another name for an instep pass is a?

Another name for instep pass is cross pass


Can you pass object as an argument to a method?

if you have a function or a method that takes Object as a parameter, you can call that function or method and pass an Object as follows: Let's say you have a class that extends Object and a function as follows public class A extends Object { ..... } void function ( A arg ){ ..... } To call this function, you need an instance of the class A A objectInstance = new A(); function(objectInstance); The next line shows how to pass an instance of the class A to the function.


What is the advantages of call by reference?

Whenever you pass a value to a function, that value must be copied. If the value is large or complex, this can hinder performance, particularly when the function does not need to alter the value. To improve performance, we use the pass by reference semantic. Rather than passing the value itself, we pass the address of the value. That is, the address is copied, not the value. The function can then refer to the value by dereferencing the address. Ideally, pass by reference should only be used when the function does not need to alter the value. This is achieved by declaring the function's formal argument a pointer to constant type. This makes it clear to the caller the value of the actual argument will not be altered by the function. In some cases, we do require the function to alter the value. In these cases the argument is regarded as being an output parameter because it allows the function to return another value besides the return value. Typically, the caller will allocate some memory for the function to use (perhaps initialising it with a value), and then pass the address of that memory to the function. The function's formal argument is therefore declared a pointer to non-constant type, making it clear to the caller that the function will modify the value being pointed.


What does the warning Illegal Argument Exception mean in Java language?

The warning Illegal Argument Exception in Java means that one has attempted to pass a wrong type of argument for a function. For example, we have a function that calculates a sum of two numbers and feed it a text string, which results in Illegal Argument Exception.


Can you pass addresses to a function in C plus plus?

If the identifier you want to pass is an ordinary identifier, pass it as the address of... function(&identifier); If the identifier you want to pass is an array identifier, pass its name... function(arrayname);


In python are actual and formal parameters matched up by position or name?

When we invoke a function, we pass the actual arguments in the same order specified by the function's formal arguments, thus it is the relative position that determines how they are matched. Note that actual parameter names are within the scope of the calling code while formal parameter names are scoped to the function in which they are declared. The calling code has no access to the formal argument names, and the function may or may not have access to the actual argument names. Python uses the pass-by-object paradigm: if the object being passed is immutable, then it is passed by value (the formal parameter is assigned a copy of the object's value), otherwise it is passed by reference (in which case the formal argument becomes an alternative name for the actual argument).


Can a variable name can be passed to a value parameter in c plus plus?

No. Pass by value always receives a copy of the value being passed. Even if it were possible to physically pass a user-defined identifier into a function by value, the compiled code would not recognise the name since all identifiers are stripped out by the compiler and replaced with memory addresses. Strictly speaking, even pass by reference does not pass the variable name, as the function argument is simply an alias, an alternate but informal name, for the formal name you actually pass. In essence you are passing the memory address of the variable, rather the value of the memory address as you would with pass by value.


How is an array name interpretedwhen it is passed to a function?

An array is still an array, regardless of how you pass it into functions. It is still an array-type variable at the beginning of a function. However, the function itself may manipulate the array by "imploding" it into a string with a delimiter, using array values with only specific keys, and such.