Saturday 31 August 2013

Concepts Associated with Functions | C Programming Language Tutorial pdf



Concepts Associated with Functions:

1. Function declaration or function prototype
2. Function definition (function declaration and function body)
3. Combination declaration and function definition
4. Passing arguments
5. Return statement
6. Function call

Parts of function :
A function declaration can appear outside all the other functions. In this case all functions know about the other function declarations and can call this function. Functions can also be declared within other functions. In this case only the function within which the declaration is present will know about it.
Ex:
void main()
{
int i;
double cube (double);
}
Parts of a function
void main( )
{
void func1(); function declaration
…………….
…………….
func1(); function call
…………….
…………….
}
void func1()
{
……………
Function body function definition
……………
}

Function declaration:
A function declaration provides the following information to the compiler
- The name of the function
- The Type of the value returned (optioned, default is integer)
- The number and the type of arguments that must be supplied in a call to the function.
When a function call is encountered, the compiler checks the function call with its declaration. So that correct argument types are used. A function declaration has the following syntax:
return type function name (type, type ….. type);
return type specifies the data type of the value in the return statement. A function can return any data type , if there is no return value, the keyword void is placed before the function name. The function declaration terminates with a semicolon.
Ex: double cube(double);
The above declaration informs the complier that the function cube has argument of type double. The function cube returns double value. The complier knows how many bytes to retrieve and how to interpret the value returned by the in function declarations are also called prototypes, since they provide a model or blue print of the function.

Function definition:
The function definition is similar to the function declaration but does not have the semicolon. The first line of the function definition is called a function declarator. This is followed by the function body. It is composed of the statements that make up the function, delimited by braces. The declarator and declaration must use the same function name, number of arguments, arguments types, and the return type. No function definition is allowed with in a function definition.
Ex:
void main(void)
{
int i,j; /* variable declaration */
long fact (unsigned int num); /* function declaration or in prototype */
}
The definition of the function cube is given below :
double cube (double dnum)
{
return dnum * d num * dnum;
}
A function can contain any number of statements.The statements are enclosed with in curly braces { and }. The function definition may include declarations of variables and declaration of other functions. Note that the return statement need not enclose the return value with in parenthesis.

Elimination of function declaration:
The programmer can place function declarations anywhere in the program. If the functions are defined before they are called, then the declarations are unnecessary.
Ex:
#include <stdio.h>
int max (inta, int b)
{
return a>b ? a: b;
}
void main ()
{
int i,j, imax;
printf( “ enter two numbers:”);
scanf(“%d%d; & ;, &;);
i max =max (i,j);
printf(“ the maximum of %d and %d is %d”, i,j, i max);
}

Function return type:
Functions in C may or may not return values. If a function does not return a value the return type in the function definition and declaration is specified as void. Otherwise, the return type is specified as a valid data type.
main()
{
unsigned sq = squareint (32); /*function call *)
printf(“ The square of 32 is ./.n\n”, sq), /* control returns here */
}
The function squareint is called and the return value is assigned to the variable sq. the control is returned to the printf statement after the statements with in the function definition of squareint are executed.

FUNCTION PARAMETERS:
Function parameters are the means of communication between the calling and the the called function. They can be classified into formal parameters and actual parameters. The formal parameters are the parameters given in the function declaration and function definition. The actual parameters, often known as arguments, are specified in the function call.
Ex:
int sum(int a, int b) /* This and the following body (in curly braces c
{ constitutes the function definition */
return a+b;
}
void main(void)
{
int x,y,z;
z = sum (x,y);
}

Definition of function that does not return anything is as follows:
void functionname(parameter list)
{
Statement/Statements ;
return; /* optional since it is at the end of the in anyway and the in has no ref value */
}
The definition of a function that returns a value of type . Type name has the following syntax:
Typename Functionname (parameterlist)
{
Statement/Statements ;
return value; /* return keyword must be used. And it must be followed by a value that matches the return type specified by typename */
}
Even in case of functions having return values multiple return statements can exist. Parameter list is the of arguments separated by commas.

Function Call :
A function call is specified by the function name followed by the values of the parameters enclosed with in parenthesis, terminated by a semi colon (;).
Ex:
unsigned squareint(unsigned x)
{
return x * x;
}

0 comments:

Post a Comment

 

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