FUNCTIONS
following post will learn in this post
1)What is function
A function is a self -contained block of statement that perform a coherent task of some kind.Every C program can be thought of as a collection of these function. As we noted earlier ,using function is something like hiring a person to do a specific job for you. sometimes the interaction with this person is very simple ,sometimes it's complex.
Suppose you have a task that is always performed exactly in the same way -say a by monthly servicing of your motorbike. When you want it to be done, you go to the service station and say ,"it is mechanic knows his job. you don't need to be told how the job is done. you assume the bike would be serviced in the usual way ,the mechanic does it and that's that.
↧
input-handwritten- function theory of c and c++ theory |
here is a output of up's function program
output handwritten-function theory of c and c++ theory
Here ,we have defined two function MAIN() and MESSAGE().in fact we have used the word MESSAGE at three place in the program.let us understand the meaning of each.
VOID MESSAGE();
this prototype declaration indicates that MESSAGE() is a function which after completing its execution does not return anything.this 'does not return anything'is indicated using the keyword Void.it is necessary to mention the prototype of every function that we intend to define in the program.
the second usage of MESSAGE is...
VOID MESSAGE()
{
printf("smile,and the world smile with you...");
}
This is the function definition ,In ths definition right now we are having only printf() , but we can also use if,for,while,switch,etc within this function definition
The third usage is....
message();
Here the function MESSAGE() is been called by MAIN() what do we mean we say that MAIN() 'calls' function MESSAGE() ? we mean that the control passes to the function MESSAGE() the activity of MAIN()
is temporarily suspended; it falls asleep while the MESSAGE() function wakes up goes to work. when the MESSAGE() function run out of statement to execute, control written to MAIN(), which comes to life again and begins executing it's code at the exact point where it left off. thus, MAIN() become the 'calling' function, whereas MESSAGE() becomes the 'called' function.
If you have grasped the concept of 'calling' a function you are prepared for a call to more than one function. considered the following example :
#include<stdio.h>
void italy();
void brazil();
void argentina();
int main()
{
printf(" i am in main\n");
italy();
brazil();
argentina();
return 0;
}
void italy()
{
printf("i am in italy\n");
}
void brazil()
{
pintf"i am in brazil\n");
}
void argentina()
{
printf("i am in argentina ");
}
The output of the above program when executed would be as under:
i am in main
i am in italy
i am in brazil
i am in argentina
A number of conclusion can be drawn from this program:
- A C program a collection of one and more function
- If a C program contain only one function,it must be main()
- If a C program contain more than one function,then one of these function muse be main(),because program execution always begins with main().
- There is no limit on the number of the number of function that might be present in a C program.
- Each function in a program is called in the sequenced specified by the function calls in main().
- After each function has done its thing, control returns to main().When man() run out of statement and function calls, the program ends.
Now we recall the function what we learnt.
a) A function gets called when the function name is followed by a semicolon. For example,
int main()
{
argentina();
}
b) A function is defined when function name is followed by a pair of braces in which one or more statements may be present . For example,
void argentina()
{
statement 1;
statement 2;
statement 3;
}
c) Any number can be called from any other function . Even main() can be called from other functions. For example,
#include<stdio>
void main();
int main()
{
message();
return 0;
}
void message()
{
printf("can't imagine life without proramming\n");
main();
}
d) A function can be called any number of times. For example,
#include<stdio.h>
void message();
int main()
{
message();
message();
return 0;
}
void message()
{
printf("jewel Thief!!\n");
}
e) The order in which the function are defined in a program and the order in which they get called need not necessarily be same . For example ,
# include<stdio.h>
void message1();
void message2();
int main()
{
message1();
message2();
return 0;
}
void message2()
{
printf("but the butter was bitter\n");
}
void messsage1()
{
printf("mary bought some butter\n");
}
Here ,even though message1() is getting called before message2(). However ,it is advisable to define the functions in the same order in which are called.
f) A function call itself . such a process is called 'recursion'..
1 comments:
Click here for commentsThanks for sharing useful information …. Such a great post,i want to know about, Near News - Always With You you can click this link and know mor about this