0% found this document useful (0 votes)
19 views11 pages

Unit 3

The document explains key programming concepts including call-by-value and call-by-reference, highlighting their differences in parameter passing. It also differentiates local and global variables, defines recursion and its characteristics, and discusses function types in C programming, including library and user-defined functions. Additionally, it covers methods for passing arrays and 2D arrays to functions, along with examples and sample code.

Uploaded by

shanmukhgara48
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views11 pages

Unit 3

The document explains key programming concepts including call-by-value and call-by-reference, highlighting their differences in parameter passing. It also differentiates local and global variables, defines recursion and its characteristics, and discusses function types in C programming, including library and user-defined functions. Additionally, it covers methods for passing arrays and 2D arrays to functions, along with examples and sample code.

Uploaded by

shanmukhgara48
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

UNIT -3

(PART 2)

1. Differentiate call-by-value and call-by-reference methods.

Call By Value
In call by value method of parameter passing, the values of actual parameters are copied to
the function’s formal parameters.

• There are two copies of parameters stored in different memory locations.


• One is the original copy and the other is the function copy.
• Any changes made inside functions are not reflected in the actual parameters of the
caller.

Call by Reference
In call by reference method of parameter passing, the address of the actual parameters is
passed to the function as the formal parameters.

• Both the actual and formal parameters refer to the same locations.
• Any changes made inside the function are actually reflected in the actual parameters
of the caller.

2.Differentiate local and global variables with examples.

GLOBAL VARIABLE

• Global variables are those variables which are declared outside of all the functions or block
and can be accessed globally in a program.
• It can be accessed by any function present in the program.
• Once we declare a global variable, its value can be varied as used with different functions.

• The lifetime of the global variable exists till the program executes. These variables
are stored in fixed memory locations given by the compiler and do not automatically
clean up.

#include<stdio.h>

int a=50, b=40;


void main()

printf("a = %d and b=%d",a,b);

In the above example, a and b are the global variables.

LOCAL VARIABLE

• Variables that are declared within or inside a function block are known as Local variables.
• These variables can only be accessed within the function in which they are declared.
• The lifetime of the local variable is within its function only, which means the variable
exists till the function executes. Once function execution is completed, local variables are
destroyed and no longer exist outside the function.

• The reason for the limited scope of local variables is that local variables are stored
in the stack, which is dynamic in nature and automatically cleans up the data stored
within it.

#include<stdio.h>

void main()

int x=50, y=40;

printf("x = %d and y=%d",x, y);

In the above example, we have declared x and y two variables inside the main function.
Hence these are local variables.

3) What is recursion? List its characteristics.

The process in which a function calls itself directly or indirectly is called recursion and the
corresponding function is called a recursive function.

Characteristics:

• Performing the same operations multiple times with different inputs.


• In every step, we try smaller inputs to make the problem smaller.
• A recursive algorithm must call itself, recursively.
• A recursive algorithm must have a base case.
-Base condition is needed to stop the recursion otherwise infinite loop will occur.

-A recursive algorithm must change its state and move toward the base case.

6)Differentiate built-in functions and user-defined functions.

8) Differentiate Recursion and Iteration in four aspects.


ESSAY QUESTIONS:

1)Define a function in programming. List and explain various function


calling methods in programming.

Function: In programming, a function is a self-contained block of code that performs a


specific task or a set of tasks. Functions are designed to be reusable and modular, allowing
developers to break down complex programs into smaller, manageable pieces. A function
usually takes input parameters, performs operations, and returns a result.

Function Calling Methods:

When calling or invoking a function in programming, various methods are used to pass
arguments and control the flow of execution. The primary methods include:

1. Call-by-Value:
o In Call-by-Value, the actual values of the arguments are passed to the
function.
o The function works with copies of the values, and changes made to the
parameters inside the function do not affect the original values in the calling
code.
o Example: Used in many programming languages by default.

#include<stdio.h>

