Source Code: C Program To Find Biggest of Two Numbers using Function
#include
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.
Source Code: C Program To Check Leap Year or Not using Function
#include
#include
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.
Here we call user defined function add from within printf() function. We pass 2 integer variables as argument to add method. The values of variable a and b are copied to variable x and y. Note that values of variable x and y are local to function add and main function do not know values of x and y. x and y are separate copy and altering which do not alter the value of variables a and b.
a and b are called actual arguments. x and y are called formal arguments.
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
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
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
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
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);
}
Write a C program to find the grace marks for a student using switch. The user should enter the class obtained by the student and the number of subjects he has failed in. Use the following logic:
– If the student gets first class and the number of subjects he failed in is greater than 3, then he does not get any grace. Otherwise the grace is of 5 marks per subject.
– If the student gets second class and the number of subjects he failed in is greater than 2, then he does not get any grace. Otherwise the grace is of 4 marks per subject.
– If the student gets third class and the number of subjects he failed in is greater than 1, then he does not get any grace. Otherwise the grace is of 5 marks per subject.