Call by name example in c
C language does not support call by name. Call by name is an evaluation technique in which the arguments to a function are not evaluated before the function is called; instead, they are substituted (using capture-avoiding substitution) straight into the function body and then allowed to be evaluated at each instance they occur within the function.
While call by name is supported in certain other languages, such as Scala or Algol, it is not directly supported in C. But, function pointers and function names passed as arguments can be used to accomplish comparable behaviour.
This short example shows how to use function pointers in C to implement a call by name method:
#include <stdio.h>
void displayInt(int p) {
printf("The given value is %d\n", p);
}
void displayFloat(float xy) {
printf("The given value is %f\n", xy);
}
void callByName(void (*function)(int), int number) {
printf("Being called by name\n");
function(number);
}
int main() {
int valueOfInt = 32;
float valueOfFloat = 5.34;
callByName(displayInt, valueOfInt);
callByName((void (*)(int))displayFloat, (int)valueOfFloat);
return 0;
}
The callByName function in this example accepts an integer argument in addition to a function pointer (void (*function)(int)). To accomplish a call by name, you can supply various functions to callByName. Keep in mind that the function pointer's type must correspond with the functions whose signatures you plan to pass.
Output:
What are these function pointers?
Pointers to functions are supported in C, much like regular data pointers (int *, char *, etc.). Here is a basic example of using a function pointer for both function declaration and call.
#include <stdio.h>
void function(int x)
{
printf("x is equal to %d\n", x);
}
int main()
{
void (*function_ptr)(int) = &function;
(*function_ptr)(13);
return 0;
}
Output;
This method gives you a useful workaround in C, enabling you to dynamically select and call functions based on runtime conditions, even though it doesn't exactly mimic call by name. A frequent practice in C programming is to achieve flexibility and dynamic behavior in function calls by using function pointers.
Why is an additional bracket required around function pointers, such as fun_ptr in the example above?
The expression "void (*function_ptr)(int)" becomes "void *function_ptr(int)" if the bracket is removed, designating a function that returns a void pointer.
Why do we use function pointers for calling by name in c
Function pointers are a workaround used in C programming languages to accomplish dynamic dispatch or "call by name" functionality. C is a statically typed language, meaning that functions are usually resolved at build time, in contrast to some other languages that directly provide call-by-name or dynamic dispatch. But by choosing and calling functions at runtime, you can create a certain amount of dynamic behavior by using function pointers.
Function pointers are employed in C for a type of "call by name" for the following reasons:
Dynamic Scheduling:
Function pointers provide you the ability to select a runtime function depending on parameters or user input.
This gives you the ability to accomplish polymorphism or dynamic dispatch in C, which is akin to what is possible in languages with more complex type systems.
Adaptability
Function pointers offer dynamic function selection flexibility, which can be helpful in situations where the function to utilize depends on runtime parameters.
Mechanisms for Callbacks:
Function pointers are frequently used to build callback systems, in which a function is passed as an input to another function, which then calls the first function later.
Adjustability:
Function pointers allow you to write extensible and modular programs, with many modules or components implementing distinct functionalities.
Why C language does not support call by name?
Because of its design principles, which include static typing, efficiency considerations, and a dedication to predictability, C does not support call by name. Programmers can use function pointers to achieve comparable dynamic behaviour even when call by name is not explicitly supported. By being aware of these design decisions and substitute mechanisms, developers can write more effective and reliable C code that takes advantage of the language's advantages while resolving its drawbacks.
Remember that, unlike other programming languages, this is merely a workaround and not a direct implementation of call by name. Because C is a statically typed language, type safety depends on matching function signatures.