Functions In C_ C Tutorial In Hindi #19 _ CodeWithHarry
Functions In C_ C Tutorial In Hindi #19 _ CodeWithHarry
Advantages of Functions :
We can avoid rewriting same logic or code through functions.
We can divide the work among programmers using functions.
We can easily debug or can find bugs in any program using functions.
Function Aspects :
There are 3 aspects of function :-
1. Declaration
2. Definition
3. Call
Types of Functions :
Library Functions – These are pre-defined functions in C Language. These
are the functions which are included in C header files.
E.g. printf(), scanf() etc.
User defined Functions – Functions created by programmer to reduce
complexity of a program i.e. these are the functions which are created by
user or programmer.
E.g. Any function created by programmer.
NOTE -: Every C Program must contain one function i.e. main() function as
execution of every C program starts from main() function.
As you can see in above example the function with name ‘Star_pattern’ is
not taking any argument (Since it’s parentheses are empty) and even it is
not returning any value as we haven’t even used return keyword for returning
something and in return_type we have written ‘void’ which means nothing.
So, with this example you can understand about functions which do not take
any argument or value and even don’t return anything.
/*Other Code*/
int Sum()
{
int x,y,z;
printf("Enter no. 1 : \t");
scanf("%d",&x);
printf("\nEnter no. 2 : \t");
scanf("%d",&y );
z=x+y;
return z;
}
In above example you can see that our function with name ‘Sum’ is not
taking any value or argument (Since parentheses are empty) but it is
returning an integer value. So, that’s how we can use functions which don’t
take any argument but returns something.
// Other Code
Product(5,4) /* Calling Product Function in main() */
In this example as you can see we have declared function globally i.e. this
function can be used anywhere in our program not just only in main()
function. After that we have defined it to tell what this function will do and in
this function as it will take some value or argument, it will process that data
and will give some output but it will not return anything.
When we pass arguments in function at the time of calling it at that time only
the value of variables get copied to variable of that function means only
values are passed. It means any modification which is done on passed
values do not affect the actual values of the variables in main() function.
And this call is known as Call by Value.
So, this is the widely used method to define function in this way we give
arguments to function and also get some value in return from it.
So, that’s all about Functions in C Language.
#include <stdio.h>
int sum(int a, int b);
void printstar(int n)
{
for(int i = 0; i < n; i++)
{
printf("%c", '*');
}
}
int takenumber()
{
int i;
printf("Enter a number");
scanf("%d", &i);
return i;
}
int main()
{
int a, b, c;
a = 9;