C- Functions

WHAT IS C FUNCTION?
A large C program is divided into basic building blocks called C function. C function contains set of instructions enclosed by “{ }” which performs specific operation in a C program. Actually, Collection of these functions creates a C program.
2. 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.
3. 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.
SIMPLE EXAMPLE PROGRAM FOR C FUNCTION:
As you know, functions should be declared and defined before calling in a C program.
In the below program, function “square” is called from main function.
The value of “m” is passed as argument to the function “square”. This value is multiplied by itself in this function and multiplied value “p” is returned to main function from function “square”.
#include<stdio.h>
// 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 ) ;

A function  in C is a group of sentences (basically commands) which altogether perform some task.
In fact all the operations performed in C are done by some function (either user defined or predefined ).
A function may take some inputs (i.e. passing parameters to function) and it may return a value after its execution (i.e. the return value).
A program should have at least one functions namely main() which marks the initiation of the execution of the program. We can define and call( this means to execute the function that we have already defined) user defined functions in C.

A file with definition of functions is called aLibrary. Including a library allows us to access(directly call) those function in our program. A library may also be predefined (like stdio.h allows us to use functions like printf and scanf) or a user defined library.

Amazing Facts about Programming

Amazing Facts about programming  The first programmer was a lady named Ada Lovelac . She was a writer and gifted mathematician...

Popular Tutorials