Handling of Functions by Sachin Mehta SignUp
Boloji.com
Boloji
Home Kabir Poetry Blogs BoloKids Writers Contribute Search Contact Site Map Advertise RSS Login Register
Boloji
Channels

In Focus

Analysis
Cartoons
Education
Environment
Going Inner
Opinion
Photo Essays

Columns

A Bystander's Diary
Business
My Word
PlainSpeak
Random Thoughts

Our Heritage

Architecture
Astrology
Ayurveda
Buddhism
Cinema
Culture
Dances
Festivals
Hinduism
History
People
Places
Sikhism
Spirituality
Vastu
Vithika

Society & Lifestyle

Family Matters
Health
Parenting
Perspective
Recipes
Society
Teens
Women

Creative Writings

Book Reviews
Ghalib's Corner
Humor
Individuality
Literary Shelf
Love Letters
Memoirs
Musings
Quotes
Ramblings
Stories
Travelogues
Workshop

Computing

CC++
Computing Articles
Flash
Internet Security
Java
Linux
Networking
Computing Articles Share This Page
Handling of Functions
by Sachin Mehta Bookmark and Share
 

We have seen that C functions by default return int value. This is obvious to raise a question , whether there are other return types for functions. Yes there are, not only the basic data types like int, float, char but also user-defined types like structures and arrays.  

For allowing other than the int return types we have the type-specifier for functions. This will help you  to return other return types. We must do two things to enable a calling function to receive a non-integer value from a called function: 

( NOTE: The following explanations are in accordance to the older method of C functions, which doesn't declare function arguments on the same line as the function header. The modern or ANSI  method declares the function on the same line as function header. There are also a few other subtle but important distinctions in the two methods. The latter method we will see later.)                        
1. The explicit type-specifier, corresponding to the data type required must be mentioned in the function header. The general form of the function definition is

 type-specifier  function-name (argument list)
 argument declaration;
{
 function statements;
}
The type-specifier tells the compiler, the type of data type the function is to return.

2. The called function must be declared at the start of the body in the calling function, like any other variable. This is to tell the calling function the type of data that the function is actually returning.

For example consider the following segment of program
main()
{
   float x,a,b, mul();              //function declaration
  double  y, div();                //function declaration
  //Program statements
  x = mul(a,b);                   //function call
  y = div(a,b);                   //function call
}

float mul(t,e)                    //function definition
float t,e;
{
  // Body
}

double div(i,o)               //function definition
float i,o;
{
  //body
}

The declaration part of main function declares not only the variables but the functions mul and div as well. This only tells the compiler that mul return a float-type value and div a double-type value. Parentheses that follow mul and div specify that they are functions instead of variables. 

If we have a mismatch between the type of data that the called function returns and the type of data that the calling function expects, we will have unpredictable results. We must, therefore be careful to make sure that both types are compatible.

In cases when the function doesn't return any values, in that situation we specify the return type as void. This  has to be done in the declaration of the function in the main as well in the function header.  

We will continue with few other function handling concepts  and start on the topic of variables in functions next time.  

23-Sep-2001
More by :  Sachin Mehta
 
Views: 1569
 
Top | Computing Articles







A Bystander's Diary Analysis Architecture Astrology Ayurveda Book Reviews
Buddhism Business Cartoons CC++ Cinema Computing Articles
Culture Dances Education Environment Family Matters Festivals
Flash Ghalib's Corner Going Inner Health Hinduism History
Humor Individuality Internet Security Java Linux Literary Shelf
Love Letters Memoirs Musings My Word Networking Opinion
Parenting People Perspective Photo Essays Places PlainSpeak
Quotes Ramblings Random Thoughts Recipes Sikhism Society
Spirituality Stories Teens Travelogues Vastu Vithika
Women Workshop
RSS Feed RSS Feed Home | Privacy Policy | Disclaimer | Site Map
No part of this Internet site may be reproduced without prior written permission of the copyright holder.
Developed and Programmed by ekant solutions