C Function

C  Function:

A large C program is divided into basic building blocks called C function. C function contains collection of instructions enclosed by “{ }” which performs specific operation in a C program. Actually, Collection of such functions creates a C program.

USES OF C FUNCTIONS:

• These are used to avoid rewriting same logic/code repetedly in a program.
• There is no limit in calling C functions to make use of same functionality wherever we need.
• We can call functions any number of times in a program and from any place in a program.
• A big C program can easily be debug when We divid into small modules
• The core concept of C functions are, re-usability, dividing a big task into small moduls to achieve the functionality and to improve understandability of very big and complicated C programs.

FUNCTION DECLARATION, FUNCTION CALL AND FUNCTION DEFINITION IN C:

• Function declaration or prototype – This provide information to compiler about the function name, function parameters and return value’s data type.
• Function call – it calls the actual function
• Function definition – This contains block of statements to be executed.

Apects of c function Syntax
function definition in C Return_type fun_name (arguments list) { Body of function; }
function call fun_name (arguments list);
function declaration in C return_type fun_name (argument list);

SIMPLE EXAMPLE PROGRAM FOR FUNCTION:

• Functions should be declared and defined before calling in a C program.
• In the below program, function “square1” is called from main function.
• The value of “m1” is passed as argument to the function “square1”.

Example 1
#include<stdio.h>

float square1 ( float x1 );

int main( )

{

float m1, n1 ;

printf ( “\nEnter a number for finding square \n”);

scanf ( “%f”, &m1 ) ;

n1 = square1 ( m1 ) ;

printf ( “\nSquare of the given number %f is %f”,m1,n1 );

}

float square1 ( float x1 ) // function definition

{

float p1 ; p1 = x1 * x1 ; return ( p1 ) ;

}

Output
Enter a number for finding square 3

Square of the given number 3.000000 is 9.000000

Example 2

#include<stdio.h>

void my_function1();

main()

{

printf(“Main function.\n”);

my_function1();

printf(“Back into the function main9().\n”);

return 0;

}

void my_function1()

{

printf(“Back into the function main().\n”);

}

Output
Main function.

Back into the function main().

Back into the function main().

 

Leave a Reply

Your email address will not be published. Required fields are marked *