Handling of Functions by Sachin Mehta SignUp
Boloji.com

Channels

In Focus

 
Analysis
Cartoons
Education
Environment
Opinion
Photo Essays
 
 

Columns

 
A Bystander's Diary
Business
Random Thoughts
 
 

Our Heritage

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

Society & Lifestyle

 
Health
Parenting
Perspective
Recipes
Society
Teens
Women
 
 

Creative Writings

 
Book Reviews
Computing
Ghalib's Corner
Humor
Individuality
Literary Shelf
Love Letters
Memoirs
Quotes
Stories
Travelogues
Workshop
 
 
Computing 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.  

Share This:
23-Sep-2001
More by :  Sachin Mehta
 
Views: 1817      Comments: 0




Name *
Email ID
 (will not be published)
Comment *
Characters
Verification Code*
Can't read? Reload
Please fill the above code for verification.
 
Top | Computing



 
 
 
 
 
 
 
2018 All Rights Reserved
 
No part of this Internet site may be reproduced without prior written permission of the copyright holder
.