PASSING VALUES BETWEEN FUNCTIONS…

Making a communication between calling and called function is called passing values to function. We can modify any user-defined function by passing our own values to it for processing. You must have used format strings and list of variables in printf() and scanf() statement inside the parentheses. These are called arguments. Arguments are the mechanism for passing values between function.

Let’s consider an example-:

Program-: WAP to find sum of two integer numbers using functions.
Coding-:

#include<stdio.h>
int add(int a, int b); //Function prototype declaration
i
nt main()
{
int l,m,sum;
printf(“Enter the numbers\n”);
scanf(“%d%d”,&l,&m);
sum=add(l,m); //Function Call
printf(“sum=%d\n”,sum);
return 0;
}
int add(int a, int b) //Function Definition
{
int s;
s=a+b;
return(s);
}

OUTPUT-:

Here is the output of above written code.

Let’s understand working of this program-:

  • Function prototype declaration-: In prototype declaration we have to declare values which we are going to use in the function and return type of function while processing of code. Here add is the function and we want to add two integer numbers, therefore we have declared add(int a, int b) i.e., we are passing values of two integers to the function. Here int add represents that function add is returning a integer value. If we write void add than it will show that function add will return a null value.
    In basic functions we used to write void text(); as a function prototype declaration which means no values are being passed to function and function is returning a null value.
  • Function call-: Till the compiler comes to function call, we have given input of two numbers l, m. Now add(l,m); is the function call. It represents that the add function is called and values of l and m which are given by users are to be passed to function add for processing.
    Here code written in function definition will use values of l and m for processing.
    Variables l, m are called actual arguments and a, b are called formal arguments.
  • Function Definition-: Compiler sees that function add is being called and it jumps to definition of function add. Here int add(int a, int b) is function definition. It represents that two integer values are passed to the function. While calling the function we used add(l,m); which means call function “add” by passing values of l and m in it. Therefore values of a and b are considered same as values of l and m.
    When we add s=a+b; compiler actually add values of l and m because function is called by passing values of l and m in it.
    Now as we already told to the compiler that function is returning an integer value therefore we must introduce a return statement. Here return(s); means whole function add is returning value s and use this value of s where needed.
  • sum=add(l,m); statement assigns returned value of s to the sum. Using printf statement we have printed the value on screen.
  • Note– We can use a and b instead of l and m because compiler will treat them as different. This is because they are in different functions.

Some important points to be noted-:

  • There are no restrictions on number of return statement that may be present in function.
  • When we execute return statement, it automatically transfer control to calling function.
  • Only one value can be returned at a time by any function.
    return(a,b) is invalid.
  • If values of formal arguments are changed in called function, no cahnge happens in calling function.

I hope you have now understood how to pass values between function. If you have still any doubt you can ask in comment section. There is lot more in this topic but i have covered basic portion of this.

Leave a comment

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started