answersLogoWhite

0

How do you call function in C?

Updated: 8/11/2023
User Avatar

Wiki User

13y ago

Best Answer

Every C function has a name so you can easily call any function provided you know its name. However, every function also has identity (a memory address), thus you can also call any function provided you know its address. This is useful as it allows you to pass functions to other functions. This might seem odd since any function can invoke any other function by its given name, however the point of passing functions to functions is that different callers can pass different functions.

For example, consider the following implementation of the insertion sort algorithm:

void insertion_sort (int* arr, size_t size) {

// insertion sort... size_t gap;

int val;

for (size_t idx=1; idx<size; ++idx) {

gap = idx;

val = arr[gap];

while (0<gap && val<arr[gap-1]) {

arr[gap] = arr[gap-1];

--gap;

}

arr[gap] = val;

}

}

Note the comparison operation within the while loop (val<arr[gap-1]) means that this function will always sort the array in ascending order of value. We could include a bool flag in the arguments to be able to choose between ascending and descending order, but this makes our code less than efficient because we now have to repeatedly test the argument in the inner while loop:

void insertion_sort (int* arr, size_t size, bool ascend=true) {

// insertion sort... size_t gap;

int val;

for (size_t idx=1; idx<size; ++idx) {

gap = idx;

val = arr[gap];

while (0<gap && (ascend ? val<arr[gap-1]:val>arr[gap-1])) { <-- INEFFICIENT!

arr[gap] = arr[gap-1];

--gap;

}

arr[gap] = val;

}

}

A better approach is to pass the comparison operator itself. This is achieved by declaring the operator as a function:

bool less_than (int a, int b) { return a<b; }

bool greater_than (int a, int b) {return a>b; }

Note that both functions have the same prototype, they only differ by name. That is, they both accept two integer arguments and they both return a bool. Functions that return a bool are commonly known as predicates.

To create a function pointer we use the following declaration:

bool (*FuncPtr)(int, int);

This declares a function pointer named *FuncPtr that can refer to any function that accepts two integers and returns a bool. Note the function pointer name includes the asterisk. Note also that the parenthesis are required. Without them, the asterisk would bind to the return type instead of the function pointer:

bool *FuncPtr (int, int);

In other words we end up declaring a function named FuncPtr that returns a pointer to bool, which is clearly not what we intended.

We typically use a typedef to avoid the (ugly!) syntax associated with function pointers:

typedef bool (*FuncPtr)(int, int);

Now we have a proper type name (FuncPtr) that we can use in our declarations.

We can now change our sorting function to accept and call the predicate:

void sort (int* arr, size_t size, FuncPtr pred) {

// insertion sort...

size_t gap;

int val;

for (size_t idx=1; idx<size; ++idx) {

gap = idx;

val = arr[gap];

while (0<gap && pred(val, arr[gap-1]) {

arr[gap] = arr[gap-1];

--gap;

}

arr[gap] = val;

}

}

The sorting algorithm can now be invoked as follows:

void f (int* arr, size_t size, bool ascend=true) {

if (ascend) {

sort (arr, size, less_than); // sorts the array in ascending order

} else {

sort (arr, size, greater_than); // sorts the array in descending order

}

}

Or more concisely:

void f (int* arr, size_t size, bool ascend=true) {

sort (arr, size, ascend ? less_than : greater_than);

}

Note that the bool flag is now tested outside of the algorithm and is therefore tested once per call, rather than multiple times within the algorithm itself.

User Avatar

Wiki User

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

Wiki User

13y ago

a function is called by writing its name followed by ();

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Nothing, you misread something. In C, when you call a function, parameters passed by-value.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

In order to use the function we need to involve it at a required place in program.this is known as function call

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you call function in C?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Call by function in c language?

I guess you meant the following:'In C language, when you call a function,the parameters are passed by-value.'


Which keyword is used to make function call faster in c?

Inline Function


A c plus plus statement that invokes a function is known as?

...a function call.


What is difference function prototype and function define in turbo c?

In C, a function prototype is a declaration to the compiler that a certain function exists, without a full definition. This allows other functions to call that function. The linker will later make sure that a function definition actually exists (perhaps somewhere else), and will turn the call to the function to a call to the correct function.


How do you make a call to a function in C?

Example:puts ("I've just called function 'puts'");


How you write a c program for making a call in gcc?

int main() { // Call the printf function printf("This is a function call!\n"); return 0; }


What do you call an object function in c plus plus?

method


How can you call a function written in FORTRAN from a C program?

Is the FORTRAN function part of a library. If it is it will be no different from call a c function in a library. There could be an issue the order that the functions attribures are pusshed onto the stack like there is with pascal.


What is the use of function in c?

using function we can call the function in the program any where. by using functions we can reduce the code redundancy


What function that call themselves are called to c programme?

Functions in C language may call themselves (ie can be recursive) without restrictions.


How do you add two numbers with out using operator in c language?

Not possible. Of course you can call a function which does the addition for you, but function-calling is also an operator in C.


Which operator require to call c function using object name?

The operator required to call c function using object name is function object. Other operator names that deal with objects are structure dereference, structure reference, and indirection