ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software

Function Call by Name in C Programming Language

Updated on June 18, 2012

C Language

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.

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);
}

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

#include<stdio.h>
#include<conio.h>

void myFunction();
int add(int, int);

void main()
{
	clrscr();

	myFunction();
	printf("\n\n%d",add(10,15));

	getch();
}

void myFunction()
{
	printf("This is inside function :D");
}

int add(int a, int b)
{
	return a+b;
}

Explanation

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:

  1. 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.
  2. The length of the source program can be reduced by using functions at appropriate places.
  3. It becomes uncomplicated to locate and separate a faulty function for further study.
  4. 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.
  5. 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:

  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.

Note : You can find details about different types of function in C language, click here read it now.

Example of function calling in C

#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

Output of above program.
Output of above program.

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()” . 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.

Share your opinion with me

Did this tutorial helped you to learn function in C language?

See results

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      DoveFreexrolo 17 months ago

      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.

    • profile image

      nayanshill@2016 20 months ago

      all c++ 2016

    • profile image

      Usha K S 3 years ago

      I Cleared my doubts after view'd ur site....

      THANK U...

    • profile image

      alpa alok 4 years ago

      super.........................................................................................................

    • profile image

      Nu Dombkowski 4 years ago

      [quote]8192 characters left.[/quote]

      Good write ups, With thanks..

      Nicely put, Thanks a lot.

    • profile image

      Rono Dominic 4 years ago

      What is the difference between calling a function by value and by reference?

    • profile image

      pihu 4 years ago

      thanks, it help me to understand function . but give more example for better understanding

    • profile image

      aashi 4 years ago

      0

    • profile image

      mah 4 years ago

      what is the differece between formal and actual arguments?

    • profile image

      faisal 4 years ago

      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

    • profile image

      fredfmah 4 years ago

      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

      executed.

      sequence then is

      0. Initiate

      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

      Fred

      Madrid, Spain

    • profile image

      rocky 4 years ago

      thank you so much...

    • profile image

      rainbow sky 5 years ago

      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.

      Thanks

    • profile image

      Megha 5 years ago

      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

    • profile image

      bhavna 5 years ago

      sir i ask

    • profile image

      Rupesh Kumar 5 years ago

      its verry-verry helpful for me.....

    • profile image

      vijay 5 years ago

      good explanation

    • rajkishor09 profile image
      Author

      RAJKISHOR SAHU 5 years ago from Bangalore, Karnataka, INDIA

      If you want you can check types of function tutorials also....

    • profile image

      himanshu gehani 5 years ago

      that's call function definition.Very good function describe and give the examples.

    • profile image

      abdul salam 5 years ago

      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.

      thank you.

    • profile image

      aiyein 5 years ago

      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!

    • profile image

      indresh bind 5 years ago

      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.

    • profile image

      hera 5 years ago

      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 input:75,95,85

      sample out put:joli got the higest number.

    • profile image

      hera 5 years ago

      its so nice

    • profile image

      heena 5 years ago

      i want to print a rectangle using functions and for loop in c...any help please

    • profile image

      piusraiser 5 years ago

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

      Example pattern

      If the user input nepal than the output should be as follow

      1N

      2EE

      3PPP

      4AAAA

      5LLLLL

    • profile image

      hussainbutt 5 years ago

      Thank you this is really very helpful for me. Thanks again.

    • profile image

      daman bakhariya 5 years ago

      very good who have posted this

    • profile image

      bipin stha 5 years ago

      differentiate between call by value and call by referance while calling a function

    • profile image

      rogelio 5 years ago

      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.

    • profile image

      shalaka gharade 5 years ago

      thanks dude

    • profile image

      Rahul Rawat 5 years ago

      thanks sir it was amazing.

    • profile image

      sandeep 5 years ago

      its very helpful for me thanks

    • profile image

      ankita gupta 5 years ago

      it is very useful thanx to give us brief knowledge

    • profile image

      Banker 5 years ago

      code for the print the longest word written in a line using udf...plzz reply fast...

    • profile image

      TANUJA 5 years ago

      SOMEWHAT CLEAR

    • profile image

      madura bharathi 5 years ago

      no comment

    • profile image

      vineela 5 years ago

      we can know more new things in c-programs

    • rajkishor09 profile image
      Author

      RAJKISHOR SAHU 5 years ago from Bangalore, Karnataka, INDIA

    • profile image

      shivani 5 years ago

      m asking 1 que.....

      what is different betbeen call by reference and call by result value

    • rajkishor09 profile image
      Author

      RAJKISHOR SAHU 5 years ago from Bangalore, Karnataka, INDIA

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

    • profile image

      now refreshed pravin 5 years ago

      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..praveenvenugopal24@yahoo.com...plzzzzzzz...

    • rajkishor09 profile image
      Author

      RAJKISHOR SAHU 5 years ago from Bangalore, Karnataka, INDIA

      @Biplab: You can find that information here https://hubpages.com/technology/Types-of-Function-...

    • profile image

      biplab 5 years ago

      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

    • profile image

      sunil nirmalkar 5 years ago

      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 :- sunilnirmalkar@gmail.com

    • profile image

      abc 5 years ago

      plz sir explain need of user defined function

    • profile image

      Tanu sharma 5 years ago

      sir plz will u explaun the program of recurrsion plz sir i cnt get tht program

    • profile image

      trishul 5 years ago

      please explain the program, how the execution will done:

      int main()

      {

      int c=printf("hello");

      return 0;

      exit(10);

      }

    • profile image

      Rajesh 5 years ago

      hi, mr.raj

      am asking: 1. what is prototype?

      can explain other way plz.

      2. call by value,call by reference ?

    • profile image

      Nancy 5 years ago

      thanks a lot.

    • profile image

      priya 5 years ago

      hi,

      its very useful...

    • profile image

      jessy 5 years ago

      Hi,some doubts..

      why do u write getch() in void main()?

      How do u realize whether your function should return a value?

      Thanks..

    • rajkishor09 profile image
      Author

      RAJKISHOR SAHU 5 years ago from Bangalore, Karnataka, INDIA

      @Kishan Patel : factorial program is already there, visit https://hubpages.com/technology/C-Programming-Recu...

    • profile image

      kishan patel 5 years ago

      hiiiiii sir,you are awesome but can you give me a program to find the factorial of a number,please post it fast.................

    • profile image

      kishan patel 5 years ago

      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

    • profile image

      Asif Raza 5 years ago

      bundle of thanks ,i have learnt much of functions

    • profile image

      aravind 5 years ago

      how to pass the characters using function..(without using pointers)

    • profile image

      A/razak haji 5 years ago

      My problems in c function are solved now

    • profile image

      dinu(baby) 5 years ago

      thanx, it's good using function.

    • profile image

      Hi.. mukesh kumar from varanasi 5 years ago

      It is a good progrram using function.

    • profile image

      M Junaid Awan 5 years ago

      good job man....

    • profile image

      soham 5 years ago

      thnx man...saved my ass before my semester.! :)

    • profile image

      nag 5 years ago

      thank you

    • profile image

      Ron 5 years ago

      thnks for info

    • profile image

      nek yadav 5 years ago

      thanx dude u are a great teacher. my whole problems are solved now in function.

    • profile image

      ashutoshmishra15@yahoo.in 5 years ago

      what are the type conversions ?explain with the help of an example?

    • profile image

      renuka 5 years ago

      sir please explain deeply about function prototype and finction defination

    • profile image

      rohit 5 years ago

      sir plz give me details fuction in c for collage seminar

    • profile image

      aruna 5 years ago

      5

      5 5

      5 5 5

      plz send me d 'c' code for the above problem

    • profile image

      ankur 5 years ago

      what is the program of multiplication in function

    • profile image

      nk 5 years ago

      thanks

    • profile image

      maneesh dwivedi 5 years ago

      thank u sir i am very happy to get such help by ur programs

    • profile image

      Balaji Naidu 5 years ago

      will u please post the program explaining using function inside a function.......which explains clearly....pls do favour....

    • profile image

      gaurav nigam 5 years ago

      thanks.....

    • profile image

      dip 5 years ago

      i'm realy thankful to u.

    • profile image

      akasha 5 years ago

      dear sir!

      i m so much happy for your nice procedure in c.thanks ......

    • profile image

      shah 5 years ago

      hi sir

      i really understand from this description. so thnx dear

    • profile image

      EMAN 5 years ago

      Thanx for providing guideeness

    • profile image

      elanr 5 years ago

      Can we pass "result" as interger as argument inside the add function, without passing separately?

      eg;

      void (int x, int y, int result)

    • profile image

      ilia 5 years ago

      This is so clear and easy to understand.Thanks a lot.

    • profile image

      barkha 5 years ago

      pls explain c progrmming ??

    • profile image

      raju karri 5 years ago

      the programm was gud and its a nice procedure

    • profile image

      diksha 5 years ago

      sir can you please give some more examples of functions that will make it more understandable???

    • profile image

      paras rajput 5 years ago

      nice it solve my problem

    • profile image

      sandy 5 years ago

      thnks

      c

    • profile image

      m.ramadivya 5 years ago

      good for reading it

    • profile image

      sanuj +91-9809600720 5 years ago

      i want to know both Advantages and Disadvantages of C functions

    • profile image

      jay 5 years ago

      how to develop the program swap the no without using variable?

    • profile image

      pramod singh 5 years ago

      i want to know full discription abaut functions in c

    • profile image

      kutubuddin azad 5 years ago

      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

    • profile image

      mahmud 5 years ago

      fantastic

    • profile image

      parminder singh 5 years ago

      it is more efficient for knowledge

    • profile image

      shruthi 6 years ago

      thanx for this help

    • rajkishor09 profile image
      Author

      RAJKISHOR SAHU 6 years ago from Bangalore, Karnataka, INDIA

      @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

    • profile image

      Yogesh Ghariya 6 years ago

      thanx a lot!!!

    • profile image

      Chaithanya 6 years ago

      hi,

      i need a program for a calculation as follows

      1*8+1=9

      12*8+2=98

      .

      .

      .

      123456789*8+9=987654321

      what's the logic please help me

    • profile image

      nana 6 years ago

      pls how solve the infix to prefix & prefix evaluation

    • profile image

      sushmita gupta 6 years ago

      Hello Sir,

      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.

    • profile image

      rambo 6 years ago

      a good example

    • profile image

      venkatesan 6 years ago

      hello brother is this enough to study c language?