Raj is an ardent coder who loves exploring new technology. He is an IT pro with 9 years of exp in C#, Angular, React, Vue.
A 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 library 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.
function syntax in C
Function Prototype and Function Definition in C Programming
Function prototype is very simple concept but the word (prototype) we use to call it makes it difficult to new C programmer to understand. In simple word, function prototype tells compiler that we are going to use a function which will have given name, return type and parameters. It’s that simple.
Function definition on the other hand is just writing logic of any function. For example you have one function prototype in C program for adding two integer numbers (i.e. int add(int, int)) but along with prototype you also have to write logic how that function will behave (respect to above prototype; how you will utilize those two passed integer value and how you will return value) when you will call that function.
Let’s take look at example for better and clear understanding:
Function Prototype and Function Definition Example
Now we have a simple C program to demonstrate function prototype and function definition concept. We will start from line no. 4 & 5. In these two lines we have function prototype, line no. 4 has a simple function prototype which doesn’t have parameters or return type. It’s very simple to declare a prototype, see line no. 17 & 4, both line are same except prototype requires semicolon (;) at the end. Code block (line no. 17-20) is function definition for our first function prototype (i.e. line no. 4). In this block we are defining behavior of that function and in my code it’s just printing a simple message for demonstration.
Similarly we have second function prototype (line no. 5) and it has parameters and return type. Code block (line no. 22 – 25) is definition of above function prototype. One important point for function prototype having parameters; in prototype, variable name for parameters is optional (see line no. 5). There is no variable name for int parameters, its (int, int). But in that prototype’s function definition you must provide parameter variable name (see line no. 22). We have (int a, int b) parameter name.
One more important thing, if you writing function definition above main() function then you don’t need to write prototype of that function. But it’s a good practice to keep all the functions below main() function.
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.
Example of function calling in C
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()” . Calling this C function from “main()” is very simple. 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 C’s inbuilt function “printf”.
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.
- How to Reverse a Number?
This tutorial will help you to learn about function with parameters and return type. This tutorial is about how to reverse a number where I created a new function to reverse a number. Hope this will help to understand function better...
- Call by Value and Call by Reference in C Programming
Call by value and call by reference is the most confusing concept among new C language programmer. I also struggled with this, the reason may be my teacher is not explaining it in simple words or I was dumb. Whatever the reason is; here I will try to
Share your opinion with me
© 2009 RAJKISHOR SAHU
DoveFreexrolo on April 29, 2016:
Helpful information. Fortunate me I found your website unintentionally, and I am surprised why this twist of fate didn took place earlier! I bookmarked it.
nayanshill@2016 on January 12, 2016:
all c++ 2016
Usha K S on August 19, 2014:
I Cleared my doubts after view'd ur site....
alpa alok on September 22, 2013:
Nu Dombkowski on July 24, 2013:
[quote]8192 characters left.[/quote]
Good write ups, With thanks..
Nicely put, Thanks a lot.
Rono Dominic on May 09, 2013:
What is the difference between calling a function by value and by reference?
pihu on April 16, 2013:
thanks, it help me to understand function . but give more example for better understanding
aashi on April 16, 2013:
mah on March 24, 2013:
what is the differece between formal and actual arguments?
faisal on January 09, 2013:
yar main ny jitny b functions waly programs try kiye hain koi b nai chala hi... sir your program is also dont working so give me some more examples. with full syntex. thanx
fredfmah on December 02, 2012:
Dear Raj, many thanks for a clear exposition of each theme.
I didn't see an example for something similar to what I am trying to code.
I am using a Pic 18F2550 and the C18 c0mpiler.
The program itself is very simple in structure.
NTC thermistor -- 18F2550 -- LCD 44780
Yes a thermometer.
You explained some very important things to me. I program quite well in Pic Assembler but you helped me see.
1. What in C is named as a function, It is named as a subroutine in assembler
2. The assembler program usually has a MAIN which basically "call" a set of subroutines.
You showed me the Call word is not need in C, naming the subroutine is sufficient
3. Just one little problem .
In the above thermometer example, before you can send data to the LCD, you need to Initiate with a series of set up commands.
This is a ONE TIME block of code.
Then after that the following functions are
sequence then is
1. Read NTC resistance do 10bit voltage to Resistance conversion..
2.Do maths to convert the resistance to temperature
3. Send temperature to the LCD
4. Go back to 1. for the next temperature reading.
I'm thinking this is a loop , an infinite while(1) loop only broken by an out of range voltage.
Then print resistance error.
I'll keep on reading your C seminar to see if you think something like this could be of general interest.
My best wishes
rocky on November 21, 2012:
thank you so much...
rainbow sky on August 24, 2012:
Can help me do this?
Write a complete user friendly menu driven C program to perform Matrix operations such as Addition, Subtraction, Multiplication and Transpose according to the user’s choice.
Megha on July 19, 2012:
Dear sir,want to know about data abstraction in c++.sir I cannot understand it .I think that data abstraction also occur in c language also sir please helpme and please tell me your book of c++ link
bhavna on July 10, 2012:
sir i ask
Rupesh Kumar on May 22, 2012:
its verry-verry helpful for me.....
vijay on May 22, 2012:
RAJKISHOR SAHU (author) from Bangalore, Karnataka, INDIA on April 28, 2012:
If you want you can check types of function tutorials also....
himanshu gehani on April 28, 2012:
that's call function definition.Very good function describe and give the examples.
abdul salam on April 22, 2012:
it's very easy and helpful solution of function . i like it
it's easy example for explain. sorry ,but i am weak in c.
i hope you can helped me.
please give me 'c' notes.
aiyein on March 31, 2012:
can you help me to do this?
i cant get the output, but there's no mistakes in my coding.
'write a program that ask the user to enter a temperature reading in centigrade and then prints the equivalent Fahrenheit value. It then ask the user to enter a Fahrenheit value and prints out the equivalent centigrade value. Run the program several times. Be sure to include at least one negative temperature reading in your test cases. Provide separate functions as needed by your design.Your main function should have only function calls. The input, Fahrenheit=100, Celsius=36.9 '
Hope you can help me.. thank you!
indresh bind on March 30, 2012:
I love c language thanks to chetan vyas sir. mai kuch mahine pahale c me kuch bhi nahi janta tha aur aaj mai program ache se kar leta hu jiska pura shrey chetan sir ko jata hai jisne itni mehanat ke sath mujhe padaya aur mujhe guide kiya thanks sir.
hera on March 27, 2012:
a program that will take total marks of 3 frndz koli,joli, moli
grading scheme: more than 89=A+,80 to 89=A,70 to 79=B+,
60 to 69=B,50 to 59 =C,less than 50=F.
kathy got the higest .
sample out put:joli got the higest number.
hera on March 27, 2012:
its so nice
heena on March 23, 2012:
i want to print a rectangle using functions and for loop in c...any help please
piusraiser on March 17, 2012:
can you help me to solve this program
Print following pattern using a C program.take the string as input from the user .write a genric program to acceptt string of any length...
If the user input nepal than the output should be as follow
hussainbutt on March 16, 2012:
Thank you this is really very helpful for me. Thanks again.
daman bakhariya on March 11, 2012:
very good who have posted this
bipin stha on March 10, 2012:
differentiate between call by value and call by referance while calling a function
rogelio on March 08, 2012:
hi!sir could you please give me an idea to make this program!!
1. Write a program that determines whether a positive integer is a prime number. A prime number is a number that can only be evenly divided by 1 and itself. The program should contain the function,isPrime, and returns 1 if the integer is a prime number and return 0 if otherwise.
shalaka gharade on February 24, 2012:
Rahul Rawat on February 22, 2012:
thanks sir it was amazing.
sandeep on February 17, 2012:
its very helpful for me thanks
ankita gupta on February 17, 2012:
it is very useful thanx to give us brief knowledge
Banker on February 13, 2012:
code for the print the longest word written in a line using udf...plzz reply fast...
TANUJA on February 09, 2012:
madura bharathi on February 03, 2012:
vineela on February 03, 2012:
we can know more new things in c-programs
RAJKISHOR SAHU (author) from Bangalore, Karnataka, INDIA on January 24, 2012:
@shivani: you can read that here https://discover.hubpages.com/technology/C-Program...
shivani on January 22, 2012:
m asking 1 que.....
what is different betbeen call by reference and call by result value
RAJKISHOR SAHU (author) from Bangalore, Karnataka, INDIA on January 17, 2012:
@Pravin : Thanks for your nice comments, it really gives motivation to write more tutorials when friends like you appreciate. By the way we can become friends on facebook, in my profile I have provided link. Take care and happy learning.............
now refreshed pravin on January 16, 2012:
itzzzz amazing raj..i dint really knew anything abt functions.the way u arrange things with numbering of each line and their link to other line is simply incredible.if i become a gr8 s/w engg,i wud surely say u were the man behind me..i wud be pumped if i cud meet u..,i live in hosur which is quite near to urs..giv ur mob.no to this pls..email@example.com...plzzzzzzz...
RAJKISHOR SAHU (author) from Bangalore, Karnataka, INDIA on January 14, 2012:
@Biplab: You can find that information here https://discover.hubpages.com/technology/Types-of-...
biplab on January 14, 2012:
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.plz explain it
sunil nirmalkar on January 12, 2012:
sir plz explaun the nots:- 1)introduction to programming 2)Algorithms for problem solving 3)introduction to 'c'lang 4)condition statements and loops 5)arrays 6)function 7)strctures and unions 8)structures and unions 9)pointers 10)self Refential structures and linked lists 11) file processing...plz plz sir i wat my e-mail id :- firstname.lastname@example.org
abc on January 08, 2012:
plz sir explain need of user defined function
Tanu sharma on January 04, 2012:
sir plz will u explaun the program of recurrsion plz sir i cnt get tht program
trishul on January 03, 2012:
please explain the program, how the execution will done:
Rajesh on January 03, 2012:
am asking: 1. what is prototype?
can explain other way plz.
2. call by value,call by reference ?
Nancy on January 03, 2012:
thanks a lot.
priya on January 02, 2012:
its very useful...
jessy on January 02, 2012:
why do u write getch() in void main()?
How do u realize whether your function should return a value?
RAJKISHOR SAHU (author) from Bangalore, Karnataka, INDIA on January 01, 2012:
@Kishan Patel : factorial program is already there, visit https://hubpages.com/technology/C-Programming-Recu...
kishan patel on January 01, 2012:
hiiiiii sir,you are awesome but can you give me a program to find the factorial of a number,please post it fast.................
kishan patel on January 01, 2012:
hiiiiiiiiiiiiiiii!!!!!it's really good,but I am a student of 8 grade,so i want extra explanation,will you help by online chat.AS i am intrested in this topic......so when you are online,please tell me
Asif Raza on December 30, 2011:
bundle of thanks ,i have learnt much of functions
aravind on December 27, 2011:
how to pass the characters using function..(without using pointers)
A/razak haji on December 25, 2011:
My problems in c function are solved now
dinu(baby) on December 24, 2011:
thanx, it's good using function.
Hi.. mukesh kumar from varanasi on December 19, 2011:
It is a good progrram using function.
M Junaid Awan on December 18, 2011:
good job man....
soham on December 11, 2011:
thnx man...saved my ass before my semester.! :)
nag on December 10, 2011:
Ron on December 07, 2011:
thnks for info
nek yadav on December 02, 2011:
thanx dude u are a great teacher. my whole problems are solved now in function.
email@example.com on December 02, 2011:
what are the type conversions ?explain with the help of an example?
renuka on November 30, 2011:
sir please explain deeply about function prototype and finction defination
rohit on November 29, 2011:
sir plz give me details fuction in c for collage seminar
aruna on November 27, 2011:
5 5 5
plz send me d 'c' code for the above problem
ankur on November 22, 2011:
what is the program of multiplication in function
nk on November 14, 2011:
maneesh dwivedi on November 12, 2011:
thank u sir i am very happy to get such help by ur programs
Balaji Naidu on November 12, 2011:
will u please post the program explaining using function inside a function.......which explains clearly....pls do favour....
gaurav nigam on November 10, 2011:
dip on November 09, 2011:
i'm realy thankful to u.
akasha on October 30, 2011:
i m so much happy for your nice procedure in c.thanks ......
shah on October 29, 2011:
i really understand from this description. so thnx dear
EMAN on October 29, 2011:
Thanx for providing guideeness
elanr on October 24, 2011:
Can we pass "result" as interger as argument inside the add function, without passing separately?
void (int x, int y, int result)
ilia on October 20, 2011:
This is so clear and easy to understand.Thanks a lot.
barkha on October 19, 2011:
pls explain c progrmming ??
raju karri on October 18, 2011:
the programm was gud and its a nice procedure
diksha on October 16, 2011:
sir can you please give some more examples of functions that will make it more understandable???
paras rajput on October 13, 2011:
nice it solve my problem
sandy on October 13, 2011:
m.ramadivya on October 11, 2011:
good for reading it
sanuj +91-9809600720 on October 08, 2011:
i want to know both Advantages and Disadvantages of C functions
jay on October 07, 2011:
how to develop the program swap the no without using variable?
pramod singh on October 06, 2011:
i want to know full discription abaut functions in c
kutubuddin azad on October 04, 2011:
in my exam, syllabus for coputer paper is functions of c, but my pages are lost of thus lesson and i was finding this lesson, so, i got it in internt, i am very happy today
mahmud on October 02, 2011:
parminder singh on September 28, 2011:
it is more efficient for knowledge
shruthi on September 26, 2011:
thanx for this help
RAJKISHOR SAHU (author) from Bangalore, Karnataka, INDIA on September 23, 2011:
@Chaithanya : logic looks like (any no.) * 8 + last digit of that (any no.)
1*8+1=9, here last digit is 1
12*8+2=98, here last digit is 2
Yogesh Ghariya on September 23, 2011:
thanx a lot!!!
Chaithanya on September 23, 2011:
i need a program for a calculation as follows
what's the logic please help me
nana on September 21, 2011:
pls how solve the infix to prefix & prefix evaluation
sushmita gupta on September 20, 2011:
I have a tc through which i execute all my c & c++ pgm.
I want to run Window base pgm through my tc compiler but it has not window.h header file.
How can i add that header file into my tc.
Please sir reply me.
rambo on September 18, 2011:
a good example
venkatesan on September 18, 2011:
hello brother is this enough to study c language?