void change(int num) {

printf("Before adding value inside function num=%d \n",num);

num=num+100;

printf("After adding value inside function num=%d \n", num);

int main() {

int x=100;

printf("Before function call x=%d \n", x);

change(x);//passing value in function

printf("After function call x=%d \n", x);

return 0;
}

Output
Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200
After function call x=100

• Call-by-Reference (or Call-by-Address):

• In Call-by-Reference, a reference or memory address of the actual variables is passed


to the function.
• Changes made to the parameters inside the function directly affect the original
variables in the calling code.
• Example: Used in languages that support pointers or references.

#include<stdio.h>

void change(int *num) {

printf("Before adding value inside function num=%d \n",*num);

(*num) += 100;

printf("After adding value inside function num=%d \n", *num);

int main() {

int x=100;

printf("Before function call x=%d \n", x);

change(&x);//passing reference in function

printf("After function call x=%d \n", x);

return 0;

Output
Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200
After function call x=200

2)Explain call-by-value and call-by-reference with a sample code and give your
differentiations. (same as above)
3)What is recursion? List its advantages. Write a sample program that
demonstrates recursion program.

The process in which a function calls itself directly or indirectly is called recursion and the
corresponding function is called a recursive function.

Advantages of recursion

1. The code may be easier to write.

2. To solve such problems which are naturally recursive such as tower of Hanoi.

3. Reduce unnecessary calling of function.

4. Extremely useful when applying the same solution.

5. Recursion reduce the length of code.

6. It is very useful in solving the data structure problem.

#include <stdio.h>

int fact (int);

int main()

int n,f;

printf("Enter the number whose factorial you want to calculate?");

scanf("%d",&n);

f = fact(n);

printf("factorial = %d",f);

int fact(int n)

if (n==0)

return 0;
}

else if ( n == 1)

return 1;

else

return n*fact(n-1);

Output
Enter the number whose factorial you want to calculate?5
factorial = 120

4)Write a recursive method that finds the factorial of a number.

(same program as above)

5)Explain how to pass an array to the function with an example.

First way:

1. return_type function(type arrayname[])

Second way:

1. return_type function(type arrayname[SIZE])

Third way:

1. return_type function(type *arrayname)

// Program to calculate the sum of array elements by passing to a function

#include <stdio.h>
float calculateSum(float num[]);

int main() {
float result, num[] = {23.4, 55, 22.6, 3, 40.5, 18};

// num array is passed to calculateSum()


result = calculateSum(num);
printf("Result = %.2f", result);
return 0;
}

float calculateSum(float num[]) {


float sum = 0.0;

for (int i = 0; i < 6; ++i) {


sum += num[i];
}

return sum;
}

6)Explain how to pass a 2D array to the function with an example.

//Pass two-dimensional arrays


#include <stdio.h>
void displayNumbers(int num[2][2]);

int main() {
int num[2][2];
printf("Enter 4 numbers:\n");
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
scanf("%d", &num[i][j]);
}
}

// pass multi-dimensional array to a function


displayNumbers(num);

return 0;
}

7) Types of Functions in C Programming


Functions in C programming are classified into two types:

1. Library Functions

Also referred to as predefined functions, library functions are already defined in the C
libraries. This means that we do not have to write a definition or the function’s body to call
them. We can simply call them without defining them as they are already defined. However,
we need to include the library at the beginning of the code for calling a library function. We
can then use the proper syntax of the function to call them. Printf(), scanf(), ceil(), and floor()
are examples of library functions.

2. User-Defined Functions

These are the functions that a developer or the user declares, defines, and calls in a program.
This increases the scope and functionality, and reusability of C programming as we can
define and use any function we want. A major plus point of C programming is that we can
add a user-defined to any library to use it in other programs.

8) Aspects of Functions in C Programming


Functions in C programming have three general aspects: declaration, defining, and calling.
Let’s understand what these aspects mean.

1. Function Declaration

The function declaration lets the compiler know the name, number of parameters, data types
of parameters, and return type of a function. However, writing parameter names during
declaration is optional, as you can do that even while defining the function.

2. Function Call

As the name gives out, a function call is calling a function to be executed by the compiler.
You can call the function at any point in the entire program. The only thing to take care of is
that you need to pass as many arguments of the same data type as mentioned while declaring
the function. If the function parameter does not differ, the compiler will execute the program
and give the return value.

3. Function Definition

It is defining the actual statements that the compiler will execute upon calling the function.
You can think of it as the body of the function. Function definition must return only one
value at the end of the execution.

Here’s an example with all three general aspects of a function.

#include <stdio.h>

// Function declaration

int max_Num(int i, int j){

// Function definition

if (i > j)

return i;
else

return j;

// The main function. We will discuss about it later

int main(void){

int x = 15, y = 20;

// Calling the function to find the greater number among the two

int m = max_Num(x, y);

printf("The bigger number is %d", m);

return 0;

Output:
The bigger number is 20

You might also like