Header Ads Widget

Ticker

6/recent/ticker-posts

Elements or parts of function

 4.5. Elements or parts of function:


There are three elements or parts of each C function. They are,

4.5.1. Function declaration or prototype:

This informs compiler about the function name, function parameters and return value’s that is data type of function.

4.5.2. Function call:

This calls to the actual function.

4.5.3. Function definition:

Function definition contains all the statements to be executed.

Sr no

C function aspects

syntax

1

function definition

return_type function_name ( formal arguments list ){ Body of function; }

2

function call

function_name ( actual arguments list );

3

function declaration

return_type function_name ( argument list );

 


 4.5.4. Simple program for C function:

As you know, functions should be declared and defined before calling in a C program.

In the below program, function “cube” is called from main function.

The value of “x” is passed as argument to the function “cube”.

This value is multiplied by itself three times in this function and return value “y” to main function.


#include< stdio.h >

#include< conio.h >

// function prototype, also called function declaration

float cube ( float x );

// main function, program starts from here

void main( )

{

float x, c ;

printf ( "\n Enter any number for finding cube \n");

scanf ( "%f", &x ) ;

// function call

c = cube ( x ) ;

printf ( "\n Cube of the given number %f is %f", x, c );

getch();

}

// function definition

float cube ( float x )

{

float y ;

y = x * x * x;

return ( y ) ;

}

Output:

Enter any number for finding cube

2

Cube of the given number 2 is 8

Post a Comment

0 Comments