0% found this document useful (0 votes)
15 views5 pages

Function Notes More

C functions are fundamental building blocks in a C program that encapsulate specific operations, promoting code reusability and organization. They consist of three main components: declaration, call, and definition, and can be invoked using call by value or call by reference methods. Each method has its advantages and disadvantages, impacting memory usage and the ability to modify original variables.
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)
15 views5 pages

Function Notes More

C functions are fundamental building blocks in a C program that encapsulate specific operations, promoting code reusability and organization. They consist of three main components: declaration, call, and definition, and can be invoked using call by value or call by reference methods. Each method has its advantages and disadvantages, impacting memory usage and the ability to modify original variables.
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/ 5

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


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

1 #include<stdio.h>
2 // function prototype, also called function declaration
3 float square ( float x );
4 // main function, program starts from here
5
6 int main( )
7 {
8 float m, n ;
9 printf ( "\nEnter some number for finding square \n");
10 scanf ( "%f", &m ) ;
11 // function call
12 n = square ( m ) ;
13 printf ( "\nSquare of the given number %f is %f",m,n );
14 }
15
16 float square ( float x ) // function definition
17 {
18 float p ;
19 p = x * x ;
20 return ( p ) ;
21 }
OUTPUT:
Enter some number for finding square
2
Square of the given number 2.000000 is 4.000000

Function arguments in c programming


Basically, there are two types of arguments:

 Actual arguments
 Formal arguments

The variables declared in the function prototype or definition are known as Formal
arguments and the values that are passed to the called function from the main function are
known as Actual arguments.

The actual arguments and formal arguments must match in number, type, and order.

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.
// arguments pass by value
# include <stdio.h>
int add (int a, int b)
{
return( a + b );
}
int main()
{
int x, y, z;
x = 5;
y = 5;
z = add(x,y); // call by value
return 0;
}
//end

2. CALL BY REFERENCE:
 In call by reference method, the address of the variable is passed to the function as
parameter.
 The value of the actual parameter can be modified by formal parameter.
 Same memory is used for both actual and formal parameters since only address is used
by both parameters.

// arguments pass by reference


#include <stdio.h>
void swap (int *a, int *b) // a and b are reference variables
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int x = 2, y = 4;
printf("before swapping x = %d and y = %d\n", x, y);
swap(&x, &y); // call by reference
return 0;
} //end of program
Call by Value vs. Call by Reference

Parameters Call by value Call by reference

While calling a function, in


While calling a function, when you pass programming language instead of
Definition values by copying variables, it is known copying the values of variables, the
as "Call By Values." address of the variables is used it is
known as "Call By References.

In this method, a copy of the variable is In this method, a variable itself is


Arguments
passed. passed.

Changes made in a copy of variable never Change in the variable also affects the
Effect modify the value of variable outside the value of the variable outside the
function. function.

Allows you to make changes in the


Alteration of Does not allow you to make any changes
values of variables by using function
value in the actual variables.
calls.

Passing of Values of variables are passed using a Pointer variables are required to store
variable straightforward method. the address of variables.

Value
Original value not modified. The original value is modified.
modification

Memory Actual and formal arguments will be Actual and formal arguments will be
Location created in different memory location created in the same memory location

Actual arguments are not Safe. They can


Actual arguments remain safe as they
Safety be accidentally modified, so you need to
cannot be modified accidentally.
handle arguments operations carefully.

Default in many programming languages


It is supported by most programming
Default like C++.PHP. Visual Basic NET, and
languages like JAVA, but not as default.
C#.
Advantages of using Call by value method

Pros/benefits of a call by value method:

 The method doesn't change the original variable, so it is preserving data.


 Whenever a function is called it, never affect the actual contents of the actual
arguments.
 Value of actual arguments passed to the formal arguments, so any changes made in
the formal argument does not affect the real cases.

Advantages of using Call by reference method

Pros of using call by reference method:

 The function can change the value of the argument, which is quite useful.
 It does not create duplicate data for holding only one value which helps you to save
memory space.
 In this method, there is no copy of the argument made. Therefore it is processed
very fast.
 Helps you to avoid changes done by mistake
 A person reading the code never knows that the value can be modified in the
function.

Disadvantages of using Call by value method

Here, are major cons/drawbacks of a call by value method:

 Changes to actual parameters can also modify corresponding argument variables


 In this method, arguments must be variables.
 You can't directly change a variable in a function body.
 Sometime argument can be complex expressions
 There are two copies created for the same variable which is not memory efficient.

Disadvantages of using Call by reference method

Here, are major cons of using call by reference method:

 Strong non-null guarantee. A function taking in a reference need to make sure that
the input is non-null. Therefore, null check need not be made.
 Passing by reference makes the function not pure theoretically.
 A lifetime guarantee is a big issue with references. This is specifically dangerous
when working with lambdas and multi-threaded programs.

You might also like