practical7
practical7
Aim: - Write a Program in “C” using function for finding square of any number.
Theory:-
C functions are basic building blocks in a program. All C programs are written using
functions to improve re-usability, understandability and to keep track on them.
1. 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.
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.
/*Write a program in c for function which is used to find square of any number */
#include<stdio.h>
// function prototype, also called function declaration
float square ( float x );
void main( )
{
float m, n ;
clrscr();
printf ( "\nEnter a number for finding square \n");
scanf ( "%f", &m ) ;
// function call
n = square ( m ) ;
printf ( "\nSquare of the given number %f is %f",m,n );
getch();
}
Output:
Enter a number for finding square
3
Square of the given number 3 is 9
Conclusion: Thus we have studied and performed the program for functions.