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.
printf("Enter a year to find leap year or not\n");
scanf("%d", &year);
//function call leap(year);
if( leap(year) )
{
printf("%d is leap year\n", year);
}
else
{
printf("%d is not leap year\n", year);
}
return 0;
}
//function definition
bool leap(int year)
{
if(year % 100 == 0)
{
if(year % 400 == 0)
returntrue;
else
returnfalse;
}
else
{
if(year % 4 == 0)
returntrue;
else
returnfalse;
}
}
#include<stdio.h>
#include<stdbool.h>
bool leap(int); // function prototype
int main()
{
int year;
printf("Enter a year to find leap year or not\n");
scanf("%d", &year);
//function call leap(year);
if( leap(year) )
{
printf("%d is leap year\n", year);
}
else
{
printf("%d is not leap year\n", year);
}
return 0;
}
//function definition
bool leap(int year)
{
if(year % 100 == 0)
{
if(year % 400 == 0)
return true;
else
return false;
}
else
{
if(year % 4 == 0)
return true;
else
return false;
}
}
Output 1 Enter a year to find leap year or not 2020 2020 is leap year
Output 2 Enter a year to find leap year or not 2021 2021 is not leap year
Logic To Find Leap Year or Not: Using Function
If user entered year is a century year(year ending with 00) and the year is perfectly divisible by 400, then it’s a leap year.
If the user entered year is not a century year and the year is perfectly divisible by 4, then its a leap year orelse it’s not a leap year.
In our function, we return true if the user entered year is leap year and return false if the user entered year is not leap year.
Note: User defined function leap has a return type of bool i.e., it returns either true or false. For our C program to support bool type we are including stdbool.h header file.
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);
}
Lets write our first C program – the typical “Hello World!” program.
In this video tutorial lets learn the structure of a basic C program: 1. Preprocessors – include directive. 2. Main method/function. 3. printf method/function. 4. Semicolon syntax. 5. Indentation for readability of code.
The “include” directive which we see in the first line is a preprocessor directive. Here we are including a standard library file which has some set of useful functions in it, which we are using in our “Hello World” C Program.
stdio stands for “Standard Input Output”. As the name suggests this library file has functions to read and write data to console window, along with other many useful functions. For example, printf() is a function present in stdio.h library file. We simply use it in our program to print data on to the console window. We need not know the implementation details of printf method/function.
main() method
Main method or function is part of all ‘C’ programs. It’s entry point of any C program execution. Function is a way of grouping some code together. We can write any logic/statements inside a function.
It’s a standard that main always returns a integer value. Thus main is preceded by a keyword called int, which means integer. It’s a keyword or reserve word. We’ll know more about keywords(or reserve words) in a separate video tutorial. Since main method needs to return a integer value, we explicitly return 0 at the end.
Readability of code
It’s very important that we write code which is readable. It helps in maintaining the code. Large programs get too clumsy very easily and reading and understanding code often becomes difficult. So we need to indent the code and make sure its readable as far as possible.
Note: Main method/function doesn’t take any argument.