C Language

Call by Value vs Call by Reference

In C language, there are two ways to passing arguments in a function, that are Call by Value and Call by Reference. Before we start to know these two process, we have to clear the concept of argument and function.

So, what is function and argument?

Function: Function is a good programming style c language coaching centres in vizag in which we can write reusable code that can be called Whenever require. JNNC Technologies

Argument: Functions are generally written for a particular operation with one or more inputs.

  • Like we write a logical function to check if the number is prime or not which is not dependent on any specific number but can be used for any positive integers.
  • C language institutes in vizag JNNC Technologies We reuse this logic by providing an input number.
  • This input is called function argument.

Let’s back to the main topic, we already discussed that C provides two ways of passing arguments to a function. These are –

Call by value

In this method a copy of each of the actual arguments is made first then these values are assigned to the corresponding formal arguments.

That means the changes made by the called function have no effect on the values of actual arguments in the calling function.

C language coaching centres in vizag JNNC Technologies When you passed a value to the function it is locally stored by the function parameter in stack memory location.

If you change the value of function parameter, then it changed for the current function only.

How the call by value works?

  • While passing parameters using call by value, the copy of original parameter is created and passed to the called function.
  • Any update made inside the method will not affect the original value of the variable in the calling function.
  • Copy of the original values are passed to the function and the values are copied into the variable of called function respectively.
  • As their scope is limited to the only function, so they cannot alter the values inside the main function.

Example –

#include

int factorial(int);
int main() {
    long int no, f = 1;
    printf("Calculate Factorial\n");
    printf("Enter a number:\n");
    scanf("%ld", &no);

    /* If the given number is not zero then factorial calculation start */
    if(no != 0) {

        /* Call factorial function by passing integer value and receive result in 'f' variable */
        f = factorial(no);
        printf("Factorial: %ld\n", f);
    }

    /* If the given number is zero then factorial will be one */
    else
        printf("Factorial: 1\n");
    return 0;
}

/* Recursive function */
int factorial(int no) {
    if(no == 1)
        return 1;
    else
        /* Call the factorial function inside in it */
        return(no * factorial(no - 1));
}

/****** Output ******/
Calculate Factorial

Enter a number:

5

Factorial: 120

Call by reference

In this method, when we call a function by passing the addresses of actual parameters then this way of calling the function is known as call by reference. In call by reference, the operation performed on formal parameters affects the value of actual parameters because all the operations performed on the value stored in the address of actual parameters.

How the call by reference works?

  • While passing parameter using call by address scheme, you are passing the actual address of the variable to the called function.
  • Any updates made inside the called function will modify the original copy since you are directly modifying the content of the exact memory location.

Example –

#include

/* Function 'swap' with pointer parameter */
void swap(int *a, int *b) {
    int temp;

    /* Swapping values using 'temp' variable */
    temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int x, y;
    printf("Enter a value for X:\n");
    scanf("%d", &x);
    printf("Enter a value for Y:\n");
    scanf("%d", &y);
    printf("Before swapping\n");
    printf("X = %d\nY = %d\n",x, y);

    /* Passing reference(memory address) of the variables */
    swap(&x, &y);
    printf("After swapping\n");
    printf("X = %d\nY = %d\n",x, y);
    return 0;
}

/****** Output ******/
Enter a value for X: 5

Enter a value for Y: 10

Before swapping

X = 5

Y = 10

After swapping

X = 10

Y = 5

Let’s find out the differences between call by value and call by reference in c language –

  • A duplicate copy of the value is passed to the function in a call by value, where the actual copy or an address of value is passed to the function is known as call by reference.
  • In call by value, changes made inside the function is not reflected on other functions. But in the call by reference, changes made inside the function and it is reflected outside the function.
  • Actual and formal arguments will be created in different memory location when processing call by value. But in the call by reference, it will be using same memory location.

Usage

The call by reference use pointer, so there is no doubling of the memory used by the variables. Lowering the memory footprint is always a good thing. So it makes sense that why not make all the parameters call by reference?

