A user-defined function can return a value after performing a comparison. In this program, the smallest() function accepts three numbers as parameters and returns the smallest number using the if-else if statement. The returned value is stored in a variable and displayed using printf().
How It Works?
smallest(int a, int b, int c) that accepts three integer values.s to store the smallest number.if-else if statement.s.s using the return statement.main() function, call the smallest() function with different values.#include <stdio.h> #include <conio.h> int smallest(int a, int b, int c) { int s; if(a < b && a < c) { s = a; } else if(b < a && b < c) { s = b; } else { s = c; } return s; } int main() { int a, b; clrscr(); a = smallest(3, 6, 9); printf("\n %d", a); b = smallest(15, 16, 19); printf("\n %d", b); getch(); }
smallest() that accepts three numbers and returns the smallest number. Call the function twice with different values and display the returned result.if-else if statement to find the smallest of three numbers.smallest(int a, int b, int c). Compare the three numbers using if-else if, return the smallest number, store the returned value in a variable, and display it using printf().
#include<stdio.h>
#include<conio.h>
int smallest(int a, int b, int c)
{
int s;
if(a < b && a < c)
{
s = a;
}
else if(b < a && b < c)
{
s = b;
}
else
{
s = c;
}
return s;
}
int main()
{
int a, b;
clrscr();
a = smallest(12, 8, 20);
printf("\n Smallest = %d", a);
b = smallest(35, 18, 25);
printf("\n Smallest = %d", b);
getch();
}
smallest() that compares three numbers and returns the smallest value. The function is called twice from the main() function with different sets of numbers. The returned values are stored in variables and displayed using printf(), demonstrating how functions can return values to the calling function.