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

CA2 Notes Functions For Students 2

Uploaded by

22u211
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

CA2 Notes Functions For Students 2

Uploaded by

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

C – Function

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. You can learn below concepts of C functions in
this section in detail.

1. What is C function?
2. Uses of C functions
3. C function declaration, function call and definition with example program
4. How to call C functions in a program?
1. Call by value
2. Call by reference
5. C function arguments and return values
1. C function with arguments and with return value
2. C function with arguments and without return value
3. C function without arguments and without return value
4. C function without arguments and with return value
6. Types of C functions
1. Library functions in C
2. User defined functions in C
1. Creating/Adding user defined function in C library
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.

C functions aspects syntax

Return_type function_name (arguments list)


function definition { Body of function; }

function call function_name (arguments list);

function declaration return_type function_name (argument list);

Syntax of a function

return_type function_name (argument list)


{
Set of statements – Block of code
}
return_type: Return type can be of any data type such as int, double, char, void, short etc. Don’t worry you
will understand these terms better once you go through the examples below.

function_name: It can be anything, however it is advised to have a meaningful name for the functions so
that it would be easy to understand the purpose of function just by seeing it’s name.

argument list: Argument list contains variables names along with their data types. These arguments are
kind of inputs for the function. For example – A function which is used to add two integer variables, will be
having two integer argument.

Block of code: Set of C statements, which will be executed whenever a call will be made to the function.

SIMPLE EXAMPLE PROGRAM FOR C FUNCTION:


As you know, 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”.
#include<stdio.h>
// function prototype, also called function declaration
float square ( float x );
// main function, program starts from here

int main( )
{
float m, n ;
printf ( "\nEnter some number for finding square \n");
scanf ( "%f", &m ) ;
// function call
n = square ( m ) ;
printf ( "\nSquare of the given number %f is %f",m,n );
}

float square ( float x ) // function definition


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

Enter some number for finding square


2
Square of the given number 2.000000 is 4.000000

4. HOW TO CALL C FUNCTIONS IN A PROGRAM?


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.
Note:

Actual parameter – This is the argument which is used in function call.


Formal parameter – This is the argument which is used in function definition
EXAMPLE PROGRAM FOR C FUNCTION (USING CALL BY VALUE):
In this program, the values of the variables “m” and “n” are passed to the function “swap”.
These values are copied to formal parameters “a” and “b” in swap function and used.
#include<stdio.h>
// function prototype, also called function declaration
void swap(int a, int b);
int main()
{
int m = 22, n = 44;
// calling swap function by value
printf(" values before swap m = %d \nand n = %d", m, n);
swap(m, n);
}
void swap(int a, int b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
printf(" \nvalues after swap m = %d\n and n = %d", a, b);
}
OUTPUT:

values before swap m = 22


and n = 44
values after swap m = 44
and n = 22

C – Argument, return value


All C functions can be called either with arguments or without arguments in a C program. These functions
may or may not return values to the calling function. Now, we will see simple example C programs for each
one of the below.

1. C function with arguments (parameters) and with return value.


2. C function with arguments (parameters) and without return value.
3. C function without arguments (parameters) and without return value.
4. C function without arguments (parameters) and with return value.

C functions aspects syntax

1. With arguments and with function declaration:


int function ( int );
function call: function ( a );

function definition:
int function( int a )
{
statements;
return a;
return values }

function declaration:
void function ( int );
function call: function( a );
function definition:
void function( int a )
{
2. With arguments and without statements;
return values }

function declaration:
void function();

function call: function();


function definition:
void function()
3. Without arguments and {
without statements;
return values }

function declaration:
int function ( );
function call: function ( );
function definition:
int function( )
{
statements;
4. Without arguments and with return a;
return values }
NOTE:
 If the return data type of a function is “void”, then, it can’t return any values to the calling function.
 If the return data type of the function is other than void such as “int, float, double etc”, then, it can
return values to the calling function.

DO YOU KNOW HOW MANY VALUES CAN BE RETURN FROM C FUNCTIONS?


 Always, Only one value can be returned from a function.
 If you try to return more than one values from a function, only one value will be returned that appears at
the right most place of the return statement.
 For example, if you use “return a,b,c” in your function, value for c only will be returned and values a, b
won’t be returned to the program.
 In case, if you want to return more than one values, pointers can be used to directly change the values in
address instead of returning those values to the function.
C – 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.
 We can make use of these library functions to get the pre-defined output instead of writing our own
code to get those outputs.
 These library functions are created by the persons who designed and created C compilers.
 All C standard library functions are declared in many header files which are saved as file_name.h.
 Actually, function declaration, definition for macros are given in all header files.

You might also like