Definition:-
- it is basically a piece of code
- it has name for identification
- a function can be called multiple times to maintain the reusability and modularity.
- by writing function we can avoid same writing/piece of code
1.function declaration:- a function must be declared globally in c , to tell the compiler about function name, function return type, function parameter.
Syntax:- return_type function_name(arguments or data_types parameter);
example:- void sum(int a,int b);
2.function call:- a function can be called anywhere from the program, pass the same no. of parameter as we write in function declaration.
Syntax:- function_name(argument_list)
example:- sum(a,b)
3.function definition:- it contains the actual statements which is executed, when a function is called.
Syntax:- return_type function_name(argument_list) { function body ; }
example:- void sum(int a, int b){ int c=a+b;}
note:- the return_type and argument_list in function write same, in function calling on function name is required which must be same as function_declaration.
Function type:- there are 2 types of function
- predefined or library function:- are those function which are already defined in the c header files such as printf(), scanf(), getch(), puts(), gets(), ceil(), floor(), etc.
- user-defined function:- are those function which can be defined by the programmer.so that the programmer use it multiple times.
Different ways to define a function calling:-
- takes nothing return nothing
- takes something return nothing
- takes nothing return something
- takes something return something
1) takes nothing return nothing:-in this a function is created which have no return_type (i.e return nothing) or no arguments in definition(takes nothing) and in function_call also.
Explanation:- here first function is declared with name add. which have void i.e no_return type and also doesn't have any arguments.
after declaration function definition is provided which takes no parameter and return nothing(i.e return type is void).
last inside main body a function is called which pass nothing and takes nothing.
2)takes something return nothing:- in this a function which takes the parameter and use them but return nothing.
Explanation:- first declare a mult function which takes one argument of int data types but return nothing.
second the declared function is defined where argument b =n(i.e 4) enter any no. multiply it with an entered number.
third to execute a function , it is called inside main body from where one argument is passed, which is n=4.
3)takes nothing return something:- in this a function is created which have no argument but return some value.
Explanation:-
1) function sum is declared with return_type int(i.e it return integer value when a function is called, which we need to store ) and takes nothing because there is no argument.
2) inside main function a function is called, and assign it to a result variable because we know that this function return integer value which is store in result variable, after storing result is printed
3) now function definition, defining what is return, it return the addition of two variable a and b.
4)takes something return something:-in this a function is created which return a value and also takes the arguments.
Please share with your friends
Comments
Post a Comment