Updated date:

Types of Function in C Programming Language

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.

how-to-work-with-multidimensional-array-in-c-programming

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:

  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 that return multiple values.
  5. 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();
}
Logic of the functions with no arguments and no return value.

Logic of the functions with no arguments and no return value.

Output

Output of above program.

Output of above program.

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

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();
}
Logic of the function with arguments and no return value.

Logic of the function with arguments and no return value.

Output

Output of above program.

Output of above program.

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 an 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.

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();
}
Logic of the function with arguments and return value.

Logic of the function with arguments and return value.

Output of the above program.

Output of the above program.

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.

types-of-function-in-c-programming-languages
Output of the above program.

Output of the above program.

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();
}
Output of the above program.

Output of the above program.

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.

More 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.
  • 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...

© 2009 RAJKISHOR SAHU

Comments

Vivek-Koshy Thomas Poovathoor from San Antonio, TX on August 05, 2017:

I'm using the same book! Despite it's age I love it! I really appreciate the effort put inside this hub, keep it up please. Thank you so much!

AP.PRAKASH.(SOFTWARE ENGINEER) on April 24, 2017:

THIS C PROGRAMMING IS AVERY USEFUL IS A IMPROVEMENT SOFTWARE IMPROVEMENT

Pooja on February 15, 2016:

Sir your notes is very useful

m iftekhar ul haque on December 09, 2015:

really helpful thank you !!

umeee on September 10, 2015:

C stands for what in c language.....?any body....

john on January 13, 2015:

awsome

Julian on April 20, 2014:

Thank you for most informative acticle. What I would like to see is an article that describe all of the built-in library fuction that come with C or C++. I would like each function in the same format as a very antiquated programming language - DBASE VI.

raj on December 11, 2013:

its very useful for me.

saam on August 04, 2013:

thanks.................................................................................................a lot

Vishal Narayanan from Hosur, Karnataka, India on May 11, 2013:

sir really u are great . i understood clear cut. it was perfect hats off. really cant go simple than this .

jani on February 22, 2013:

really helpful thank you !!

vipul on January 25, 2013:

thanks boss

nandini on December 27, 2012:

nice i can understand clearly

RAJKISHOR SAHU (author) from Bangalore, Karnataka, INDIA on December 11, 2012:

thank you guys......

Kalaiselvam N on December 08, 2012:

nice....easily understandable from diagram itself

priya on November 02, 2012:

It is really useful page for students

RAJKISHOR SAHU (author) from Bangalore, Karnataka, INDIA on October 25, 2012:

deep: thanks..... :)

deep on October 20, 2012:

thanks buddy.......its really good

Dhaval on October 18, 2012:

How can i set the username or password in C

sayeed khan on July 29, 2012:

it is the vary good explation..........

Himanshu Bakshi on July 26, 2012:

Raj Kishore, I am very greatful to you. You did really amazing job. High Recommend Sir...

vam on July 12, 2012:

need some more examples mr.kishor

RAJKISHOR SAHU (author) from Bangalore, Karnataka, INDIA on July 07, 2012:

thanks @Sonam

sonam on July 05, 2012:

sir

ur notes is good..........

Anonymous Programmer on May 07, 2012:

Thank You

``

RAJKISHOR SAHU (author) from Bangalore, Karnataka, INDIA on April 30, 2012:

Thanks @sangobingo...

Pramod on April 30, 2012:

sir, your notes is very helpful to us

sangobingo on April 30, 2012:

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!

RAJKISHOR SAHU (author) from Bangalore, Karnataka, INDIA on April 19, 2012:

@deepak : how you will retrieve result of add1 in main function?

deepak kumar bansal on April 19, 2012:

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????

vivek on March 23, 2012:

Thanks A lot friend really very Helpful website.

sound on March 05, 2012:

.......................................................................................................nice hub

sandeep on February 17, 2012:

totally awesome.

jay on January 26, 2012:

Wow it is easy to understand but for better understand i need more example

mmmmm on January 17, 2012:

thank you

jk patel on January 12, 2012:

yupiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii

thnx

$onam on January 04, 2012:

Best explanation for knowing concept....ThanX

ameer ali khoso on December 31, 2011:

gud work

Dr. Debugger on December 30, 2011:

Great Explanation, can't go simple than this

razaatc from lahore on December 30, 2011:

i am clearewd sbout the functions right now thank u sir very much

santosh mca on December 30, 2011:

types of user defined functions explanation is superb. given information is easily understood by the begeners also

Abdullah on December 08, 2011:

