Computer Programming C: UNIT III: Functions and Pointers

Parameter Passing Methods

C Programming

In 'C' language, there are two ways that the parameters can be passed to a function, they are i) Call by value ii) Call by reference

PARAMETERS PASSING METHODS

In 'C' language, there are two ways that the parameters can be passed to a function, they are

i) Call by value

ii) Call by reference


1. CALL BY VALUE

This method copies the values of actual parameters into the formal parameters of the function. Here, the changes of the formal parameters cannot affect the actual parameters, because formal arguments are photocopy of actual arguments. Changes made in the formal arguments are local to the block of the called functions. Once control returns back to the calling function, the changes made disappear.


Example: Find the cube of given value.

/* Program to find the cube of given value */

#include <stdio.h>

int cube (int x)

main()

{

         /* Local definition */

         int n=5;

         /* Statements */

         printf("Cube of %d is..%d", n,cube(n));

} /* main */

int cube(int);  /* cube is a function to cubic the values */

{

         x=x*x*x;

         return(x);

} /* cube */

OUTPUT

Cube of 5 is.. 125

EXPLANATION: In this example, the value of argument to the function cube(), is copied to the parameter 'x'. The expression x= x*x*x is evaluated, and only the local variable 'x' is modified. The main program print the value of n and transfers the control to called function cube() and calling program get back the values from the called program cube(). The function cube() executes the block of statement and return the values to the main function.

 

2. CALL BY REFERENCE

Call by Reference is another way of passing parameters to the function. Here, the address of arguments are copied into the parameters inside the function, the address is used to access the actual arguments used in the call. Hence changes made in the arguments are permanent.

Here pointers are passed to function, just like any other arguments. Here, we need not declare the parameters as pointer type.

Example: Interchanging two values.

 /* Program to interchanging two values */

#include <stdio.h>

void interchange (int *a,int *b); /* Prototype declaration */

void main()

{

/* Local definition */

int i=5,j=10;

/* Statements */

clrscr();

printf("i and j values before interchange: %d %d\n",i,j);

interchange(&i,&j); /* pass address to function */

printf("i and j values after interchange : %d %d\n",i,j);

printf("i and j values after interchange in the main(): %d %d\n",i,j);

getch();

} /* main */

void interchange(int *a, int *b)

{

int t;

t=*a;

*a= *b;

*b=t;

} /* interchange */

OUTPUT

i and j values before interchange: 5 10

i and j values after interchange : 10 5

i and j values after interchange in the main(): 10 5

EXPLANATION: In this example, the variables i and j are assigned to values 5 and 0 respectively, then the interchange() function is called with the address of i and j and it interchanges the i and j values. In the program, we are passing addresses of formal arguments to the interchange() function. The pointers in the interchange() function operate on the actual argument through the pointer. So the changes made in the values are permanent.

 

Computer Programming C: UNIT III: Functions and Pointers : Tag: Computer Science : C Programming - Parameter Passing Methods


Computer Programming C: UNIT III: Functions and Pointers



Under Subject


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation



Related Subjects


English Essentials I

EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


தமிழர் மரபு - Heritage of Tamils

UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Calculus

MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I

PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Chemistry I

CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Makerspace

ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming Python

CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Fundamentals of Electrical and Electronics Engineering

EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Mechanical Engineering

ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Civil Engineering

CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing

CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I Laboratory

PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Applied Chemistry I Laboratory

CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming Python Laboratory

CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Engineering Drawing

ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electronics and Electrical Engineering

EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing - Laboratory

CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation