C Programming Lesson - Types of Function
By rajkishor09
C Programming Language
In my previous C programming tutorial I tried to explain what function is, its advantages, how you can to declare a C function and call a function in your C program. And I told you that there are five types of functions and they are:
- 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.
1. Functions with no arguments and no return value.
A C function without any arguments means you cannot pass data (values like int, char etc) 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. This type of function which does not return any value cannot be used in an expression it can be used only as independent statement. Let’s have an example to illustrate this.
#include<stdio.h>
#include<conio.h>
void printline()
{
int i;
printf("\n");
for(i=0;i<30;i++)
{
printf("-");
}
printf("\n");
}
void main()
{
clrscr();
printf("Welcome to function in C");
printline();
printf("Function easy to learn.");
printline();
getch();
}
Output
Source Code Explanation:
The above C program example illustrates that how to declare a function with no argument and no return type. I am going to explain only important lines only because this C program example is for those who are above the beginner level.
Line 4-13: This C code block is a user defined function (UDF) whose task is to print a horizontal line. This is a simple function and a basic programmer can understand this. As you can see in line no. 8 I have declared a “for loop” which loops 30 time and prints “-” symbol continuously.
Line 15-23: These line are “main()” function code block. Line no. 18 and 20 simply prints two different messages. And line no. 19 and 21 calls our user defined function “printline()”. You can see output this program below
|
|
Microsoft Visual C# 2008 Step by Step, John Sharp, Excellent Book
Current Bid: $5.74
|
|
|
Programming Video Games for the Evil Genius by Valdean C. Lembke, Thomas E....
Current Bid: $7.50
|
|
|
C Programming Language (2nd Edition) (Prentice Hall Software)
Current Bid: $27.48
|
| No Photo |
Applications Programming in C++ by Martin Kalin, Ric...
Current Bid: $.95
|
2. Functions with arguments and no return value.
In our previous example what we have noticed that “main()” function has no control over the UDF “printfline()”, it cannot control its output. Whenever “main()” calls “printline()”, it simply prints line every time. So the result remains the same.
A C function with arguments can perform much better than previous function type. 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. Rather, it displays the result on the terminal. But we can control the output of function by providing various values as arguments. Let’s have an example to get it better.
#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(30,15);
add(63,49);
add(952,321);
getch();
}
Output
Source Code Explanation:
This program simply sends two integer arguments to the UDF “add()” which, further, calculates its sum and stores in another variable and then prints that value. So simple program to understand.
Line 4-9: This C code block is “add()” which accepts two integer type arguments. This UDF also has a integer variable “result” which stores the sum of values passed by calling function (in this example “main()”). And line no. 8 simply prints the result along with argument variable values.
Line 11-18: This code block is a “main()” function but only line no. 14, 15, 16 is important for us now. In these three lines we have called same function “add()” three times but with different values and each function call gives different output. So, you can see, we can control function’s output by providing different integer parameters which was not possible in function type 1. This is the difference between “function with no argument” and “function with argument”.
Functions with arguments and no return value.
- Call by Value and Call by Reference in C Programming
This C example is good example of Functions with arguments and no return value. Please have a look to understand this concept better.
3. 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. And 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.
#include<stdio.h>
#include<conio.h>
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();
}
Source Code Explanation:
This program sends two integer values (x and y) to the UDF “add()”, “add()” function adds these two values and sends back the result to the calling function (in this program to “main()” function). Later result is printed on the terminal.
Line No. 4-9: Look line no. 4 carefully, it starts with int. This int is the return type of the function, means it can only return integer type data to the calling function. If you want any function to return character values then you must change this to char type. In line no. 8 you can see return statement, return is a keyword and in bracket we can give values which we want to return. You can assign any integer value to experiment with this return which ultimately will change its output. Do experiment with all you program and don’t hesitate.
Line No. 11-21: In this code block only line no. 16, 17 and 18 is important. We have declared an integer “z” which we used in line no. 16. Why we are using integer variable “z” here? You know that our UDF “add()” returns an integer value on calling. To store that value we have declared an integer value. We have passed 952, 321 to the “add()” function, which finally return 1273 as result. This value will be stored in “z” integer variable. Now we can use “z” to print its value or to other function.
You will also notice some strange statement in line no. 17. Actually line no. 17 and 18 does the same job, in line no. 18 we have used an extra variable whereas on line no. 17 we directly printed the value without using any extra variable. This was simply to show you how we can use function in different ways.
Functions with arguments and return value.
- How to Reverse a Number?
In this tutorial we are going to learn reverse number program logic and how to reverse a number in C, C++, C-Sharp (C#) and java programming language. This programming example shows you the concept functions with arguments and return value.
4. 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. The best example of this type of function is “getchar()” library function which is declared in the header file “stdio.h”. We can declare a similar library function of own. Take a look.
#include<stdio.h>
#include<conio.h>
int send()
{
int no1;
printf("Enter a no : ");
scanf("%d",&no1);
return(no1);
}
void main()
{
int z;
clrscr();
z = send();
printf("\nYou entered : %d.", z);
getch();
}Functions with no arguments and return values.
Source Code Explanation:
In this program we have a UDF which takes one integer as input from keyboard and sends back to the calling function. This is a very easy code to understand if you have followed all above code explanation. So I am not going to explain this code. But if you find difficulty please post your problem and I will solve that.
5. Functions that return multiple values.
So far, we have learned and seen that in a function, return statement was able to return only single value. That is because; a return statement can return only one value. But if we want to send back more than one value then how we could do this?
We have used arguments to send values to the called function, in the same way we can also use arguments to send back information to the calling function. The arguments that are used to send back data are called Output Parameters.
It is a bit difficult for novice because this type of function uses pointer. Let’s see an example:
#include<stdio.h>
#include<conio.h>
void calc(int x, int y, int *add, int *sub)
{
*add = x+y;
*sub = x-y;
}
void main()
{
int a=20, b=11, p,q;
clrscr();
calc(a,b,&p,&q);
printf("Sum = %d, Sub = %d",p,q);
getch();
}
Source Code Explanation:
Logic of this program is that we call UDF “calc()” and sends argument then it adds and subtract that two values and store that values in their respective pointers. The “*” is known as indirection operator whereas “&” known as address operator. We can get memory address of any variable by simply placing “&” before variable name. In the same way we get value stored at specific memory location by using “*” just before memory address. These things are a bit confusing but when you will understand pointer then these thing will become clearer.
Line no. 4-8: This UDF function is different from all above UDF because it implements pointer. I know line no. 4 looks something strange, let’s have a clear idea of it. “Calc()” function has four arguments, first two arguments need no explanation. Last two arguments are integer pointer which works as output parameters (arguments). Pointer can only store address of the value rather than value but when we add * to pointer variable then we can store value at that address.
Line no. 10-17: When we call function “calc()” in the line no. 14 then following assignments occurs. Value of variable “a” is assigned to “x”, value of variable “b” is assigned to “y”, address of “p” and “q” to “add” and “sub” respectively. In line no. 6 and 7 we are adding and subtracting values and storing the result at their respective memory location. This is how the program works.
I tried to put things in simple for beginner but if you find it difficult to understand then please let me know through comments and I will try to change that part as soon as possible.
Help me to write better.
Did this help you?
See results without votingMore Article
- How to swap two numbers without using third (temp) variable
This article explains how we can swap variable values without using third variable. There are different ways to do so and I tried including all know methods. You can get code in C, Java and C# language. - How to get a 100% free website?
Before making this decision I feel that we should aware about some basic terms which are frequently used in internet world. Domain name: First of all what we ponder when we think about a website is the... - How to build ultimate and extreme gaming computers?
- File Copy Program in C Language
Today we are going to learn a simple file copy program in C language. As I said this is a simple file copy program so you should not expect its output like DOS copy command has. Ok lets start. The main... - A Brief History of the C Language
Before we start any complex program in C, we must understand what really C is, how it came into existence and how it differs from other languages of that time. In this tutorial I will try to talk about these... - Turn Your PC Keyboard to Musical Keyboard
To accomplish this task you dont need to buy any expensive software or hardware, only a little knowledge of C program will do and you can build your own musical keyboard software. Before we begin you...
Comments
please give more example
i got the required information.thank u
I really very happy when i see most of us information was there
thanks
really explained each and every type clearly thanks a lot it was really very useful
i get satisfactory information, which i want. really thanks.
thnx man you really help me
grt work man..........
excellent way to give a propre defintions on c language associated functions
great..........
give example for multiple values with out using pointer variable
hi really nice hub.............do u have source code for parser..............
hey!!! really awesome explanation..i was confused about functions before entering in to this site..thanks alot..
got the right staff sport on. Kudos
what a excellent explanation....its very useful to improve my knowledge.....thank u boss
thankzx a lot
really super explanation
Hey you explain very clearly and effectively about function. Function keeps main importance in C language. I simplifies program a lot.
C rocks. Just got to keep those pointers under control!
GREAT JOB....KEEP IT UP...
it is very easy to understand informations abouy c thnx a lot
easy to understand
nice
wow.. its so easy to understand.. how to program :)
very good
It is very useful to us.Thank you very much
does the gantt chart exist in this language?
Thanks for your explanation. it helps me to understand it.
btr dn a text book :)
thanks
Functions are explained clearly and with simple examples.
It helps in better understanding. Thanks.
can u give more examples???
Thanks for clear explanation
thanks for ur clear explanation
please,clear explanation in functions of c program
thank u i got the information
my problem is using switch functions, im counting the days between the entering the first month and last month
example
suppose im enter 2 in the first month
and 5 for the last month the solution must be
28+31+30+31+30
the output must be 120 and asking again the program if he/her try again
iam totally confused
example programs and explanation was very clear and easy to understand
well done.. thanks a lot.. finally, I can answer my homework! ^^
I really dont understand the function
In Fn. with arguments and no return type,how we take run time entries.
@shobin i think u want to take input from user, if so then u can use scanf within function as u do in main().
thanks dude
if we use scanf in function then it is fn.with arguments.
please explain this clearly;
#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(30,15);
add(63,49);
add(952,321);
getch();
}
sir,please tell this is right or wrong.
void add(int b,int c)
{
int a;
a=b+c;
cout
@shobin : when we declare function in following manner like "void add(int x, int y)" then "int x & int y" are parameter or arguments of "add" function. So, code inside function (like printf, scanf, for loop etc.) is not an arguments.
can u give more example
plz tell how can we get logics in c programs sir . plz explain me once plz
You are too good... keep it up.
thanks
hi raj,
after going through your function calls i decided to try it on my prog but its giving me error. Can u help out.
thanks.
#include
using namespace std;
char password(char pass)
{
pass='\0';
while (pass!='a')
{
cout
THANK YOU THIS HELPED ME A LOT
can you please try to give an example of c programing in functions and structure of program
really very helpful
very very usefull
What are such types of functions called?
the info was helpful.
this information helps me a alot....thnx..
thanks..........u have given a wonderful explaination about function.
Sir, could you help me about this program that calculate the distance between two points using the 3 functions. the scond function will calculate the distance bet x and y. the return values should be of type double. and the main fucntion will just call those functions that you've created. so here is the sample run:
enter two num : 1.2 1
you entered :1.2 and 1
distance between the two of them : 2
i almost forgot of how to program and this is the program that i've seen in my old notes before. kindly help me to solve this sample prog for me to refresh my ideas about programmings. thanks and more power\.
hear you soon...
ya its fine but give me some real exampels
kaka kamal kar diti
really super
i got great satisfaction after reading dis contents
Good Hub
you are right sneha...
i m totaly confused so plz explain clearly
thanks
help me out try a flames
I HAVE UNDERSTOOD IT CLEARLY.I LEARNT A NEW THING FROM IT.
its nice
can i ask What are the two types of Function in C?
thank u i got some clarification regarding this
what a explanation!!!!!!!!!!good.......i understand clearly.....Thank u soooooo much.........
very good explanation.........thanx alot for such a clear expalnation......
Its the best explaination for the userdefined functions.
Thanx
thanx dear;;;;;;;;;;;;;
thanks for giving detailed information
more explain more understanding level.
more perfect
very good code.simple and easy.keep going
this is simple function u have explained. so pls be ellaborate so that everyone can properly understand of function from a to z.
thnx a lot it really helped in my college project thnq sooooooooooo much.
its useful yaar...
ITS EASY
Thanks a lot, it's really helpfull
very very nice
The Explanation given for functions is really good & easy to understand also. Thanks for giving so good explanation about functions
give more examples
that's very best explanation in "c" language
good explanation.really helpful. thanks
kevvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
awosome work broo,,,,,,,
great job...... thanks alot...... now c became easy to me....
tnx...
Exercise#1 semi-finals
1)(Prime numbers)An integer is said to be prime it is divisible by only 1 and by itself.For example 2,3,5 & 7 are prime, but 4,6,8 & 9 are not.
a)Write a function that determines whether a number is prime.
b)Use the function in a program that determines all the prime numbers between 1 to 1000.Store all the results in an array and print them(in a neat table format).
2) (Bubble sort)in the bubble sort algorithm, smaller values gradually"bubble" their way to the top of the array like air bubbles rising in water, while the larger values sink to the bottom. The bubbles sort makes several passes through the array.On each pass, successive pairs of elements are compared. If a pair is increasing order ( or the values are identical), we leave the values as they are. If a pair in decreasing order, their values are swapped in the array.
While a program that sorts an array of 10 integers using bubble sort. Use the rand function to generate the 10 integers. Display the integers before and after applying the bubble sort. Sample display is;
3 1
2 2
1 3
7 4
5 5
6 6
4 7
10 8
9 9
8 10
3)Write a program that uses random number generation to create sentences.The program should use four arrays of pointers to char called article, noun, verb, & preposition. The program should create a sentence by selecting a word at random from each array in the following order:article, noun, verb, preposition, article & noun.
As each word is picked, it should be concatenated to the previous words in an array that is large enough to hold the entire sentence. The words should be separated by spaces, when the final sentence is output, it should start with the capital letter & end with a period.
The arrays should be filled as follows:the article array should contain the articles "the" "a","one","some" & "any" the noun array should contain the nouns "boy","girl","dog","town" & "car" the verb array should contain the verbs "drove","jumped","rain","walked"&"skipped" the preposition array should contain the preposition "to,"form","over","under" & "on".
After completing the program, modify it to produce a short consisting of several of these sentences.Each paragraph should consist of 5 sentences Use a random generator to display 3 to 5 paragraph.Use array of pointers to char for the paragraphs as well...
Thanks so much...
take care always....
I loved Functions that return multiple values. I read it few other places but this was the best and easiest. Thank you.
I GOT VERY MUCH HELP FROM THIS
THANXXXXXXXXXXXXXXXX................
hi?
can you help me out of my excercises ?
i get so .. gggrr !
tnx .. mhuaah
plssss ..
there is a example, Structure of a program
// my first program in C++
This is a comment line. All lines beginning with two slash signs (//) are considered comments and do not have any effect on the behavior of the program. The programmer can use them to include short explanations or observations within the source code itself. In this case, the line is a brief description of what our program is.
#include
Lines beginning with a hash sign (#) are directives for the preprocessor. They are not regular code lines with expressions but indications for the compiler's preprocessor. In this case the directive #include tells the preprocessor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program.
using namespace std;
All the elements of the standard C++ library are declared within what is called a namespace, the namespace with the name std. So in order to access its functionality we declare with this expression that we will be using these entities. This line is very frequent in C++ programs that use the standard library, and in fact it will be included in most of the source codes included in these tutorials.
int main ()
This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function.
The word main is followed in the code by a pair of parentheses (()). That is because it is a function declaration: In C++, what differentiates a function declaration from other types of expressions are these parentheses that follow its name. Optionally, these parentheses may enclose a list of parameters within them.
Right after these parentheses we can find the body of the main function enclosed in braces ({}). What is contained within these braces is what the function does when it is executed.
cout
@sheena, i will help you, you can contact me for any help..
U have clearly explained about the categoreis of functions.
Thank u very much.....
How to write down a funtion definition :
A function called bilbo accepts a character and returns an integer value ?
a function called frodo which accepts two integer values and returns a character
a function called gamcho which accepts three characters and returns a long integer
a function called memo accepts a long integer and returns a short integer
a function called poloko accepts two double precision quantities and return nothing
Can anyone help me to solve this ?
@ash : those function will have any logic or just prototype (structure) for whatever you are saying?
can u the solution to find out prime no using function?
It very use full.
u have not use pointer while passing argument how they will send
I got a function definition something like
sum (a,b)
int a, b
{
int sum;
sum=a+b;
}
It is working...but i don't know how..??plz describe this...
thanks
thanks for providing beautifull and helpfull notes
programming is good.
super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super
super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super supersuper super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super supersuper super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super
super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super supersuper super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super
super
u r jst awesome yaaaaaarrrrrrrrrr'
thnx......... see u soon
nice explanation got an idea about functions but want some more examples
You are given a very nice job
in c language main is which function
your explanation is good,but you doesn't specify about function prototype and function definition if u explain them in a good way with few examples your notes will be excellent try to improve it.
CAN u Tell Me the BasIc book 4 the Guidance of c language????
types of user defined functions explanation is superb. given information is easily understood by the begeners also
i am clearewd sbout the functions right now thank u sir very much
Great Explanation, can't go simple than this
gud work
Best explanation for knowing concept....ThanX
yupiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
thnx
thank you
Wow it is easy to understand but for better understand i need more example
totally awesome.
.......................................................................................................nice hub
Thanks A lot friend really very Helpfull website.
what is the basic difference in the functions which one return a value and second which not return any value ...
but give the same result like
void add1()
{
int a=4+5;
}
int add2()
{ int a=4+5;
return a;
}
both add1 and add2 both return same result than what is use of return type????
@deepak : how you will retrieve result of add1 in main function?
I've never seen such a detailed explanation anywhere.its really a fantastic job done by you to help us understanding the functions concept in c! Thanks! keep it up!
sir, your notes is very helpful to us
Thanks @sangobingo...
Thank You
``



latha 2 years ago
You clearly explained about function calls. Thanks a lot. if possible Please post topics on data structures in c also.