printf("Biggest of %d, %d and %d is %d\n", a, b, c, biggest(a, b, c));
return 0;
}
// function definition
int biggest(int x, int y, int z)
{
if(x > y && x > z)
{
return x;
}
else
{
if(y > z)
return y;
else
return z;
}
}
#include<stdio.h>
int biggest(int, int, int); // function prototype
int main()
{
int a, b, c;
printf("Enter 3 integer numbers\n");
scanf("%d%d%d", &a, &b, &c);
//function call biggest(a, b, c)
printf("Biggest of %d, %d and %d is %d\n", a, b, c, biggest(a, b, c));
return 0;
}
// function definition
int biggest(int x, int y, int z)
{
if(x > y && x > z)
{
return x;
}
else
{
if(y > z)
return y;
else
return z;
}
}
Output Enter 3 integer numbers 50 40 60 Biggest of 50, 40 and 60 is 60
Source Code: C Program To Find Biggest of Three Numbers using Function, Using Ternary Operator
printf("Biggest of %d, %d and %d is %d\n", a, b, c, biggest(a, b, c));
return 0;
}
// function definition
int biggest(int x, int y, int z)
{
return( (x>y && x>z)?x:(y>z)?y:z );
}
#include<stdio.h>
int biggest(int, int, int); // function prototype
int main()
{
int a, b, c;
printf("Enter 3 integer numbers\n");
scanf("%d%d%d", &a, &b, &c);
//function call biggest(a, b, c)
printf("Biggest of %d, %d and %d is %d\n", a, b, c, biggest(a, b, c));
return 0;
}
// function definition
int biggest(int x, int y, int z)
{
return( (x>y && x>z)?x:(y>z)?y:z );
}
Logic To Find Biggest of 3 Numbers using Function
We ask the user to enter 3 integer numbers. We pass those 3 integer numbers to user defined function biggest. Inside function biggest we use ternary operator to determine the biggest of those 3 numbers. Function biggest returns the biggest of the 3 numbers back to the calling method/function – in above progam its main() method.
Note: Function biggest returns integer type data. And it takes 3 arguments of type integer. We’re calling function biggest from inside printf() function.
printf("Biggest of %d and %d is %d\n", a, b, biggest(a, b));
return 0;
}
//function definition
int biggest(int x, int y)
{
return( x>y?x:y );
}
#include<stdio.h>
int biggest(int, int); // function prototype
int main()
{
int a, b;
printf("Enter 2 integer numbers\n");
scanf("%d%d", &a, &b);
// function call biggest(a, b)
printf("Biggest of %d and %d is %d\n", a, b, biggest(a, b));
return 0;
}
//function definition
int biggest(int x, int y)
{
return( x>y?x:y );
}
Output 1 Enter 2 integer numbers 50 25 Biggest of 50 and 25 is 50
Output 2 Enter 2 integer numbers -5 -10 Biggest of -5 and -10 is -5
Logic To Find Biggest of 2 Numbers using Function
We ask the user to enter 2 integer numbers. We pass those 2 integer numbers to user defined function biggest. Inside function biggest we use ternary operator to determine the biggest number. Function biggest returns the biggest of the 2 numbers.
x>y?x:y
Here if x is greater than y, x will be returned else y will be returned.
Note: Function biggest returns integer type data. And it takes 2 arguments of type integer. We’re calling function biggest from inside printf() function.
1. It provides modularity to the program structure. 2. You can reuse your code. You can write function definition once and call the function any number of times in your program. 3. Easy to read, understand, edit and debug your code.
Types of Functions/Methods
1. Built in functions/methods. 2. User defined functions/methods.
1. Built in functions/methods: So far we’ve used a lot of builtin methods like pow(), sin(), cos(), tan(), sqrt() etc which are all present in library / header file math.h
If you open math.h header file and check the code, it’ll have function prototype and function definition for pow(), sin(), cos(), tan(), sqrt() etc.
Functions like printf(), scanf() and other input output functions/methods are present in stdio.h library file.
Similarly, if we want to write user defined function, we must specify the function prototype and function definition ourselves.
2. User defined functions/methods: We can define and use our own functions. We can have 4 types of user defined functions/methods. a. No return type, no arguments. b. No return type, with arguments. c. With return type, no arguments. d. With return type, with arguments.
Stay subscribed to our YouTube channel and blog for video tutorials explaining all types of user defined functions.
General Syntax of user defined Funtion/Method
1. Function prototype. return_type funtion_name(argument_data_type_list);
2. Function definition. return_type funtion_name(argument_data_type_list) { //instructions }
3. Function Call. funtion_name(argument_data_type_list);
Source Code: Function / Methods In C Programming Language: With Return Type and With Arguments
#include<stdio.h>
int add(int, int); // function prototype
int main()
{
int a = 10, b = 20, c;
c = add(a, b); // function call
printf("Addition of %d and %d is %d\n", a, b, c);
return 0;
}
//function definition
int add(int i, int j)
{
return(i+j);
}
Output Addition of 10 and 20 is 30
Source Code: Function / Methods In C Programming Language: No Return Type and No Arguments
#include<stdio.h>
void multiply(int, int); // function prototype
int main()
{
int a = 9, b = 12;
multiply(a, b); // function call
return 0;
}
//function definition
void multiply(int x, int y)
{
printf("%d x %d = %d\n", x, y, (x*y));
}
Output 9 x 12 = 108
Source Code: Function / Methods In C Programming Language: With Return Type and No Arguments
printf("Subtraction Result = %d\n", subtract() ); // function call
return 0;
}
//function definition
int subtract()
{
int a = 50, b = 25, c;
c = a - b;
return(c);
}
#include<stdio.h>
int subtract(); // function prototype
int main()
{
printf("Subtraction Result = %d\n", subtract() ); // function call
return 0;
}
//function definition
int subtract()
{
int a = 50, b = 25, c;
c = a - b;
return(c);
}
function is a keyword, fun1 is a function name we have given. Once the webpage is loaded, fun1 function will be called, and Oracle is passed to it as parameter. Inside fun1(), the span tag is selected and the passed parameter is appended to the span tag. Since the function doesn’t return any value back, we have written return false.
jQuery code: Passing Parameter To Function And Returning Value my_script.js
1
2
3
4
5
6
7
8
9
10
$(document).ready( function() {
var d = fun1(10, 20);
$("span").append(d);
});
function fun1(a, b){
return(a+b);
}
$(document).ready( function() { var d = fun1(10, 20); $("span").append(d);
});
function fun1(a, b){ return(a+b);
}
Once the webpage is loaded, fun1 function will be called, and 10, 20 is passed to it as parameters. Inside fun1(), 10 and 20 are added and the resulting value is returned back to the calling function, where in it is stored in a variable called d, where we append it to the span tag.
Here, once the user clicks on the button(with an id name), the custom function software gets invoked. Since the function here doesn’t return any value, we have written return false;
jQuery code: Function Expression with Event Binding my_script.js
Main Purposes of writing a function: Re-usability of code. Reduce the number of lines of code. Cleaner looking code. We could easily trace, modify and maintain the code.
Helps in Object Oriented Programming, as the data is bind to methods. We could write the code in such a way that, we could pass different parameters to the same function and get desired results. Hence reducing a lot of code overhead and cost of writing extra useless codes!