answersLogoWhite

0


Best Answer

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

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why it is not possible to pass a function as an argument to another function in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

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.


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.


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.


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 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.


How can you pass a function name as argument of another function?

== == 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.


Does C even have pass by reference?

Strictly speaking, no. All arguments in C are passed by value. However, when the argument being passed is a memory address, although the address itself is passed by value, we're effectively passing the object that resides at that address -- by reference. Thus when a function's formal argument is a pointer variable (of any type), then it can be taken as read that the function is using the pass by reference semantic rather than the pass by value semantic. Nevertheless, it is important to keep in mind that the formal argument is assigned a copy of the actual argument and is therefore being passed by value.


What is difference between call by value and pass by value?

When you pass by value, the function's parameter is assigned the value of the argument that was passed. When you pass by reference, the function's reference parameter is assigned the address of the argument. In other words, pass by value copies the value, pass by reference passes the variable itself.Pass by reference is always the preferred method of passing arguments to functions when those arguments are complex objects. If the argument is a primitive data type then the cost in copying the value is minimal, but copying a complex object is expensive in terms of performance and memory consumption. If the function parameter is declare constant, you can be assured the object's immutable members will not be affected by the function. If it is non-constant and you do not wish your object to be altered, you can either copy the object and pass the copy, or pass the object by value if the function is overloaded to cater for this eventuality (good code will provide both options).


What is the difference between parameters and arguments in VB?

In programming languages, a parameter and an argument are the same thing; there is no actual difference between the two. Although a few languages do differentiate between an actual argument and a formal argument by calling one a parameter and the other an argument (or vice versa), the terms are universally interchangeable. That is; there is no single definition that applies to any one language, including Visual Basic. The language may have a convention, but there's no reason to follow that convention. Personally, I prefer the term argument and use the terms formal argument and actual argument whenever I need to specifically differentiate one from the other. In this way I can refer to them in a consistent but language-agnostic manner. Only a Pedant would argue that the terms parameter and argument have a specific meaning to a specific language, even when the creators of that language use the terms interchangeably themselves. To clarify, an actual argument is the argument being passed to a function while a formal argument is the argument that is used by the function. The two will always have the same value, but they are not the same argument. For instance, consider the following function definition: f (int a) { print a*2 } Whether we regard 'a' as being a parameter or an argument is immaterial -- it is a formal argument or formal parameter, whichever you prefer. The meaning is clarified by using the word "formal". Now consider the calling code: b = 42 f (b) Here, b is the actual argument (or actual parameter) being passed to the function f. Note that a and b are not the same variable or reference. That alone means there is no reason to differentiate them; the meaning of argument or parameter is implied by the context alone. It doesn't matter whether the function uses pass by value or pass by reference semantics. When passing arguments by value, a is simply a copy of b (independent variables with the same value). When passing by reference, a refers to the same memory address as b (a is an alias for b). In either case, the function uses the formal argument named a while the calling code uses the actual argument named b. In other words, the names are only accessible from within the scope in which they are declared, even if they refer to the same memory address. Of course, a function may pass one of its formal arguments to another function. Thus with respect to the calling function, its formal argument becomes an actual argument to the function being called.


What are the advantages of pass by reference as compared to pass by value?

Call by reference, particularly when applied to objects, because call by value automatically invokes an object's copy constructor, which is seldom desirable when passing objects into functions.


What is pass by value in c functions?

Pass by value is a semantic that describes the way in which the actual argument in the calling code is assigned to the corresponding formal argument of the function being called. In pass by value, the actual argument and the corresponding formal argument are independent of each other; changing the value of the formal argument has no effect whatsoever upon the actual argument's value. In other words, the function receives a copy of the actual argument's value, never the actual argument itself. In C, all arguments are passed by value. However, when the formal argument is declared a pointer, we are effectively declaring a pass by reference semantic. The formal and actual arguments are still independent of each other (the formal argument's pointer value can still be changed without affecting the actual argument's pointer value), but if the formal argument and the actual argument both refer to the same object, changing that object's value via the function changes the same value in the caller. Pass by reference is useful when the value being passed is large or complex and cannot be implicitly or easily copied, such as an array or data structure. Pass by reference can also be used to return a value to the caller via an "output parameter", thus allowing a function to return more than one value. [Object-oriented programmers will detest the use of output parameters, citing bad programming practice (or poor style), but in C, it is not only desirable to use them to maintain efficiency, it is largely unavoidable. Object-oriented languages have highly efficient move constructors and move assignment operators, but C does not and copying large structures unnecessarily is detrimental to performance.]