Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Parameter Passing in Compiler Design
2.1.
Actual Parameter
2.2.
Formal Parameter
2.2.1.
Code
2.2.2.
 
2.2.3.
Output
3.
Basic Terminology in Parameter Passing in Compiler Design
3.1.
R-value
3.2.
L-value 
4.
Methods for Parameter Passing in Compiler Design
4.1.
Call by Values
4.1.1.
Code
4.1.2.
Output
4.2.
Call by Reference
4.2.1.
Code
4.2.2.
 
4.2.3.
Output
4.3.
Call by Copy Restore
4.3.1.
Code
4.3.2.
 
4.3.3.
Output
4.4.
Call by Name
4.4.1.
Code
4.4.2.
 
4.4.3.
Output
5.
Frequently Asked Questions
5.1.
What does parameter passing accomplish?
5.2.
Can arguments be passed in a GET request?
5.3.
Can we give parameters to the main function?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Parameter Passing in Compiler Design

Author Shivani Singh
1 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Hey Ninjas, have you ever thought about how the code execution is done in the compiler? There are many steps involved in the execution of any program. As you already know, there are many parameters passed in the code; how those parameters will be called in the memory is a fascinating topic to learn. 

Parameter Passing in Compiler Design

In this blog, we will discuss parameter passing in compiler design. But before that, let us know about the compiler. A compiler's job is to convert high-level source code into a low-level machine language without altering the meaning or functionality of the original code. A compiler's translation, analysis, and optimization processes are governed by a structure and set of rules known as compiler design. Now let us discuss the blog's main topic: parameter passing in compiler design.

Parameter Passing in Compiler Design

Parameter passing is an essential step in compiler design. Parameter passing refers to the exchange of information between methods, functions, and procedures. Some mechanism transfers the values from a variable procedure to the called procedure. 

Parameters are of two types: Actual Parameters and Formal Parameters. Let us discuss each of them in detail.

Parameter Passing in Compiler Design

Actual Parameter

Actual parameters are variables that accept data given by the calling process. These variables are listed in the called function's definition. They accept the calling process's data. The called function's definition contains a list of these variables. 

There is no need to specify the datatype in the actual arguments. They could be expressions, constants, or variables without regard to data types. The term "actual parameters" refers to the parameters handled during a function call.

 

Let us move to the next section of the blog to learn about formal parameters.

Formal Parameter

Formal parameters are variables whose values and functions are given to the called function. These variables are supplied as parameters in the function call. They must include the data type. 

They are the numbers listed in a subprogram's parameter index. Specifying the data type of the receiving value for formal parameters is necessary. Formal parameters are data-type-related variables. 

 

Now let us see an example of actual and formal parameters to understand them in a better way. 

Code

#include <bits/stdc++.h>
using namespace std;
// Function declaration
int add(int x, int y);
int main() {
	int x = 50;
	int y = 5;
	// Actual parameters
	int sum = add(x, y);
	cout << "Actual parameters are: " << x << " and " << y << endl;
	cout << "Result is: " << sum;
}
// Function definition
// Formal parameter
int add(int a, int b) {
	cout << "Formal parameters are: " << a << " and " << b << endl;
	int c = a + b;
	return c;
}

 

Output

Actual Parameter and Formal Parameter

Explanation

In the earlier example, the actual parameters x and y pass the real values of 50 and 5, respectively. They are passed as parameters in the calling function and are in the main method.

The formal parameters a and b are given here in the function specification. The value of the actual arguments is copied into the formal parameters, a given function is run, and the result is then returned to the calling function.

 

Let us move to the next section of the blog to learn some of the basic terminologies in parameter passing in compiler design.

Also see, Cross Compiler

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Basic Terminology in Parameter Passing in Compiler Design

Some basic terminologies in parameter passing in compiler design are the following:

R-value

The value of an expression is its r-value. An rvalue is a temporary object or literal without a permanent place in memory. They are basically placed on the right-hand side of the assignment operator. 

Some examples of R-values are given below:

Examples

a = 1;
b = a * 7;
c = b * 12;

 

Here, all variables, including a, b, and c, have R-values like 1, a * 7, and b * 12. Here, the variables do not have a permanent place in memory. 

L-value 

The L-Value of the expression refers to the location in memory where the expression is kept. Here the expression has a permanent memory location assigned. L-values are placed on the left side of an assignment operator. 

Some examples of L-value are given below:

Examples

x = 10;
a = 20;

 

In the above-mentioned examples, x and a are L-values, and the R-values are 10 and 20

Methods for Parameter Passing in Compiler Design

Methods for Parameter Passing in Compiler Design

There are four methods for parameter passing in compiler design. The four methods are given below:

Call by Values

When using call by value, the compiler adds the r-value of the actual parameters that were passed to the calling procedure to the called procedure's activation record. Any modifications made to the formal parameters do not affect the actual parameters because they include the values given by the calling procedure.

Let us see an example of call-by-values.

Code

