A function can also receive values from the main() function through parameters. In this program, the user-defined function add() accepts two integer values, adds them, and displays the result. The same function is called multiple times with different values, which avoids writing the addition logic repeatedly.
How It Works?
stdio.h and conio.h.add(int a, int b) that accepts two numbers.k.printf() function.main() function, call add() several times with different values.getch().#include <stdio.h> #include <conio.h> void add(int a, int b) { int k; k = a + b; printf("\n Total is %d", k); } int main() { clrscr(); add(20, 10); add(30, 20); add(40, 30); add(50, 40); add(60, 50); getch(); }
function named add() to add two numbers. Call the function five times with different values and display the sum each time.add(int a, int b). Inside the function, add the two numbers and print the result. Call the function multiple times from main() using different values.
#include <stdio.h> #include <conio.h> void add(int a, int b) { int total; total = a + b; printf("\n Total is %d", total); } int main() { clrscr(); add(5, 10); add(12, 18); add(20, 25); add(30, 40); add(50, 60); getch(); }
add() that accepts two integer values as parameters. Whenever the function is called, it adds the two numbers and displays the result. The same function is reused five times with different arguments, demonstrating the advantage of functions in reducing code repetition.