2 answers
Best answer
Answer: a function is a self-contained, named block of code that performs a specific task. It runs only when it is called, can take inputs (arguments) and can return a value.
Every C program has at least one function, main(). Functions are of two kinds: library functions (like printf(), sqrt()) and user-defined functions.
int add(int a, int b) /* function definition */
{
return a + b;
}
int main()
{
int s = add(5, 3); /* function call, s = 8 */
printf("%d", s);
return 0;
}
A user-defined function has three parts: the declaration (prototype, e.g. int add(int, int);), the definition (the body) and the call.
Advantages: the same code can be reused many times, the program is divided into small manageable modules, and it is easier to read, test and debug.
Your answer
Log in or create a free account to answer this question.