Two reasons why this is not good for and need to choose between call by value and call by reference. The reasons are side effects which are usually caused by inadvertently changes that are made to a call by reference parameter.  And most cases you want the data to be private and that someone calling a function only be able to change if you want it. So it is better to use a call by value by default. But use call by reference if the data changes are expected.

Why C is so popular and still the best programming language?



Computer programming has changed a lot since the 1960’s but one language has managed to stay the same through thick and thin C. When C compilers had gotten into the hands of the common man, things really exploded. At a glance, there have nothing special about C compared to Ruby or Python, but C is way more powerful than anything.

There are many programming languages, today, that allow developers to be more productive than with C for different kinds of projects. There are higher level languages that provide much larger built-in libraries that simplify working with JSON, XML, UI, web pages, client requests, database connections, media manipulation, and so on. But despite that, there are plenty of reasons to believe that C programming will remain active for a long time. Here are some reasons that C is unbeatable.

Portability and Efficiency –

C is almost a portable assembly language. Before C, programmers had to rely on Assembly. Assembly is no doubt a great language, but there was one drawback – it was based on system instructions differing between CPUs. When C came along, it break all the barrier. It is as close to the machine as possible while it is almost universally available for existing processor architectures. There is at least one C compiler for almost every existent architecture.

C has no pre-defined style –

Despite its reputation of chat-channel flame wars, the fact that C has no style guide can actually be quite good. In a sense, C teaches us to accept the fact that not the only one coding in the world. Macro naming, function naming, and data structure naming all depends on your preference of style.

Deterministic Usage of Resources –

Arbitrary memory address access and pointer arithmetic is an important feature that makes C a perfect fit for system programming. At the hardware/software boundary, computer systems and microcontrollers map their peripherals and I/O pins into memory addresses. System applications must read and write to those custom memory locations to communicate with the world. So C’s ability to manipulate arbitrary memory addresses is imperative for system programming.

Another common language feature that system programming cannot rely on is garbage collection, or even just dynamic allocation for some embedded systems. Embedded applications are very limited in time and memory resources. They are often used for real-time systems, where a non-deterministic call to the garbage collector cannot be afforded. And if dynamic allocation cannot be used because of the lack of memory, it is very important to have other mechanisms of memory management, like placing data in custom addresses, as C pointers allow.

C is small and simple –


C has a very small runtime. And the memory footprint for its code is smaller than for most other languages. Since C is fully based on variables, macros, functions, and structures, there isn’t all that much to it. Due to this, C has been embedded on almost any modern microprocessor, from fridges to alarm clocks. It lacks the expressivity of sophisticated OOP or functional languages, but its simplicity means it can be picked up quickly.

Reasons to Learn C –

C is not a hard language to learn, so all the benefits from learning it will come quite cheap. Since almost all programming languages nowadays are themselves implemented in C, knowing C basically gives you a free ticket to knowing all programming languages. It’s true to say that C is very different from C++ and Objective-C, but from a learning standpoint they all aren’t that far apart. Of course, C is a procedural language, which means classes and objects are non-existent compared to languages like Python, but Python’s class model itself is written in C. This means that understanding C may not teach you object-oriented programming, but it will teach you how it was conceived, designed, and implemented.

 Many Interesting Projects to Power the World –

C is the best programming language today is still the fact that it simply powers everything. From your phone to your Wifi, no other language provides the level of hardware interaction with the practicality. Many of the C projects that exist today were started decades ago.

The UNIX operating system’s development started in 1969, and its code was rewritten in C in 1972. The C language was actually created to move the UNIX kernel code from assembly to a higher level language, which would do the same tasks with fewer lines of code.

Oracle database development started in 1977, and its code was rewritten from assembly to C in 1983. It became one of the most popular databases in the world.

In 1985 Windows 1.0 was released. Its kernel is mostly written in C, with some parts in assembly. Linux kernel development started in 1991, and it is also written in C. The next year, it was released under the GNU license and was used as part of the GNU Operating System. The GNU operating system itself was started using C and Lisp programming languages, so many of its components are written in C.

But C programming isn’t limited to projects that started decades ago, when there weren’t as many programming languages as today. Many C projects are still started today.

 

 

Open chat