Functions are introduced by the concept of write once and run any number of times.
--> A Function is self contained block of code which perform a predefined task.
Syntax:
returntype function_name(arg list)
{
----------
----------
----------
}
--> Each and every function identified with a name and it is referred as FUNCTION NAME.
--> The rules for naming a function are same as that of naming a variable.
--> Function name should be unique with in a program i.e no two functions should have same name in the program.
--> The function name should be followed by parenthesis which contains the data supplied to that function.
--> The data supplied to the function are referred as arguments or parameters.
--> The function name should be preceded with a return type.
--> If a function does not return any value, then return type is to be specified as void.
--> Return type specifies the type of values being sent out of the function.
--> A function can return only one value.
--> If we don't specify any return type then by default the return type is considered as int.
--> Function body specify the work assign to that function.
--> Even if we define several functions, they will not be executed unless they are called directly or indirectly by the main( ) function.
--> Using an existing function perform the required operation is referred as FUNCTION call.
--> In Function call, we shouldn't specified return type and function call.
--> The functions provided by C language as a part of C language are referred as primitive / pre-defined / built-in functions.
--> The functions defined by the user as the requirement are referred as user defined function.
--> Based on the arguments and returning value, functions are classified under four categories as follows.
1) Functions without arguments and not returning a value.
2) Functions with arguments and not returning a value.
3) Functions without arguments and returning a value.
4) Functions with arguments and returning a value.
--> When a function is called, the control transfers from function call to the function definition, execute all the statements in that function and after completing the execution the control transfer to the next statement after the function call and continue the execution.
Sample Program:
#include< stdio.h >
#include< conio.h >
void display( );
void display( )
{
printf("\nLet Us Code in C");
}
void main( )
{
display( );
display( );
display( );
}
Note:
--> We can't define a function in another function.
--> A function can be defined either before using it, then its prototype is to be specified before using it.
Program:
#include< stdio.h >
#include< conio.h >
void main( )
{
void display( void );
display( );
display( );
display( );
}
void display( void );
{
printf("\nLet Us Code in C");
}
--> The prototype should be specified only in the declaration section.
--> When we specified prototype in a function, then that function can be access only in that function.
--> Sometimes we may have a requirement that the function should be access any function in that program.
--> In general, the life time of a variable will be only for the execution of the block or function.
Program:
#include< stdio.h >
#include< conio.h >
void display( );
void main( )
{
display( );
display( );
display( );
}
void display( )
{
int x;
printf("\nEnter any number: ");
scanf("%d",&x);
if(x%2 == 0)
printf("\n\t EVEN NUMBER");
else
printf("\n\t ODD NUMBER");
}
Scope of a variable:
Scope of a variable refers to portion of the program in which that variable can be access or referred to.
--> So, the variables declared in a block are said to be local to that block.
--> Similarly the variables declared in a function can be access only within that function and can't be access directly in other functions.
--> So, the variables declared in a function are said to be local to that function.
--> Sometimes the variable declared in a function may be required to the access in some other function. This is possible by sending the corresponding values as arguments when calling that function.
--> The arguments specified in the function call are referred as Actual Parameters.
--> The arguments specify in the function definition are referred as Formal Parameters.
--> When the function is call, the control transfers from function call to the function definition and the actual parameters get copied to the formal parameters.
Program:
#include< stdio.h >
#include< conio.h >
void display( int );
void main( )
{
int x;
printf("\nEnter any number: ");
scanf("%d", &x);
display(x);
}
void display(int n);
{
if(n%2 == 0)
printf("\nEVEN NUMBER");
else
printf("\nODD NUMBER");
}
Note:
--> In function prototype, it is optional to specify variable names.
--> In general actual parameters are referred as arguments i.e the data supplied to a function in the function call.
--> Formal parameters are referred as parameters i.e the data specified in the function definition.
--> We can send as many number of arguments as possible and required to a function.
--> The number of actual parameters should be same as number of formal parameters.
--> The datatypes of the actual parameters should be same as the datatypes of the formal parameters.
--> The actual parameters get copied to the formal parameters in the same order and sequence.
Program:
#include< stdio.h >
#include< conio.h >
void sum(int,int);
void main( )
{
int x,y;
printf("\nEnter two numbers x & y : ");
scanf("%d"%d",&x,&y);
sum(x,y);
}
void sum(int a,int b)
{
int c;
c = a + b;
printf("\nSUM = %d",c);
}
--> return statement is used for sending values from one function to another function.
Syntax:
return value/variable/expression;
--> A function can be return only one value.
--> A program can have as many number of return statements as possible and required, but only one among then will be executed.
--> The value return will be placed in the corresponding function call or statement.
--> Based on the requirement, we can store the returned value in a variable.
--> The return type should be specified as data type of the value being return.
Program:
#include< stdio.h >
#include< conio.h >
void sum(int,int);
void main( )
{
int x,y,z;
printf("\nEnter two numbers x & y : ");
scanf("%d"%d",&x,&y);
z = sum(x,y);
printf("\nRESULT: %d",z);
}
int sum(int a, int b)
{
int c;
c = a + b;
return c;
}
0 comments:
Post a Comment