Function is a self-contained block of statement that perform a coherent task of some kind. In programming languages function played an important role. Here in every program we use “main()” function. When compiler start compiling if first find out “main()”. If any other function is called from “main()” only then that function will be executed.
Now there are some readymade library functions are also available in C language that is “printf()”, “scanf()”. To use these function we are using header files. For example if we want to use “printf()” in c language. Then we must use “#include<stdio.h>”. Same for C++ language if we want to use “cout” then header file “iostrem” is important.
Now here we also can create our own functions too. Why it is important? May be you need to compute same operation many time. Now if we write the code without using function then it will increases the size of the code. So if we call a function from main function then and put all operation inside that called function then size of code is much less and complexity is also decreases.
Now there is three important stapes to create a function.
1. Function Prototype Declaration
2. Function Call
3. Function Definition
we will understand these stages by example
Now Let’s see the output of this program
Now how function call work
In programming “main()” function is executed first. If you declare a function which is not connected with “main()” will not executed. For example you may declare functions “fun1()”, “fun2()” and “fun3()”. “fun1()” called from “main()” and “fun2()” called from “fun1()”. After execution of “fun2()” complicated the control back to “fun1()” then “main()”. Here “fun3” is never executed because it is not connected from “main()”.
Now let’s see the example
Now there are some readymade library functions are also available in C language that is “printf()”, “scanf()”. To use these function we are using header files. For example if we want to use “printf()” in c language. Then we must use “#include<stdio.h>”. Same for C++ language if we want to use “cout” then header file “iostrem” is important.
Now here we also can create our own functions too. Why it is important? May be you need to compute same operation many time. Now if we write the code without using function then it will increases the size of the code. So if we call a function from main function then and put all operation inside that called function then size of code is much less and complexity is also decreases.
Now there is three important stapes to create a function.
1. Function Prototype Declaration
2. Function Call
3. Function Definition
we will understand these stages by example
#include<stdio.h>
void you();//Function Prototype Declaration
void main()
{
you();//function Call
printf("I am in main Now\n");
}
void you()//function definition
{
printf("I am in Your Function\n");
}
Here “you ()” is a user defined function. Now when this function is called from “main()” function the control pass to “you ()” function and activity of main function is suspended. After execution of “you()” function is complicated the control back to “main()” function and it start execution.void you();//Function Prototype Declaration
void main()
{
you();//function Call
printf("I am in main Now\n");
}
void you()//function definition
{
printf("I am in Your Function\n");
}
Now Let’s see the output of this program
I am in Your Function
I am in main Now
Now what if you call that function after “printf()” in program. Let me write code for youI am in main Now
#include<stdio.h>
void you();//Function Prototype Declaration
void main()
{
printf("I am in main Now\n");
you();//function Call
}
void you()//function definition
{
printf("I am in Your Function\n");
}
Let see the output of this program nowvoid you();//Function Prototype Declaration
void main()
{
printf("I am in main Now\n");
you();//function Call
}
void you()//function definition
{
printf("I am in Your Function\n");
}
I am in main Now
I am in Your Function
Function is start execution from where you call it. The statement in program must executed in the same order in which you want them to executed.I am in Your Function
Now how function call work
In programming “main()” function is executed first. If you declare a function which is not connected with “main()” will not executed. For example you may declare functions “fun1()”, “fun2()” and “fun3()”. “fun1()” called from “main()” and “fun2()” called from “fun1()”. After execution of “fun2()” complicated the control back to “fun1()” then “main()”. Here “fun3” is never executed because it is not connected from “main()”.
Now let’s see the example
#include<stdio.h>
void fun1();
void fun2();
void fun3();
void main()
{
fun1();
printf("I am in main Now\n");
}
void fun1()
{
printf("I am in fun1\n");
fun2();
}
void fun2()
{
printf("I am in fun2\n");
}
void fun3()
{
printf("I am in fun3\n");
}
As we can see the “fun3()” is not connected with “main()”. Let’s see the output nowvoid fun1();
void fun2();
void fun3();
void main()
{
fun1();
printf("I am in main Now\n");
}
void fun1()
{
printf("I am in fun1\n");
fun2();
}
void fun2()
{
printf("I am in fun2\n");
}
void fun3()
{
printf("I am in fun3\n");
}
I am in fun1
I am in fun2
I am in main Now
So “fun3()” is declared but not executed. I am in fun2
I am in main Now


0 comments:
Post a Comment