A function is a module or block of program code which deals with a particular task. Making functions is a way of isolating one block of code from other independent blocks of code. Functions serve two purposes. They allow a programmer to say:"this piece of code does a specific job which stands by itself and should not be mixed up with anything else", and they make a block of code reusable since a function can be reused in many different contexts without repeating parts of the program text. In previous modules, main( ) itself is a pre-defined function.
Functions help us to organize a program in a simple way
Each function has a name or identifier by which is used to refer to it in a program. A function can accept a number of parameters or values which pass information from outside, and consists of a number of statements and declarations, enclosed by curly braces { }, which make up the doing part of the object. The declarations and `type of parameter' statements are formalities which will be described in good time.
Some Rules for writing functions:
The name of a function in C can be anything from a single letter to a long word. The name of a function must begin with an alphabetic letter or the underscore _ character but the other characters in the name can be chosen from the following groups:
a .. z (any letter from a to z)
A .. Z (any letter from A to Z)
0 .. 9 (any digit from 0 to 9)
_ (the underscore character)
Functions help us to organize a program in a simple way
Each function has a name or identifier by which is used to refer to it in a program. A function can accept a number of parameters or values which pass information from outside, and consists of a number of statements and declarations, enclosed by curly braces { }, which make up the doing part of the object. The declarations and `type of parameter' statements are formalities which will be described in good time.
Some Rules for writing functions:
The name of a function in C can be anything from a single letter to a long word. The name of a function must begin with an alphabetic letter or the underscore _ character but the other characters in the name can be chosen from the following groups:
a .. z (any letter from a to z)
A .. Z (any letter from A to Z)
0 .. 9 (any digit from 0 to 9)
_ (the underscore character)
Uses of C functions:
- C functions are used to avoid rewriting same logic/code again and again in a program.
- There is no limit in calling C functions to make use of same functionality wherever required.
- We can call functions any number of times in a program and from any place in a program.
- A large C program can easily be tracked when it is divided into functions.
- The core concept of C functions are, re-usability, dividing a big task into small pieces to achieve the functionality and to improve understandability of very large C programs.
C function declaration, function call and function definition:
There are 3 aspects in each C function. They are,- Function declaration or prototype - This informs compiler about the function name, function parameters and return value’s data type.
- Function call – This calls the actual function
- Function definition – This contains all the statements to be executed.
{ Body of function; }
#include// function prototype, also called function declaration float square ( float x ); // main function, program starts from here int main( ) { float m, n ; printf ( "\nEnter some number for finding square \n"); scanf ( "%f", &m ) ; // function call n = square ( m ) ; printf ( "\nSquare of the given number %f is %f",m,n ); } float square ( float x ) // function definition { float p ; p = x * x ; return ( p ) ; }
Output: Enter some number for finding square 2 Square of the given number 2.000000 is 4.000000