Friday 30 August 2013

FUNCTIONS | C Programming Language Tutorial pdf



FUNCTIONS

Introduction :
Functions are subprograms which are used to compute a value or perform a task. They cannot be run independently and are always called by the main ( ) function or by some other function.
There are two kinds of functions:
1. Library or built–in functions
2. User–designed functions

1. Library or built-in functions are used to perform standard operations eg: squareroot of a number sqrt(x), absolute value fabs(x), scanf( ), printf( ), and so on. These functions are available along with the compiler and are used along with the required header files such as math.h, stdio. h, string.h and so on at the beginning of the program.
2. User defined functions are self–contained blocks of statements which are written by the user to compute a value or to perform a task. They can be called by the main() function repeatedly as per the requirement.

USES OF FUNCTIONS :
1. Functions are very much useful when a block of statements has to be written/executed again and again.
2. Functions are useful when the program size is too large or complex.Functions are called to perform each task sequentially from the main program. It is like a top-down modular programming technique to solve a problem
3. Functions are also used to reduce the difficulties during debugging a program USER DEFINED FUNCTIONS :
In C language, functions are declared to compute and return the value of specific data type to the calling program. Functions can also written to perform a task. It may return many values indirectly to the calling program and these are referred to as void functions.

FUNCTION DECLARATION :
The general form of a function declaration is
type name (type arg1, type arg2 …….. type argn)
{
<local declaration >
--------------------
< statement block>
--------------------
return (variable or expression)
}
Where type is the data type of the value return by the function and arguments expected.
arg1, arg2…. argn are the arguments which are variables which will receive values form the calling program, name is the name of function by which the function is called by the calling program.
There is a local declaration of variables. These variables are referred as local variables, are used only inside the function. The statement block consists of a set of statements and built-in functions which are executed when the function is called. The result is returned to the calling program through a return statement that normally appears at the end of a function block. This function block starts and ends with braces { }.

Function main() :
1. main() is the starting function for any C program. Execution commences from the first statement in the main () function
2. It returns int value to the environment that called the program. Usually zero is returned for normal termination of the main(). Non zero is returned to convey abnormal termination
3. It uses no parameter. But it may use two specific parameters
4. Recursive call is allowed for main () function also
5. Only the function body varies from programmer to programmer main (). Function heard follows the common syntax by either having no parameter or only two standard parameters.
6. The program execution ends when the closing brace of the in main is reached.

FUNCTION PROTOTYPE :
When a C program is compiled, the compiler does not check for data type mismatch of actual arguments in the function call and the formal arguments in the function declaration. To enable the compiler to check the same, a function prototype declaration is used in the main program.
Function prototype is always declared at the beginning of the main() program.

ACTUAL AND FORMAL ARGUMENTS
Passing of values between the main program and the function takes place through arguments.
The arguments listed in the function calling statements are referred to as actual arguments. These actual values are passed to a function to compute a value or to perform a task.
The arguments used tin the function declaration are referred as formal arguments.
They are simply formal variables that accept or receive the values supplied by the calling function.

Note: The number of actual and formal arguments and their data types should match.
The function call sends two integer values 10 and 5 to the function int mul(int x, int y) which are assigned to x and y respectively.
The function computers the product x and y assigns the result to the local variable p,and then returns the value 25 to the main() where it is assigned to y again..

Rules to call a function :
The following rules are used to call a function is a program:
1. A function has a statement block which is called by the main( ) or any other function.
2. When the data type in a function declaration is omitted the function will return a value of the type integer.
3. The data type of the formal argument may be declared in the next line which follows the function declaration statement.
Formal Parameter List :
The parameter list declares the variables that will receive the data sent by the calling program.
They serve as input data to the function to carry out the specified task. Since they represent actual input values, they are often referred to as formal parameters.

FUNCTION CALLS:
A function can be called by simply using the function name followed by a list of actual parameters (or arguments)
main()
{ int y ;
y = mul (10,5); / * function call * /
printf (“?%d \n”,y) ;
}
int mul(int x,int y)
{
int p ; / * local variables x=10, y=5 * /
p= x * y ;
return(p);
}
When the compiler encounters a function call the control is transferred to the function mul(), this function is then executed line by line as described and the value of p is returned when the return statement is encountered. This value is assigned to y.
Parameters can also be used to send values to the calling programs.
Parameter list contains declaration of variables separated by commas and surrounded by parenthesis.

Examples:
float quadratic ( int a , (n+b, )n+c) {………}
double power 9double x, int n) {……….}
float mul (float x, float y) {………..}
int sum (int a, int b) {………}
There is no semicolon after closing parenthesis.
The declaration of parameter variables cannot be combined int sum (int a,b) is invalid.
A function need not always receive values form the calling program.
In such cases, functions have no formal parameters,
To indicate that the parameter list is empty we use the keyword void between parenthesis as
void printline(void)
{
}
This function neither receives any input values nor returns back any value.
Many compilers accept an empty set of parenthesis without specifying anything as void printline ( )
But it is good programming style to use void to indicate null.

Note:
1. The parameter list must be separated by commas.
2. The parameter names do not need to be the same in the prototype declaration and the function definition.
3. The types must match the types of parameters in the function definition in number and order.
4. Use of parameter names in the declaration is optional.
5. If the function has no formal parameters, the list is written as void.
6. The return type is optional, when the function returns int type data.
7. The return type must be void of no value is returned.
8. When the declared type do not match with the types on the function definition compiler will produce an error.

FUNCTIONS ACCEPTING MORE THAN ONE PARAMETER:
A function can accept more than one parameter. The parameters are separated by commas. For example, consider the following program having a function maxfunc() that accepts three parameters and computes their maximum.
#include <stdio.h >
int maxfunc(int i, int j, int k)
{
int max ;
if (i> = j && i> = k)
max = i ;
else if (j> = k)
max = j;
return max;
}
void main ( )
{
int m, a,b,c;
printf (“ input 3 numbers:”) ;
scanf(“%d%d %d “, & a ,&b, & c);
m= maxjunc (a,b,c);
printf(“ The maximum is%d \n”,m);
}
Run:
Input 3 numbers: 4
6
5
The maximum is 6

The main function calls the function maxfunc(). Three integer variables passed to it are separated by commas. The return value is assigned to m and is displayed. Since maxfunc() comes before the function main separate function and function definition are unnecessary.
The variables m,a,b and c are declared in main. They can be used only in the function main. An attempt to use them in maxfunc() causes a compile time error. Similarly, the variables max,i,j and k (i,j,k being the parameters which the function accepts) belong to the maxfunc(). These variables cannot be accessed in main. The region of the program where an identifier can be accessed is called the “Scope of the identifier”. Thus the scope of 'a' is the function main, while the scope of i is the function maxfunc().
The variables with the same name can exist in both main and maxfunc().

USER DEFINED AND LIBRARY FUNCTIONS :
We used the functions such as printf and scanf which are already written, compiled and placed in the C-library and are called library functions.
Functions which can be defined by the user are called user defined functions.

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved