Find the biggest of two numbers using function and ternary operator.
Basics of functions:
A function is a sub program to perform a specific task.
OR
a group of instructions to perform specific task.
Along with main() it is possible to define our own functions by following these steps:
1. Function prototype or Function declaration.
2. Function call.
3. Function definition.
Function Prototype:
Giving information about the function such as return type, function name and arguments type to the compiler is known as function prototype; It is written at the declaration section.
Syntax:
< return_type > < function_name >( arguments_type ); |
Example:
int findsum(int a, int b);
float findsum(int a, int b);
void findsum(int a, int b);
int findsum(int a[], int size);
int findsum(int, int);
Function Definition:
Writing the actual code of the function in a block. It is at this stage the task of the function is defined. It is written after the main function.
Syntax:
< return_type >< function_name >(parameters) { } |
Example:
int findsum(int a, int b) { int sum; sum = a + b; return(sum); } |
Function Call:
It is a technique used to invoke a function.
Syntax:
[variable] < function_name >([arguments]); |
Example:
res = findsum(10, 20);
res = findsum(x, y);
res = findsum();
Full Source Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include< iostream.h > #include< conio.h > void main() { int Big(int x,int y); // Prototype int a, b; clrscr(); cout< <"Enter 2 numbers\n"; cin>>a>>b; int res = Big(a, b); // Function call cout< <"Biggest = "<<res; getch(); } int Big(int x, int y) // Function Definition { return( x>y?x:y ); } |
You must also watch these videos, before continuing with this program:
Find Biggest of 2 Numbers: C++
Biggest of Two Numbers Using Ternary Operator: C++
Video Tutorial: Biggest of 2 Numbers Using Function
[youtube https://www.youtube.com/watch?v=mZOaAXL4t9c]
Output:
Enter 2 numbers
420
305
Biggest = 420