C Program to Find Maximum Between Two Numbers using Function
This program finds the maximum of two numbers using a user-defined function in C. It demonstrates how to declare, define, and call a function with return values.
Concept Overview
A function in C allows code reusability and better program organization. In this program, the max() function takes two integer arguments and returns the greater of the two.
Program
C
#include <stdio.h>
// Function to find maximum of two numbers
int max(int a, int b) {
return (a > b) ? a : b;
}
int main() {
int num1 = 10, num2 = 20;
int maximum;
// Function call
maximum = max(num1, num2);
printf("Max value is : %d\n", maximum);
return 0;
}
Output
Max value is : 20
Explanation
- The function
max()takes two integer parameters and returns the greater value. - In
main(), two integersaandbare declared and passed to themax()function. - The returned result is stored in
retand displayed usingprintf(). - This program demonstrates function declaration, definition, and calling.
Note: You can modify the program to take user input instead of using predefined values.