[go: up one dir, main page]

0% found this document useful (0 votes)
9 views14 pages

Lec 16

The document provides an introduction to programming concepts, focusing on parameter passing in functions, specifically Call by Value and Call by Reference. It explains the use of pointers, their syntax, and their significance in memory management and function modifications. The document includes examples and outputs to illustrate these concepts in practice.

Uploaded by

shekhargupta3435
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)
9 views14 pages

Lec 16

The document provides an introduction to programming concepts, focusing on parameter passing in functions, specifically Call by Value and Call by Reference. It explains the use of pointers, their syntax, and their significance in memory management and function modifications. The document includes examples and outputs to illustrate these concepts in practice.

Uploaded by

shekhargupta3435
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/ 14

Introduction to Programming

Dr. Tanmay Basu and Dr. Akash Anil


Dept. of Data Science and Engineering
Indian Institute of Science Education and Research Bhopal
Learning Objectives [Lecture 16]
❏ Parameter Passing in Function (Call by Value)
❏ Pointer
❏ Parameter Passing in Function (Call by Reference)
Function: Parameter Passing
We saw multiple examples where a function was passed one or more
parameters/arguments at the time of calling.

E.g., gcd(a, b), power(x, n), fact(n)


Where a,b,x, and n are variables.

In the above example, we passed the actual values of the variables.


Parameter Passing: Call by Value
❏ Pass the actual value while calling the defined
functions.
double power(double x, int n) {
❏ Example, power(x, n), where x is a double type
value and n is an integer value. if (n == 0)
❏ power(2, 3)
return 1;

So, how call by value work? if (n < 0)


Example, power(x, n) // Calling a function power return 1 / power(x, -n);

return x * power(x, n - 1);


A local copy of x, n is generated in the definition of power
function. }

All the previous examples used Call by Value Approach.


Find the Output void changeValue(int x, int y){

void main() { x = x+5;

int x=5, y=8; y = y+10;

printf("The x and y before printf("The x and y in


changeValue are %d, %d\n", x, y); changeValue are %d, %d\n", x, y);
changeValue(x, y); }
printf("The x and y after changeValue
in Main are %d, %d\n", x, y); Output
The x and y before changeValue are 5, 8
} The x and y in changeValue are 10, 18
The x and y after changeValue in Main
are 5, 8
void swap(int a, int b) {
Find the Output a = a + b;

int main() { b = a - b;

int x = 5, y = 10; a = a - b;

swap(x, y); printf("Inside swap function: a = %d, b


= %d\n", a, b);
printf("After swap function: x =
%d, y = %d\n", x, y); }

return 0; Output
Inside swap function: a = 10, b = 5
} After swap function: x = 5, y = 10
Completed so far…
Module 3: Functions: Introduction to Functions, Function Execution, Function
Prototyping and Definition, Parameter passing in functions (Call by Value, Call by
Reference), Recursion.

Note: Parameter Calling by Reference will be discussed in Module 4 after


understanding Pointer.

Module 4: Pointers: Introduction, Pointer Syntax, Pointer


Arithmetic, Pointers to Pointer, Function Pointer, Dynamic Memory
Allocation, sizeof [4L]
Pointer
❏ A variable which stores address of another variable.
❏ Pointer has Direct Memory Access. Useful in low level programming.
❏ Enables Call by reference where modifications can be made in called function
unlike Call by Value.
❏ Dynamic Memory Allocation to efficiently allocate memory.
What is pointer?
Revisiting Variable
Suppose, int roll_no = 125; is stored
at base address equal to 1024. 1027

So, the roll_no has a value equal to


125.

A pointer variable is capable of


1026

1025

1024
.
.
125

}
storing address of another variable
as value.
. …
0
What is pointer?
int roll_no = 125; P = 1024

A pointer variable is capable of storing


1027

}
address of another variable as value.
1026
Roll_no = 125

1025
Syntax: return_type *pointer_name;
1024
Int *p; (* : dereference operator) .
.
p = &roll_no; (&: Address of Operator) . …
0
// WAP to demonstrate pointer. Output
#include <stdio.h>
The roll no is 125
void main() { The address of roll no is
0x7ffe70c38c5c
int roll_no = 125; The address stored by ptr of roll no is
int *ptr = &roll_no; 0x7ffe70c38c5c
The value at address 0x7ffe70c38c5c
printf("The roll no is %d \n", roll_no); is 125
printf("The address of roll no is %p \n", &roll_no);

printf("The address stored by ptr of roll no is %p \n",


ptr);

printf("The value at address %p is %d \n", ptr, *ptr);

}
Parameter Passing: Call by Reference
❏ Pass the address while calling the defined
double power(double *x, int *n) {
functions.
❏ Example, power(&x, &n), where x is a double type if (*n == 0)
value and n is an integer value.
return 1;

So, how call by value work? if (*n < 0)


Example, power(&x, &n) // Calling a function power return 1 / power(*x, -*n);

return *x * power(*x, *n - 1);


No local copy of x, n is generated in the definition of
power function. Actual address is passed instead. }
Find the Output void changeValue(int *x, int *y){

void main() { *x = *x+5;

int x=5, y=8; *y = *y+10;

printf("The x and y before printf("The x and y in


changeValue are %d, %d\n", x, y); changeValue are %d, %d\n", *x, *y);
changeValue(&x, &y); }
printf("The x and y after changeValue
in Main are %d, %d\n", x, y); Output
The x and y before changeValue are 5, 8
} The x and y in changeValue are 10, 18
The x and y after changeValue in Main
are 10, 18
void swap(int *a, int *b) {
Find the Output *a = *a + *b;

int main() { *b = *a - *b;

int x = 5, y = 10; *a = *a - *b;

swap(&x, &y); printf("Inside swap function: a = %d, b


= %d\n", *a, *b);
printf("After swap function: x =
%d, y = %d\n", x, y); }

return 0; Output
Inside swap function: a = 10, b = 5
} After swap function: x = 10, y = 5

You might also like