CAN u Tell Me the BasIc book 4 the Guidance of c language????

mohan on November 30, 2011:

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.

sanga on November 28, 2011:

in c language main is which function

C Teacher on November 23, 2011:

You are given a very nice job

yogitha on November 23, 2011:

nice explanation got an idea about functions but want some more examples

ashvin vd on November 22, 2011:

u r jst awesome yaaaaaarrrrrrrrrr'

thnx......... see u soon

gg on November 16, 2011:

super

senthil on November 16, 2011:

super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super 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

uma on November 16, 2011:

super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super

nico on November 11, 2011:

programming is good.

shekhar on November 10, 2011:

thanks for providing beautifull and helpful notes

tarun gandhi on November 02, 2011:

thanks

Rahul S on October 30, 2011:

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...

bunty on October 28, 2011:

u have not use pointer while passing argument how they will send

Indhu on October 21, 2011:

It very use full.

suman.srestha on October 18, 2011:

can u the solution to find out prime no using function?

RAJKISHOR SAHU (author) from Bangalore, Karnataka, INDIA on October 09, 2011:

@ash : those function will have any logic or just prototype (structure) for whatever you are saying?

ash on October 09, 2011:

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 ?

keerthi.M on October 01, 2011:

U have clearly explained about the categoreis of functions.

Thank u very much.....

RAJKISHOR SAHU (author) from Bangalore, Karnataka, INDIA on September 28, 2011:

@sheena, i will help you, you can contact me for any help..

caglar betos on September 26, 2011:

there is an 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 on September 26, 2011:

hi?

can you help me out of my excercises ?

i get so .. gggrr !

tnx .. mhuaah

plssss ..

TARUN on September 25, 2011:

I GOT VERY MUCH HELP FROM THIS

THANXXXXXXXXXXXXXXXX................

Ali on September 25, 2011:

I loved Functions that return multiple values. I read it few other places but this was the best and easiest. Thank you.

junangao on September 17, 2011:

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....

Joseph Kate Sobrio on September 15, 2011:

tnx...

suresh on September 13, 2011:

great job...... thanks alot...... now c became easy to me....

ali rizviiii on September 13, 2011:

awosome work broo,,,,,,,

jessi on September 02, 2011:

kevvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

urmila pilania on September 01, 2011:

good explanation.really helpful. thanks

pradeep mishra on August 30, 2011:

that's very best explanation in "c" language

vishnu on August 26, 2011:

give more examples

pratik vaishya on August 19, 2011:

The Explanation given for functions is really good & easy to understand also. Thanks for giving so good explanation about functions

mohan krishna on August 19, 2011:

very very nice

Amilton on August 17, 2011:

Thanks a lot, it's really helpful

SWEETY on August 13, 2011:

ITS EASY

priyaa on August 13, 2011:

its useful yaar...

swetha on August 11, 2011:

thnx a lot it really helped in my college project thnq sooooooooooo much.

abracadabra on August 07, 2011:

this is simple function u have explained. so pls be ellaborate so that everyone can properly understand of function from a to z.

poonam on July 05, 2011:

very good code.simple and easy.keep going

chandan kumar on May 24, 2011:

more explain more understanding level.

more perfect

tahir khan on April 15, 2011:

thanks for giving detailed information

NED on April 09, 2011:

thanx dear;;;;;;;;;;;;;

Ruchika on March 24, 2011:

Its the best explaination for the userdefined functions.

Thanx

mini on February 22, 2011:

very good explanation.........thanx alot for such a clear expalnation......

sharan on February 14, 2011:

what a explanation!!!!!!!!!!good.......i understand clearly.....Thank u soooooo much.........

C.SUHANA AMBRIN on February 14, 2011:

thank u i got some clarification regarding this

bebz on February 03, 2011:

can i ask What are the two types of Function in C?

keerthi k.p on January 22, 2011:

its nice

ANU on January 22, 2011:

I HAVE UNDERSTOOD IT CLEARLY.I LEARNT A NEW THING FROM IT.

^^ on January 16, 2011:

help me out try a flames

hemant on January 11, 2011:

thanks

madiha on January 11, 2011:

i m totally confused so plz explain clearly

Ambily on January 10, 2011:

you are right sneha...

Mousumi Paul on January 09, 2011:

Good Hub

sneha on January 06, 2011:

i got great satisfaction after reading dis contents

saran on January 01, 2011:

really super

jatinder on December 29, 2010:

kaka kamal kar diti

brundha on December 27, 2010:

ya its fine but give me some real exampels