Search This Blog

Friday, November 13, 2009

FUNCTION NOTES

Function
Definition
§ A function is a self-contained block or sub program which has one or more statements that will do the specific task when called

§ The best way to develop and maintain large programs is to construct them from smaller pieces or modules, each of which is more manageable than the original program.These smaller pieces are called functions
§ The functions are reusable

§ There are basically two categories of function:
Predefined functions
Available in the C / C++ standard library such as stdio.h, math.h, etc
User-defined functions
ü functions that the programmers create for specialized tasks
ü This is non-standard functions normally provided in the non-standard libraries.

characteristics
A function is named with unique name.
ü By using the name, the program can execute the statements
contained in the function, a process known as calling the function.

ü A function can be called from within another function.

A function performs a specific task
A function is independent
A function may receive values from the calling program (caller)
ü Calling program can pass values to function for processing whether directly or indirectly (by reference).
A function may return a value to the calling program
ü A function may or may not return a value.
ü If function does not return a value, then the function return type is said to be of type void.
ü To return a value from a function, use return keyword, followed by C expression.
ü The value is passed back to the caller.


Syntax of Function definition
Return_type function_name(parameter 1,parameter 2..)
{

Function body // consists of declaration & executable part
}


ü The first line of every function is called function header. It has 3 components, as shown below:
ü Function body consists of the following
o Enclosed in curly braces, immediately follows the function header.
o When a function is called execution begins at the start of the function body and terminates (returns to the calling program) when a return statement is encountered or when execution reaches the closing braces (}).
o Variable declaration can be made within the body of a function.
o Variables declared in a function, are called local variables. The
scope, that is the visibility and validity of the variables are local.
o Outside of any functions, those variables are called global variables. Three rules govern the use of variables in functions:

1. To use a variable in a function, the programmer must declare it in the function header or the function body.
2. For a function to obtain a value from the calling program (caller), the value must be passed as an argument (the actual value).
3. For a calling program (caller) to obtain a value from function, the value must be explicitly returned from the called function (callee).

For example

// To Display the message Hello using Function

#include
Void main()
{
// function prototype
Void show();
;
;
show(); // function call

}

// function definition ; function contains the code that will be executed

Void show()
{
Printf(“Hello”);
}


O/P
Hello


§ When a program calls a function show , executions passed to the function show and display the message “ Hello” and then back to the calling program’s code.

Types of functions
1. Functions with no arguments and no return values.
2. Functions with arguments and no return values.
3. Functions with arguments and return values.
4. Functions with no arguments and return values
Functions with no arguments and no return value.
· No data can be passed to the called function. Similarly, function with no return type does not pass back data to the calling function.
· It is one of the simplest types of function in C.

For example
#include
#include
void main()
{
void printline()
clrscr();
printf("Welcome to function in C");
printline();
printf("Function easy to learn.");
printline();
getch();
}

void printline()
{
int i;
printf("\n");
for(i=0;i<30;i++)
{ printf("-"); }
printf("\n");
}

O/P
Welcome to function in C
----------------------------------
Function easy to learn.
----------------------------------
Functions with arguments and no return value.
· This type of function can accept data from calling function. In other words, you send data to the called function from calling function but you cannot send result data back to the calling function.
For example
#include
#include
void main()
{
void add(int x, int y)
clrscr();
add(30,15);
add(63,49);
add(9,1);
getch();
}
void add(int x, int y)
{
int result;
result = x+y;
printf("Sum of %d and %d is %d.\n\n",x,y,result);
}
O/P
Sum of 30 and 15 is 45
Sum of 63 and 49 is 112
Sum of 9 and 1 is 10
Functions with arguments and return value.
· This type of function can send arguments (data) from the calling function to the called function and wait for the result to be returned back from the called function back to the calling function.
· this type of function is mostly used in programming world because it can do two way communications; it can accept data as arguments as well as can send back data as return value.
· The data returned by the function can be used later in our program for further calculations.


For example
#include
#include
int add(int x, int y)
{
int result;
result = x+y;
return(result);
}
void main()
{
int z;
clrscr();
z = add(952,321);
printf("Result %d.\n\n",add(30,55));
printf("Result %d.\n\n",z);
getch();
}
O/P
Result 85
Result 85
Functions with no arguments but returns value.
· We may need a function which does not take any argument but only returns values to the calling function then this type of function is useful.
For example
#include
#include
void main()
{
int send() ;
int z;
clrscr();
z = send();
printf("\nYou entered : %d.", z);
getch();
}
int send()
{
int no1;
printf("Enter a no : ");
scanf("%d",&no1);
return(no1);
}
O/P
Enter a no 5
You entered :5



Types of Function

No comments:

Post a Comment