QUESTION 01 · Functions
What is a user-defined function?
2 marksAnswer
A user-defined function is a named block of code written by the programmer to perform a task.
- It can receive values through parameters.
- It can return a result to its caller.
QUESTION 01 · Functions
A user-defined function is a named block of code written by the programmer to perform a task.
QUESTION 02 · Functions
A function groups statements that perform a particular task. A program can call the function whenever that task is needed.
The example defines add to receive two integers and return their sum. main calls add(4, 6). The function receives the values in a and b, calculates 10 and returns it. printf then displays the returned result. The function definition appears before main, so it also provides the required declaration before the call.
Keep each function focused on a clear task. Its parameter and return types form its interface: they describe how values enter and leave it. A function is not executed merely because it is defined; a call is needed. For the fixed integer addition shown here, time and extra working space are both O(1).
/* CodeBhavya | PPS AR25 | Learning example */
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int main(void)
{
printf("Sum = %d\n", add(4, 6));
return 0;
}This example uses values already written in the program.
Sum = 10Related questions in the function declarations category from the supplied papers. The marks below are the printed allocations, including any combined subquestion.
Define function signature.
Paper P05 · page 1What is the signature of a function?
Paper P06 · page 1