C Foundation

What is C? C Compiler Installation C Extensions C Compiler C Interpreter C Program Structure

C Basics

C Keywords C Data Types C Identifiers C Variables C Constant C Escape Sequences C Constant and Volatile C Typecast

Operators

What is Operator C Comma Operator C Arithmetic Operators C Relational Operators C Logical Operators C Bitwise Operators C Conditional Operators C : : Operator C Operator Priority

Basic IO's

Basic IO's C Formatted Functions C Unformatted Functions C Common Functions

Control Statements

What is Control Statement C if Statement C if else Statement C Nested if Statement C Else if Statement C Break Statement C Continue Statement C Switch Statement C Goto Statement

Looping

What is Control Loop C for Loop C Nested for Loop C while Loop C Nested while Loop C do while Loop C Nested do while loop

Functions

What is Function C User Defined Functions C Recursion C Passing Parameters

Scope

Scope C Local Scope C Global Scope

Storage Classes

What is Storage Class C Auto C Extern C Static C Register

Array

What is Array C One Dimensional Array C Two Dimensional Array C Multi Dimensional Array C Arrays Of Strings

String

What is String C String Functions

Pointer

What is Pointer C Pointers Arithmetic C Pointer to Pointer C Pointers and Arrays C Pointers and Strings C Pointer to Functions Void Pointers Null Pointers C Null and Void Pointer

Structure

What is Structure C Struct within Struct C Array within Structure C Pointer to Structure C Structure and Function C Enum C Bitfield Structure C Type def

Union

What is Union

Files

What is File C read a file C write a file C File Handling C Error Handling C Low Level Disk I/O C Other file functions

Memory Allocation

What is Memory Allocation C Malloc() C Calloc() C Free() C Realloc() C Coreleft()

C Reference

All ASCII Code Basic C Questions

C Interview

C Interview Sets All Star Patterns All Number Patterns All Alphabet Patterns All Series Patterns
The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C Call By Value And Call By Reference

Accessing Function

There are two different way a function can be accessed. Eitheir by passing arguments (parameters) to a function or by passing nothing to a function. Function which doesn't accept any parameters are mostly void type.

Accessing function using parameters are of two types. They are

  • Call by value
  • Call by Reference or address

Before getting into the play of call by value and call by reference, one need to know what is actual parametes and formal parameters.

What Is Actual Parameters

Actual parameters are specified in the function call. In simple terms, value or reference passing at the time of function call.

What Is Formal Parameters

Formal parameters are specified in the function declaration and function definition. In simple terms, variables with datatype at the function declaration and definition.

Call By Value

  • Call by value is the method of passing values as the parameters to the function.
  • In this method, the values of actual parameters are passed to the formal arguments.
  • In call by value method, the values of actual parameters are not changed even they are changed in the called function.
  • Because the actual parameters pass the copy of original parameters to the called function.

C program - Call By Value

Let us write a c program to demonstrate how to use call by value?

callbyvalue.c
#include <stdio.h>
int pass(int, int);     //function declaration
int main()
{
//main function definition
int a = 5, b = 10;
printf("The values of a and b in main function before function call : %d %d ", a, b);
pass(a, b);    //function call with passing arguments as values
printf("\nThe values of a and b in main function after function call : %d %d ", a, b);
}
// function definition
int pass(int a, int b)
{
a = 10; b = 5;     //This change will not affect the actual parameters
printf("\nThe values of a and b in called function : %d %d ", a, b);
}
  • The values of a and b in main function before function call : 5 10
  • The values of a and b in called function : 10 5
  • The values of a and b in main function after function call : 5 10

Note:

The program illustrates that, the actual parameters are passed as copy of original parameters to the called function. So, the changes of values inside the called function does not affect the parameters of calling function.

Call by Reference

  • Call by reference is the method of passing variable addresses as the parameters to the function.
  • In call by reference method, changes made in called function will affect the parameters inside the main function.

C program - Call By Reference

Let us write a c program to demonstrate how to use call by value?

callbyreference.c
#include <stdio.h>
int pass(int*, int*);     //function declaration
int main()
{
//main function definition
int a = 5, b = 10;
printf("The values of a and b in main function before function call : %d %d ", a, b);
pass(&a, &b);    //function call with passing arguments as addresses
printf("\nThe values of a and b in main function after function call : %d %d ", a, b);
}
// function definition
int pass(int * a, int *b)
{
*a = 10; *b = 5;     //This change will affect the actual parameters
printf("\nThe values of a and b in called function : %d %d ",*a, *b);
}
  • The values of a and b in main function before function call : 5 10
  • The values of a and b in called function : 10 5
  • The values of a and b in main function after function call : 10 5

Note:

The program illustrates that, changes made in called function affect the parameters inside the main function. So, the changes made in the parameters are permanent.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report

We to update you