C Program To Find Biggest of Three Numbers using Function

In this video tutorial you’ll learn how to find biggest of three integer numbers using function.

Related Read:
Nested if else Statement In C
Biggest of 3 Numbers: C
Biggest of 3 Numbers Using Ternary Operator: C

Video Tutorial: C Program To Find Biggest of Three Numbers using Function


[youtube https://www.youtube.com/watch?v=0fxG98WizAo]

YouTube Link: https://www.youtube.com/watch?v=0fxG98WizAo [Watch the Video In Full Screen.]

Source Code: C Program To Find Biggest of Three Numbers using Function

#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

#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.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Biggest of Two Numbers using Function

In this video tutorial you’ll learn how to find biggest of two integer numbers using function.

Related Read:
Biggest of Two Numbers: C
Biggest of Two Numbers Using Ternary Operator: C

Video Tutorial: C Program To Find Biggest of Two Numbers using Function


[youtube https://www.youtube.com/watch?v=PF7sGUN_U_M]

YouTube Link: https://www.youtube.com/watch?v=PF7sGUN_U_M [Watch the Video In Full Screen.]

Source Code: C Program To Find Biggest of Two Numbers using Function

#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.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Function / Methods In C Programming Language

In today’s video tutorial lets learn the basics of function or methods in C programming language.

Function / Method: is a group of statements that together perform certain task.

Note: Every C program should have 1 main function. No 2 functions / methods should be named as main.

Video Tutorial: Function / Methods In C Programming Language


[youtube https://www.youtube.com/watch?v=X7auVd3QpTM]

YouTube Link: https://www.youtube.com/watch?v=X7auVd3QpTM [Watch the Video In Full Screen.]

Advantages of Function/Methods

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 companies(); // function prototype

int main()
{
    companies(); // function call

    return 0;
}

//function definition
void companies()
{
    printf("1. IBM\n");
    printf("2. Apple\n");
    printf("3. Google\n");
    printf("4. Oracle\n");
    printf("5. Ripple\n");
}

Output
1. IBM
2. Apple
3. Google
4. Oracle
5. Ripple

Source Code: Function / Methods In C Programming Language: No Return Type and With 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

#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);
}

Output
Subtraction Result = 25

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Custom Functions: jQuery

Video tutorial illustrates writing custom functions in jQuery.

Following topics are covered in the video:
Function Declaration.
Function Expression.
Passing parameters.
Returning value.
Returning nothing, etc ..

HTML code
index.html

1
2
3
4
5
6
7
8
9
10
11
12
<html>
<head><title>Functions in jQuery!</title></head>
<body>
 
<span></span>
<button id="name">Software</button>
 
 
<script src="script/jquery-1.8.1.min.js" type="text/javascript"></script>
<script src="script/my_script.js" type="text/javascript"></script>
</body>
</html>

Here we have 1 span tag for display purpose and a button with an id name.

jQuery code: Passing Parameter To Function
my_script.js

1
2
3
4
5
6
7
8
9
$(document).ready( function() {
 fun1("Oracle");
});
 
 
function fun1(text){
  $("span").append(text);
  return false;
}

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);
}

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.

jQuery code: Function Expression
my_script.js

1
2
3
4
5
6
7
8
$(document).ready( function() {
  $("#name").click(software);
});
 
var software = function() {
 $("span").append("<h1>Microsoft ltd</h1>");
 return false;
}

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

1
2
3
4
5
6
7
8
$(document).ready( function() {
  $("#name").bind('click', software);
});
 
var software = function() {
 $("span").append("<h1>Microsoft ltd</h1>");
 return false;
}

Here we bind the event click to the button and then invoke the function software.

Video Tutorial: Custom Functions: jQuery


[youtube https://www.youtube.com/watch?v=P_68D1foqXg]

YouTube Link: https://www.youtube.com/watch?v=P_68D1foqXg [Watch the Video In Full Screen.]



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!