Function Inc
Function Inc
Function in C Language
Outline 2
1. What is C function?
2.Types of C functions
3. Uses of C functions
4. Advantage of C functions
8. Overview
3
1.What is C Function?
In short: A function is a group of statements that together
perform a task.
2. Types of C functions
Classification Of
Function
-printf()
-scanf()
- main()
-sqrt()
-getchar()
Standard Library Functions:
Library functions in C language are inbuilt functions which are grouped together and placed in a
common place called library. Each library function in C performs specific operation. 5
The standard library functions are built-in functions in C programming to handle tasks such as
mathematical computations, I/O processing, string handling etc.
These functions are defined in the header file. When you include the header file, these functions
are available for use. For example:
➤ The printf() is a standard library function to send formatted output to the screen (display output
on the screen). This function is defined in "stdio.h" header file.
➤ There are other numerous library functions defined under "stdio.h", such as scanf(), printf(),
getchar() etc. Once you include "stdio.h" in your program, all these functions are available for use.
User-defined Functions:
As mentioned earlier, C allow programmers to define functions. Such functions created by the
user are called user-defined functions.
Depending upon the complexity and requirement of the program, you can create as many user-
defined functions as you want.
3. Uses of C 6
functions:
C functions are used to avoid rewriting same logic/code again and
again in a program.
is
easy to find and fix
5. C function declaration, function call and function definition: 8
Return_type function_name
function definition (arguments list)
{ Body of function; }
return_type function_name
function declaration
(argument list);
6. Simple Example Program for C Function:
As you know, functions should be declared and defined before calling in a C program. 9
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”.
1
2
#include<stdio.h>
3 // function prototype, also called function declaration Output:
4
5
float square ( float x );
6 // main function, program starts from here
7
8 Enter some number for finding square 2
9 int main( )
10
{ Square of the given number 2.000000 is
11 4.000000
12 float m, n ;
13
14
printf ( "\nEnter some number for finding square \n");
15
16
scanf ( "%f", &m ) ;
17 // function call
18
19
n = square ( m ) ;
20 printf ( "\nSquare of the given number %f is %f",m,n ); Enter some number for finding square 4
21 } Square of the given number 2.000000 is
16.000000
float square ( float x ) // function definition
{
float p ;
p=x*x;
return ( p
);
#include <stdio.h> 10
/* function declaration */
int max(int num1, int num2);
We have kept max() along with main() and
int main () {
compiled the source code. While running the
/* local variable definition */ final executable, it would produce the following
int a = 200;
int b = 800;
result −
int ret;
return result;
}
11
Arguments/formal parameter
There are two ways that a C function can be called from a program. They
are,
1.Call by value
2.Call by reference
1. Call By Value:
•In call by value method, the value of the variable is passed to the function as
parameter.
•The value of the actual parameter can not be modified by formal parameter.
•Different Memory is allocated for both actual and formal parameters. Because, value
of actual parameter is copied to formal parameter.
Example Program For C Function (Using Call By Value): In this program, the
values of the 13
1
2 #include<stdio.h> variables “m” and “n”
3
4 // function prototype, also called function declaration are passed to the
5
6 void swap(int a, int b); function “swap”.
7
8
9
These values are
10 int main() copied to formal
11
{
12
13 parameters “a” and
14 int m = 22, n = 44;
15
// calling swap function by value
“b” in swap function
16
17
18 printf(" values before swap m = %d \nand n = %d", m, and used.
19
20 n); swap(m, n);
}
Thank You