0% found this document useful (0 votes)
57 views17 pages

Function Inc

Uploaded by

Ruchita Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views17 pages

Function Inc

Uploaded by

Ruchita Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

1

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

5. C function declaration, function call and definition

6. Simple Example Program for C Function

7. How to call C functions in a program?

8. Overview
3
1.What is C Function?
In short: A function is a group of statements that together
perform a task.

C functions are basic building blocks in a program.

Long Answer: 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
A large program in c can be divided to many subprogram 4
The subprogram posses a self contain components and have well define
purpose.
The subprogram is called as a function
Basically a job of function is to do something
C program contain at least one function which is main().

2. Types of C functions
Classification Of
Function

User define Library


function 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.

 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.
7

It is much easier to write a structured program where a large


program can be divided into a smaller, simpler task.

Allowing the code to be called many times

Easier to read and update

It is easier to debug a structured program


where there error

is
easy to find and fix
5. C function declaration, function call and function definition: 8

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.

C functions aspects syntax

Return_type function_name
function definition (arguments list)
{ Body of function; }

function call function_name (arguments list);

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;

/* calling a function to get max value */


ret = max(a, b);

printf( "Max value is : %d\n", ret );


Output:
return 0;
} Max value is : 800
/* function returning the max between two numbers */
int max(int num1, int num2) {

/* local variable declaration */


int result;

if (num1 > num2)


result = num1;
e
result = num2;

return result;
}
11
Arguments/formal parameter

1: #include <stdio.h>  Function names is


2:
3: long cube(long x);
cube
4:  Variable that are
5: long input, answer; Return data type
requires is long
6:  The variable to be
7: int main( void )
8: {
passed on is X(has
9: printf(“Enter an integer value: “);
Actual parameters
single arguments)—
10: scanf(“%d”, &input); value can be passed
11: answer = cube(input); to function so it can
12: printf(“\nThe cube of %ld is %ld.\n”, input, answer);
13:
perform the specific
14: return 0; task. It is called
15: }
16:
17: long cube(long x)
18: {
19: long x_cubed;
Output
20:
21: x_cubed = x * x * x; Enter an integer value: 4
22: return x_cubed;
23: } The cube of 4 is 64.
7. How To Call C Functions In A Program? 12

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

void swap(int a, int b)


{
int tmp; values before swap m = 22
tmp = a; and n = 44
a = b; values after swap m = 44
b = tmp; and n = 22
printf("
\nvalues
after
1
2. Call by reference: 2
#include<stdio.h>
•In call by reference method, the 3
4
// function prototype, also called function declaration
void swap(int *a, int *b); 14
address of the variable is passed to 5
6
the function as parameter. 7
int main()
8
•The value of the actual parameter 9 {
10
can be modified by formal parameter. 11 int m = 22, n = 44;
•Same memory is used for both actual 12 // calling swap function by reference
13
and formal parameters since only 14 printf("values before swap m = %d \n and n = %d",m,n);
15
address is used by both parameters 16
swap(&m, &n);
17 }
18
19
20 void swap(int *a, int *b)
21
22 {
Example Program For C Function 23
24
int tmp;
(Using Call By Reference): 25 tmp = *a;
26
*a = *b;
•In this program, the address of the 27
28 *b = tmp;
variables “m” and “n” are passed to 29
30
printf("\n
the function “swap”. 31 values
•These values are not copied to after
swap a =
formal parameters “a” and “b” in %d \nand Output:
swap function. b = %d",
•Because, they are just holding the values*a,before
*b);
swap m = 22
}
address of those variables. and n = 44
•This address is used to access and values after swap a = 44
change the values of the variables. and b = 22
Overview 15

C functions are basic building blocks in every C program.


There are 2 types of functions in C. They are, 1. Library functions 2. User defined functions

Key points to remember while writing functions in C language:

 All C programs contain main() function which is mandatory.


 main() function is the function from where every C program is started to execute.
 Name of the function is unique in a C program.
 C Functions can be invoked from anywhere within a C program.
 There can any number of functions be created in a program. There is no limit on this.
 There is no limit in calling C functions in a program.
 All functions are called in sequence manner specified in main() function.
 One function can be called within another function.
 C functions can be called with or without arguments/parameters. These arguments are nothing but inputs to
the functions.
 C functions may or may not return values to calling functions. These values are
nothing but output of the functions. 16
 When a function completes its task, program control is returned to the function from
where it is called.
 There can be functions within functions.
 Before calling and defining a function, we have to declare function prototype in
order to inform the compiler about the function name, function parameters and return
value type.
 C function can return only one value to the calling function.
 When return data type of a function is “void”, then, it won’t return any values
 When return data type of a function is other than void such as “int, float,
double”, it returns value to the calling function.
 main() program comes to an end when there is no functions or commands to
execute.
17

Thank You

You might also like