C Tutorial

Define C

Define Programming

Programming Approach

First C Program

TurboC Shortcut keys

Compiler vs Interpreter

C Variable

C Keywords

C Data Types

C Comments

C Operators

Hierarchy of Operators

Ex: Arithmetic Operator

C Formatting Output

C Escape Sequence

C if statement

Ex: If statement

C Switch statement

Ex: Switch

Increment / Decrement

C Loops

Ex: Loops

C Nesting Of Loops

Ex: Nested Loops

C Jumping Statements

C Exit(), Gotoxy()

C Arrays 1D

Ex: Arrays 1D

C Arrays 2D

Ex: Arrays 2D

C Sorting

ASCII Value

Character I/O Function

Ex: Character

C String Functions

Ex: Strings

Array of Strings

C Math Functions

User-defined Function

Exercise Function

Local, Reference variable

Function Calling types

Array Passing Function

Recursion Function

Ex: Recursion Function

Constant Variable

Storage Class

C Header Files

C Preprocessor

C Pointers

Pointer to Pointer

C Structures

Exercise Structure

C Typedef

C Enumeration

C File Handling

Ex: File Handling

Command Line Argument

MCQ

Question-Answer

Page Stats

Visitor: 474

Types of function

In C Language, when function is called with arguments, you can pass the values using 2 ways: call by value and call by reference.

  1. Call by value When a function is called and value of a variable is passed via argument it is known as call-by-value. In this case, any changes made inside the function have no effect on another functions.
  2. Call by Reference When a function is called and reference (address) of a variable is passed via argument it is known as call-by-reference. In this case, changes will reflect in another functions.

Example 1: Call by value.

#include<stdio.h>
#include<conio.h>

void fun(int); //function declaration
void main()
{
    int a=10;
    clrscr();
    fun(a);  //function call
    printf("\nValue of A outside function is = %d",a);
    getch();
}
void fun(int a) //function definition
{
    a=20;
    printf("\nValue of A inside function is %d",a);
}
Value of A inside function is 20
Value of A outside function is 10

Value change inside function will NOT reflect in main function.

Example 2: Call by reference.

#include<stdio.h>
#include<conio.h>

void fun(int &a); //function declaration
void main()
{
    int a=10;
    clrscr();
    fun(a);  //function call
    printf("\nValue of A outside function is = %d",a);
    getch();
}
void fun(int &a) //function definition
{
    a=20;
    printf("\nValue of A inside function is %d",a);
}
Value of A inside function is 20
Value of A outside function is 20

Value change inside function will reflect in another function.

Advertisement

Example 3: Program to return multiple values by using call by reference.

#include<stdio.h>
#include<conio.h>

void AreaCircum(float, float &ar, float &cir);
void main()
{
    float radius, area=0, circum=0;
    clrscr();
	printf("Enter Radius: ");
    AreaCircum(radius, area, circum);
    printf("\nArea of Circle = %f",area);
    printf("\nCircumference of Circle = %f",circum);
    getch();
}
void AreaCircum(float r, float &ar, float &cir)
{
    ar=3.14*r*r;
    cir=2*3.14*r;
}
Enter Radius: 10
Area of Circle = 314.000000
Circumference of Circle = 62.8
S.No. Call by value Call by reference
A copy of value is passed to the function An address of value is passed to the function
Changes made inside the function is not reflected on other functions Changes made inside the function is reflected outside the function also
Actual and formal arguments will be created in different memory location Actual and formal arguments will be created in same memory location

Exercise Questions:

  1. WAP to Swap 2 Numbers using Call by Reference