Overview of Functions in C.
Hi Everyone,
Today we will discuss about Function in C and its uses and benefits in C Program. In my Previous blog I discussed about Pointers so you can use Pointers in Functions as well . So before starting Functions in C please go through my previous blog in below link where you will be able to see the topics in archive folder and it’s easy for you to understand the programs and uses of Functions in C.
dheeraj60.blogspot.com
Let’s Start with Function In C.
Function is a block of statements that performs a specific task, For re usability we use Function in C Program If you are building an application in C language and in one of your program, you need to perform the same task more than once we can use functions.
Uses of Functions in C:
1. C functions are used to avoid rewriting same code again and again in a program.
2. No limit in calling C functions to make use of same functionality wherever it is required.
3. We can call functions any number of times in a program and from any place in a program.
4. If you write large program in C it can be easily tracked when it is divided into functions
There are two Types of Functions in C .
1. Pre-defined Function : Pre- Defined Function we use is puts(), gets(),scanf() , printf() etc which we already defined in header file stdio.h so we can use this functions whenever it’s required as per requirement of the program.
2. User – Defined Function : Programmer creates the function its own that is User- Defined function . In User- Defined function Programmer can manipulate the function whenever it’s required and have control over the program.
Understand the uses of Functions by example:
Syntax of Function:
return_type function_name (argument list)
{
statements – Block of code
}
Here return_type can be used for any data type like int, char , float etc. function_name we can give any suitable name so that we can refer and use it accordingly.In Argument list we can have multiple variable with their data types and finally block of code is set of C statement where we can use programming as per our requirement.
Let’s use this syntax to write one simple program by using User – Defined Function where we can use the function for addition and call the function in program only.
Program is as below:
#include <stdio.h>
#include <stdlib.h>
int add(int i, int j)
{
int sum;
sum = i+j;
return sum;
}
int main()
{
int a, b;
printf("Enter First number: ");
scanf("%d",&a);
printf("Enter second number: ");
scanf("%d",&b);
int result = add(a,b);
printf ("Output: %d", result);
return 0;
}
Here in above program before main we created one function add and in statement / block of c code we will use the function add.
Arguments used in Function add before main in function is add as int add(int i, int j){ int sum; sum = i+j; Function return type is integer so we are returning integer as sum . Now in code block under main we use input from user as integer as for calling the function add here in the function return type. Integer variable to hold the return value of add function. And for output we simply use function add to add the number passed by the user.
Below is the output of above program in compiler code block.
Here you can see the output as sum of two number by using function . So we can write some another program with different datatype for function and use it in same manner. Let’s take 1 simple example of finding the square of any number by using function with float data type .
Program is as below:
#include <stdlib.h>
float square ( float i );
int main( )
{
float a, b ;
printf ( "\nEnter some number for finding square \n");
scanf ( "%f", &a ) ;
b = square (a) ;
printf ( "\nSquare of the given number %f is %f",a,b );
}
float square ( float i )
{
float s ;
s = i * i ;
return ( s ) ;
}
Here we define float square ( float i ) so main function program will starts from here and in block code or statement float square ( float i) we define the function and return the function in variable so here we not use return 0 as we already returning value from function square variable s.
Below is the output of above Program in Compiler Code Block.
Note : If Function return type is char then function should return a value of char data type and while calling this function the main() function should have a variable of char data type to store the returned value.
Structure of function by using char data type should be as below:
char dotc(char ch1, char ch2)
{
char ch3;
return ch3;
}
int main()
{
char c1 = abc('a', 'b');
}
Hope you are able to understand the basic overview of functions and it’s benefits. Please try to write some code by using user defined functions and call it in code and see the benefits and if any doubt let me know.
Function is a big topic as function can be uses as Function – Call by value method and Call by reference method. So in next blog I will come up with both the topics in functions see its uses and benefits then we will also see the uses of Pointers in Functions.
Please read this blog carefully so that it will be easy for you to understand function call by value method and call by reference method.
Thanks.
Comments
Post a Comment