C Functions (Call by Value N Call by Reference)
C Functions (Call by Value N Call by Reference)
C Functions (Call by Value N Call by Reference)
Types of functions
1) Predefined standard library functions – such
as puts(), gets(), printf(), scanf() etc – These are the functions which already
have a definition in header files (.h files like stdio.h), so we just call them
whenever there is a need to use them.
2) User Defined functions – The functions that we create in a program are
known as user defined functions.
Why we need functions in C
Functions are used because of following reasons –
a) To improve the readability of code.
b) Improves the reusability of the code, same function can be used in any
program rather than writing the same code again n again
c) Debugging of the code would be easier if you use functions, as errors are
easy to be traced.
d) Reduces the size of the code, duplicate set of statements are replaced by
function calls.
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. 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.
Lets take an example – Suppose you want to create a function to add two
integer variables.
Let’s split the problem so that it would be easy to understand –
Function will add the two numbers so it should have some meaningful name
like sum, addition, etc. For example lets take the name addition for this
function.
return_type addition(argument list)
This function addition adds two integer variables, which means I need two
integer variable as input, lets provide two integer parameters in the function
signature. The function signature would be –
return_type addition(int num1, int num2)
The result of the sum of two integers would be integer only. Hence function
should return an integer value – I got my return type – It would be integer –
int addition(int num1, int num2);
Char with function Structure would look like –
#include<stdio.h>
int sum(int x, int y)
{
return (x + y);
}
int main()
{
int x, y, s;
printf("Enter Two Numbers :");
scanf("%d%d", &x, &y);
s = sum(x, y);
printf("\nThe Sum is :%d", s);
return 0;
}
Example1: Creating a user defined function addition()
#include <stdio.h>
int addition(int num1, int num2)
{
int sum;
/* Arguments are used here*/
sum = num1+num2;
int main()
{
int var1, var2;
printf("Enter number 1: ");
scanf("%d",&var1);
printf("Enter number 2: ");
scanf("%d",&var2);
return 0;
}
Output:
Enter number 1: 100
Enter number 2: 120
Output: 220
Example:
#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 );
}
Example2: Creating a void user defined function that doesn’t return anything
#include <stdio.h>
/* function return type is void and it doesn't have parameters*/
void introduction()
{
printf("Hi\n");
printf("My name is Chaitanya\n");
introduction();
printf("How are you?");
#include <stdio.h>
int sum(int n);
int main()
{
int number, result;
printf("Enter a positive integer: ");
scanf("%d", &number);
result = sum(number);
printf("sum = %d", result);
return 0;
}
int sum(int n)
{
if (n != 0)
// sum() function calls itself
return n + sum(n-1);
else
return n;
}
Parameter Passing to functions
The parameters passed to function are called actual parameters. For example, in
the above program 10 and 20 are actual parameters.
The parameters received by function are called formal parameters. For example, in
the above program x and y are formal parameters.
There are two most popular ways to pass parameters.
Pass by Value: In this parameter passing method, values of actual parameters are
copied to function’s formal parameters and the two types of parameters are stored in
different memory locations. So any changes made inside functions are not reflected
in actual parameters of caller.
OR
Call by value/Pass By Value : This method uses in-mode semantics. Changes
made to formal parameter do not get transmitted back to the caller. Any
modifications to the formal parameter variable inside the called function or
method affect only the separate storage location and will not be reflected in the
actual parameter in the calling environment. This method is also called as call
by value.
Function call by value is the default way of calling a function in C programming.
Before we discuss function call by value, lets understand the terminologies that
we will use while explaining this:
Actual parameters: The parameters that appear in function calls.
Formal parameters: The parameters that appear in function declarations.
return 0;
}
// C program to illustrate
// call by value
#include <stdio.h>
// Passing parameters
func(x, y);
printf("In main, x = %d y = %d\n", x, y);
return 0;
}
Example:
#include <stdio.h>
int increment(int var)
{
var = var+1;
return var;
}
int main()
{
int num1=20;
int num2 = increment(num1);
printf("num1 value is: %d", num1);
printf("\nnum2 value is: %d", num2);
return 0;
}
Example:
#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);
}
return 0;
}
#include <stdio.h>
void swapnum( int var1, int var2 )
{
int tempnum ;
/*Copying var1 value into temporary variable */
tempnum = var1 ;
EXAMPLE:
# include <stdio.h>
void fun(int *ptr)
{
*ptr = 30;
}
int main()
{
int x = 20;
fun(&x);
printf("x = %d", x);
return 0;
}
O/P=30
However, in C, we can use pointers to get the effect of pass by reference. For
example, consider the below program. The function fun() expects a pointer ptr to an
integer (or an address of an integer). It modifies the value at the address ptr. The
dereference operator * is used to access the value at an address. In the statement
‘*ptr = 30’, value at address ptr is changed to 30. The address operator & is used to
get the address of a variable of any data type. In the function call statement ‘fun(&x)’,
the address of x is passed so that x can be modified using its address.
Example:
#include <stdio.h>
void increment(int *var)
{
/* Although we are performing the increment on variable
* var, however the var is a pointer that holds the address
* of variable num, which means the increment is actually done
* on the address where value of num is stored.
*/
*var = *var+1;
}
int main()
{
int num=20;
/* This way of calling the function is known as call by
* reference. Instead of passing the variable num, we are
* passing the address of variable num
*/
increment(&num);
printf("Value of num is: %d", num);
return 0;
}
O/P=21
// C program to illustrate
// call by reference
#include <stdio.h>
int main(void)
{
int a = 10, b = 20;
// passing parameters
swapnum(&a, &b);
int main()
{
int ageArray[] = {2, 8, 4, 12};
return 0;
}
#include <stdio.h>
float calculateSum(float age[])
{
float sum = 0.0;
int i;
for (i= 0; i < 6; ++i)
{
sum += age[i];
}
return sum;
}
int main()
{
float result, age[] = {23.4, 55, 22.6, 3, 40.5, 18};
int main()
{
int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
print(arr);
return 0;
}
Example 2:
#include <stdio.h>
const int N = 3;
int i, j;
int main()
print(arr, 3);
return 0;
}
return 0;}
Output:a b c d e f g h i j
return 0;}
Output:
1234567890