0% found this document useful (0 votes)
1 views

practical7

The document outlines a practical exercise in C programming focused on creating a function to calculate the square of a number. It explains the concept of C functions, their uses, and the syntax for function declaration, call, and definition. A sample program is provided, demonstrating how to implement the square function and its execution output.

Uploaded by

jadhaoshiv3012
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

practical7

The document outlines a practical exercise in C programming focused on creating a function to calculate the square of a number. It explains the concept of C functions, their uses, and the syntax for function declaration, call, and definition. A sample program is provided, demonstrating how to implement the square function and its execution output.

Uploaded by

jadhaoshiv3012
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Practical no.

Aim: - Write a Program in “C” using function for finding square of any number.

Objective: - Learning functions & its compilation.

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.

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.

S.no C function aspects syntax


1 function definition return_type function_name ( arguments list )
{ Body of function; }
2 function call function_name ( arguments list );
3 function declaration return_type function_name ( argument list );

Program for C function :

 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”.

/*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 );

// main function, program starts from here


#include<stdio.h>
#include<conio.h>

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();
}

float square ( float x ) // function definition


{
float p ;
p=x*x;
return ( p ) ;
}

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.

You might also like