#include <bits/stdc++.h>
using namespace std;
// Formal parameter
void swap(int x, int y) {
	int temp = x;
	x = y;
	y = temp;
}
int main() {
	// Actual parameter
	int x = 9;
	int y = 12;
	cout << "Values before swap: x= " << x << " y= " << y << endl;
	swap(x, y);
	cout << "Values after swap: x= " << x << " y= " << y << endl;
}

 

Output

Call by Values

Explanation

As we can see, even when we modify the contents of variables x and y defined in the scope of the swap function, the modifications do not affect the variables' definitions in the scope of the main. This is because main() will not be affected by changes performed in the swap() because we call swap() by value, which will receive separate memory for x and y.

Call by Reference

The formal and actual parameters in a call by reference relate to the same memory address. The activation record of the called function receives a copy of the L-value of the actual arguments. As a result, the address of the actual parameters is passed to the called function. 

If the actual parameters do not contain an L-value, they are evaluated in a new temporary location, and the location's address is passed. Since changes are made at the address, any modifications to the formal parameter are reflected in the actual parameters.

Let us see an example of a call by reference.

Code

#include <stdio.h>
// Call by reference
// Formal parameter
void swap(int *x, int *y) {
	int temp = *x;
	*x = *y;
	*y = temp;
}
int main() {
	// Actual parameter
	int x = 9;
	int y = 12;
	printf("Values before swap: x = %d, y = %d\n", x, y);
	swap(&x, &y);
	printf("Values after swap: x = %d, y = %d", x, y);
}

 

Output

Call by Reference

 

Explanation

As we can see, instead of int x, int y, we used int *x, int y, and instead of giving x,y, we gave &x,&y in the function call. Due to the usage of pointers as function parameters, which return the original parameters' address rather than their value, this practice is called by reference

The variables' addresses are given using the & operator, and the memory location to which the pointer is pointing is accessed using the * operator. The modifications done in the swap() reflect in main(), as shown in the output above because the variable function points to the same memory address as the original arguments.

Call by Copy Restore

In contrast to call-by-reference, changes to actual parameters are made when the called procedure completes. The actual parameter values are stored in the called procedure's activation record during the function call. 

The manipulation of formal parameters has no immediate impact on the actual parameters; however, the L-value of the formal parameters is copied to the actual parameters when the called procedure completes.

Let us disucss an example of a call by reference.

Code

#include <bits/stdc++.h>
using namespace std;
int a;
void func(int x) {
	//a is still 10
	x = 5;
	//a is now 5
	a = x;
}
//Function ends so the value of x is now stored in a and value of a is now 5 
int main() {
	a = 10;
	// When this ends the value of a will be 5
	func(a);
	cout << "The value of a is: " << a; //prints 5
} 

 

Output

Call by Copy Restore

Explanation

Value is passed into the function in the example above, but until the function is complete, it has no impact on the value of the passed-in variable. At that time, the function variable's final value is saved in the passed-in variable.

Call by Name

A new form of preprocessor-like argument parsing mechanism is offered by languages like Algol. Here, the procedure's body is called instead of the procedure's name. 

Let us see an example of a call by reference.

Code

#include <bits/stdc++.h>
using namespace std;
int a, b, c;
// Formal parameter
void swap(int a, int b) {
	int t;
	// These three swapping is done by compiler
	t = a;
	a = b;
	b = t;
	cout << a << " " << b << endl;
}
int main() {
	// Actual parameter
	int a = 9;
	int b = 12;
	cout << a << " " << b << endl;
	//cout<<"Values before swap: x= "<<x<<" y= "<<y<<endl;
	swap(a, b);
	cout << a << " " << b;
	//cout<<"Values after swap: x= "<<x<<" y= "<<y<<endl;
}

 

Output

Call by Name

Explanation

Here, the evaluation is done based on parameters. In all cases where formal parameters are used in the technique, actual parameters are used instead of formal ones.

Also see,  cousins of compiler

Frequently Asked Questions

What does parameter passing accomplish?

Parameter passing has a vital role in compiler design. Without using global variables or the requirement to create them, parameter passing enables the values of local variables in a main program to be read, modified, and utilized in many sub-programs.

Can arguments be passed in a GET request?

Yes, arguments can be passed in a GET request. We can add parameters to an HTTP get request by using the params property. Either HttpParams or an object with parameter key-value pairs can be sent. To better comprehend it, let's look at an illustration.

Can we give parameters to the main function?

Yes, we can. The main() function allows us to pass arguments. The values of the command line parameters are supplied to your program during program execution and are specified after the program name in the system's command line.

Conclusion

As we have reached the end of this blog, let us see what we have discussed so far. In this blog, we have discussed the basics of parameter passing in compiler design, its types, the basic terminologies, and methods for parameter passing in compiler design.

If you like to learn more, you can check out our articles: 

You may refer to our Guided Path on Code Studios for enhancing your skill set on DSACompetitive ProgrammingSystem Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Happy Learning, Ninja!

Live masterclass