FunctionsAuthor : Pandu Ranga Rao N
E - Mail : pandu2050@gmail.com

A function is a sub - program which contains block of statements which is required to be called many times in a program. The function has got three states, namely,

  1. Function Prototype
  2. Function Definition
  3. Function Call
  • Prototype declaration :
  • A function prototype determines how the function should be. It is used only by the compiler.

    The general format of declaring prototype for a function is,


    [Return-Type] [Function-Name]([Arguments]);

    A function prototype declaration has 3 major parts. They are,

    1. Function name
    2. Argument list
    3. Return type

    The [Function-Name] is a name given to a function by the user. It usually determine the type of task, the function is going to perform when it is called.

    The [Arguments] consist of list of arguments which contains the arguments to be passed to the function, when it is being called. The function uses it to perform a specific task. It is not compulsory to include arguments in argument list.

    The [Return-Type] determines the type of value should a function to return.

  • Defining a function what to do :
  • The function definition contains a block of the code, which defines the task to be performed by that function.

    The general format for defining a function is,


       [Return-Type] [Function-Name]([Arguments])
       {
         [Statements];
         return([Return-Value]);
        }

    Function definition takes the value passed to it by the caller function and performs the tasks given in the [statements]. The return statement will return the result to the caller function.

  • Calling a function :
  • To perform the defined task, the function must be called and it can be called anywhere. Any functions can be called within another functions also.

    The general format of Function call is,


    [Variable-Name] = [Function-Name]([Arguments]);

    We can call a function anywhere in the program. When we want to call a function in the program, we first determine type of value, the function will return and the number of arguments, which the function can take and specify that, in the function call.

    If a function has no arguments to pass, then void must be specified as argument, which means, the function without any arguments. In the same way, the return type may also contain void.

Now, Let us see a program, which lets the user to give input two values and passes these values to the function add, which we will assign the task of adding those two values. The function add finally gives the added value as return value, which is printed on the screen, as an output.


#include <stdio.h>
int add(int,int); /* Prototype. */
void main(void)
{
 int a,b,sum;
 printf("Enter two numbers : ");
 scanf("%d%d",&a,&b);
 sum = add(a,b); /* Function call */
 printf("%d",sum);
}

int add(int x, int y) /* Function definition */
{
 z = x + y;
 return z;
}

 


Functions do play the basic fundamental role in C. In C for printing the values on the screen and reading values from keyboard, there are two statements, printf, and scanf. They are all really functions!, not statements. Even the main() is a function!!!... C looks for and detects function main() while starting executing a program. It then, goes on executing the rest of the code, line by line. Whenever it sees function call, program execution jumps to that function definition and comes back with any return value and continues the execution.

The aim of function is to give modularity in the programs. Very large, very complex programs can be easily written, if we divide them into smaller functions.

Do you know?... The so far used statements printf() and scanf() are also functions!!!...


Author : Pandu Ranga Rao N
E - Mail : pandu2050@gmail.com
 
 
 
 
 
 
  About Team ProgrammingBasics.In

Contact Team ProgrammingBasics.In

Instructions for submitting your own article
 
 
X

About Team ProgrammingBasics.In

Right now, we are a small but, dedicated team who strived day and night to alter the pretty old programmingbasics.in into a new conceptual website "The ProgrammingBasics Foundation Library".

The articles and topics in the website are the major contributions of freelancers, programmers across the globe. We thank each of them for their valuable contributions. You can also become a contributor by sending some software related articles to us. We will publish it in our website. Instructions for the same are given in the "Instructions for submitting your own article" page.

Contact Us

ICan Technologies,
HIG 44, 23rd Cross, 8th Main,
E-Block, Vijayanagar 3rd Stage
Mysore-570017, Karnataka, India

Website: www.icantech.in
Mobile: +918050239039

Instructions for submitting your own article

Want to become an article contributor to this site ?

If yes, then send us an article written by you on your own words, neatly typed in MS Word document. If we feel that, you have written the article by using some other article then we will reject the article. Every article we will cross verify, before we publish them. The right to reject the article is with us.

If you want to publish your name and email id's, specify the same in the document.

NOTE: You must give your consent in the email for publishing your email id and your name. Otherwise, your name and email-ids will not get published. We respect your privacy.

One article per document, which means, you must not send more than one article in one document. Everything in the sent document will be treated as one article and your article will get rejected without looking in, further.

The articles must and should be related to computer basics, programming concepts and programming languages. We will not entertain any other topics. Please bear this in your mind, before starting an article write-up.

Articles published in ProgrammingBasics.in are the sole property of ICan Technologies, Mysore. You are here in authorizing ICan Technologies, Mysore to take the ownership of the published article. Rejected articles will not be published and you can try to publish the same in other sites. We will inform you the status of your submitted article within 10 working days, from the date of sent mail. If not responded within 10 days, then you are free to try publishing your article on some other websites. We will simply discard them and will not publish them any further. Once we publish the article, you will not be allowed to publish as it is, in some other website. If we find it, then your article will be removed from site and the removal will be informed to you.

Send your articles to programmingbasics.info@gmail.com with "Article for Publishing" in the subject line.

You must create your own images in PSD (Photoshop Document) only, which is to be supported in CS3 or lower versions. Any animated stuffs, must be in fla files (Flash editable file) and not in any other format and must be readable in Falsh 8.0.

You are now in