Previous | Home | Next |
A function is a block of code that performs a particular task. It has a name and it is reusable and it can be executed from as many different parts in a C Program as required. It also optionally returns a value to the calling program So function in a C program has some properties discussed below.
Every function has a unique name. This name is used to call function from main() function. A function can be called from within another function.
A function is independent and it can perform its task without intervention from or interfering with other parts of the program.
A function performs a specific task. A task is a distinct job that your program must perform as a part of its overall operation, such as adding two or more integer, sorting an array into numerical order, or calculating a cube root etc.
A function returns a value to the calling program. This is optional and depends upon the task your function is going to accomplish. Suppose you want to just show few lines through function then it is not necessary to return a value.
But if you are calculating area of rectangle and wanted to use result somewhere in program then you have to send back (return) value to the calling function.
C language is collection of various inbuilt functions. If you have written a program in C then it is evident that you have used C inbuilt functions. Printf, scanf, clrscr etc. all are C inbuilt functions. You cannot imagine a C program without function.
Example
int sum (int x, int y) { int result; result = x + y; return (result); }
Types of functions
A function may belong to any one of the following categories:
- Functions with no arguments and no return values.
- Functions with arguments and no return values.
- Functions with arguments and return values.
- Functions that return multiple values.
- Functions with no arguments and return values.
#include
#include
void add(int x,int y)
{
int result;
result = x+y;
printf("Sum of %d and %d is %d.\n\n",x,y,result);
}
void main()
{
clrscr();
add(10,15);
add(55,64);
add(168,325);
getch();
}
Call by value and Call by reference
The argument passed to a function can be of two types:
1. value passed (call by value)
2. address passed (call by reference)
Let's say we have an integer variable named x.
A call to a function by value using x means (a copy of) the value that x stores is passed in the function call and no matter what the function does with that value, the value stored in x remains unchanged.
A call to a function by reference using x means a reference (also called a pointer or alias) to the variable x is passed in the function call and so any changes the function makes using this reference will actually change the value stored in x.
Previous
Home
Next
A function may belong to any one of the following categories:
- Functions with no arguments and no return values.
- Functions with arguments and no return values.
- Functions with arguments and return values.
- Functions that return multiple values.
- Functions with no arguments and return values.
#include#include void add(int x,int y) { int result; result = x+y; printf("Sum of %d and %d is %d.\n\n",x,y,result); } void main() { clrscr(); add(10,15); add(55,64); add(168,325); getch(); }
Call by value and Call by reference
The argument passed to a function can be of two types:
1. value passed (call by value)
2. address passed (call by reference)
Let's say we have an integer variable named x.
A call to a function by value using x means (a copy of) the value that x stores is passed in the function call and no matter what the function does with that value, the value stored in x remains unchanged.
A call to a function by reference using x means a reference (also called a pointer or alias) to the variable x is passed in the function call and so any changes the function makes using this reference will actually change the value stored in x.
Previous | Home | Next |