- 2070 Chaitra · 4 marks
Function definition; sum of two numbers
Short answer
A function definition is the complete code of a function with header and body. A program to sum two numbers reads two integers, calls a user-defined function int sum(int a, int b) { return a + b; }, and prints the result. For input 12 and 30, output is Sum = 42.
Define "function definition" and write the program to find the sum of two numbers using user-defined functions.
A function definition is the complete code of a function: the header (return type, name, parameters) followed by the body in braces which performs the task. A function must be defined once, and it can be called many times.
#include <stdio.h>
int sum(int a, int b);
int main(void)
{
int x, y;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
printf("Sum = %d\n", sum(x, y));
return 0;
}
int sum(int a, int b)
{
return a + b;
}
Output for the input 12 and 30:
Enter two numbers: Sum = 42
The prototype int sum(int a, int b); tells the compiler about the function; sum(x, y) calls it with the actual parameters x and y; the definition adds the formal parameters a and b and returns a + b.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Significance of user defined functions · Computer Programming, chapter 6
- Switch versus user defined function · Computer Programming, chapter 5
- Complex number sum via functions · Computer Programming, chapter 8
- GOTO types; sum of series program · Computer Programming, chapter 11
- Sum of two matrices in FORTRAN · Computer Programming, chapter 11
- Declaration vs definition; C program structure · Computer Programming, chapter 3
- C program: sum of two 8-bit BCD numbers · Embedded System, chapter 9
- Convert user-defined type to basic type · Object Oriented Programming, chapter 5
- LandMeasure class and sum function · Object Oriented Programming, chapter 4
- 8086 ALP: sum of 100 even numbers · Microprocessor, chapter 3