What is Function in C Language?
81A function in C language is a block of code that performs a specific task. It has a name and it is reusable i.e. 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’s inbuilt functions. Printf, scanf, clrscr etc. all are C’s inbuilt functions. You cannot imagine a C program without function.
C Language
Structure of a Function
A general form of a C function looks like this:
<return type> FunctionName (Argument1, Argument2, Argument3……)
{
Statement1;
Statement2;
Statement3;
}
An example of function.
int sum (int x, int y)
{
int result;
result = x + y;
return (result);
}
Books which can help you to learn more
|
C Programming Language (2nd Edition)
Price: $39.98
List Price: $60.33 |
|
Programming in Objective-C 2.0 (2nd Edition)
Price: $25.68
List Price: $44.99 |
|
C Programming: A Modern Approach, 2nd Edition
Price: $52.71
|
|
Programming in C (3rd Edition)
Price: $30.29
List Price: $49.99 |
|
C Programming for the Absolute Beginner
Price: $15.50
List Price: $29.99 |
|
Absolute Beginner's Guide to C (2nd Edition)
Price: $17.60
List Price: $34.99 |
Best in Amazon.com
|
Apple MacBook MB881LL/A 13.3-Inch Laptop
Price: $1,169.00
List Price: $899.00 |
|
Canon PIXMA MX860 Wireless All-In-One office Printer
Price: $127.00
List Price: $199.97 |
|
Apple iPod touch 8 GB (2nd Generation) [Previous Model]
Price: $180.00
List Price: $199.99 |
|
Logitech Cordless Desktop Wave Pro
Price: $99.95
List Price: $129.99 |
|
Canon PowerShot SD1200IS 10 MP Digital Camera with 3x Optical Image Stabilized Zoom and 2.5-inch LCD (Silver)
Price: $149.00
|
|
Kindle DX Wireless Reading Device (9.7" Display, U.S. Wireless, Latest Generation)
Price: $489.00
List Price: $489.00 |
Advantages of using functions:
There are many advantages in using functions in a program they are:
- It makes possible top down modular programming. In this style of programming, the high level logic of the overall problem is solved first while the details of each lower level functions is addressed later.
- The length of the source program can be reduced by using functions at appropriate places.
- It becomes uncomplicated to locate and separate a faulty function for further study.
- A function may be used later by many other programs this means that a c programmer can use function written by others, instead of starting over from scratch.
- A function can be used to keep away from rewriting the same block of codes which we are going use two or more locations in a program. This is especially useful if the code involved is long or complicated.
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.
These function types will be dicussed in my next posting. So please keep visiting my hubpage.
Example of a simple function to add two integers.
#include<stdio.h>
#include<conio.h>
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();
}
Program Output
Explanation
Before I explain, let me give you an overview of above c program code. This is a very simple program which has only function named “add()” . This “add()” function takes two values as arguments, adds those two values and prints the result.
Line 3-8 is a function block of the program. Line no. 3 is the header of function, void is return type of function, add is function name and (int x, int y) are variable which can hold integer values to x and y respectively. When we call function, line no. “12, 13, 14”, we need to send two integer values as its argument. Then these two values get stored in variable x and y of line no. 3. Now we have two values to perform addition; in line no. 5 there is an integer declaration named “result”. This integer will store the sum of x and y (please see line no. 6). Line no. 7 simply prints the result with message.
Now imagine the same program without using function. We have called “add()” function three times, to get the same output without using function we have to write Line no. 6 & 7 three time. If you want to add more value later in the program then again you have to type those two lines. Above example is a small and simple program so it does not appear great to use function. But assume a function consist 20 – 30 or more lines then it would not be wise to write same block of code wherever we need them. In such cases functions come handy, declare once, use wherever you want.
PrintShare it! — Rate it: up down flag this hub
Comments
quite difficult 4r beginners........
i never understand about functions clearly . After reading ur topic i understand very well. Thanks a lot.
Tell me one thing Mr. Raj how function internally operate?
I wanna ask about the existence and working of formal arguements?
SIR can you teach me/us about function with scanf? i suggest about programming a factorial or fibonacci... i just dont really get it how to call the function from main... specially the design...:((
hello, dost its very easy way to understand
thanx!
plz send that how to program excute.
I hav to giv a seminar on functions in my college,will u plz suggest me d points that I include n how to answer d queries asked by students? plz answer ma immediately.
Astha i have created many tutorial on c function. You can check that in my profile. Just browse through my tutorials.
what is coding of adding two number in function using





rajkishor09 says:
9 months ago
Feel free to ask questions, post suggestion, discuss this with me or other.