StudyLover
  • Home
  • Study Zone
  • Profiles
  • Typing Tutor
  • Contact us
  • Sign in
StudyLover Parameter Passing in Functions
Download
  1. C Programming
  2. Unit 3: Modular Programming with Functions, Arrays & Recursion
Functions : Call by Value
Unit 3: Modular Programming with Functions, Arrays & Recursion

1. What is Parameter Passing?

Parameter Passing is the method used to send data into a function so that the function can use it.

Think about a function like add(a, b). It's a block of code designed to add two numbers. But it doesn't know *which* two numbers to add until you tell it. When you "call" the function, you must "pass" it the numbers you want it to work with.

Parameter passing is the mechanism that connects the variables in your main() function (or any calling function) to the variables inside the function you are calling.

2. Why is it Necessary? (Function Scope)

By default, functions in C are isolated. A function (like add) has no knowledge of the variables inside your main function. This isolation is called "scope". The add function has its own "local scope," and main has its own "local scope."

This is a good thing! It stops functions from accidentally changing each other's variables. Parameter passing is the controlled, official way to bridge this gap and hand information from one scope to another.

3. Key Terminology: Parameters vs. Arguments

These two words are often used together, but they mean different things:

A. Parameters

Parameters are the variables declared in the function's definition. They are like empty "slots" or "placeholders" that the function expects to receive.

// 'a' and 'b' are PARAMETERS

int add(int a, int b) {

    return a + b;

}

B. Arguments

Arguments are the actual values (or variables) you provide when you call the function. They are the "data" you put into the "slots."

int main() {

    int x = 10;

    int y = 20;

    

    // 10 and 20 are ARGUMENTS

    int sum1 = add(10, 20); 

    

    // 'x' and 'y' are ARGUMENTS

    int sum2 = add(x, y); 

}

Simple Analogy: A Recipe

·         Function: A "Recipe" (e.g., makeCake)

·         Parameters: The ingredient list in the recipe (e.g., cupsOfFlour, numberOfEggs)

·         Arguments: The actual ingredients you use (e.g., 2 cups of flour, 3 eggs)

·         Function Call: The act of "making the recipe" (e.g., makeCake(2, 3))

4. How it Works (General Flow)

When you call a function like add(x, y):

1.  The program temporarily pauses the main function.

2.  It goes to the add function.

3.  It creates the parameter variables a and b.

4.  It performs the "parameter passing" step: it copies or links the data from the arguments (x and y) into the parameters (a and b).

5.  The code inside add runs, using the values in a and b.

6.  The function hits the return statement, sending a value back.

7.  The main function unpauses and receives the returned value.

The specific way the data is copied in step 4 is what you will learn about next. C has different methods for passing data, and they have very different results.

 

Functions Call by Value
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website