Passing values between functions

-- Leave a Comment
The function that we used in previous article is of no use as we can write those piece of words in the main functions insist of calling the other function. those functions always perform the same task. It will be very useful if we can provide some more instruction to them and the work accordingly.f t

Here is the general format of the function that can get value and perform the task.
return_type function name(dataype argument1, datatype argument2)
{
local veriable deceleration
statememts;
return;
}

It will be clear if showing some example insist of formate so here goes a sample example. we will describe it later.

#include<stdio.h>
int add(int, int, int);
int main()
{
int a,b,c,sum;
printf("Enter the three numbers:- ");
scanf("%d %d %d",&a, &b, &c);
sum = add(a,b,c);
printf("Finally the sum is %d",sum);
return 0;
}
int add(int x, int y, int z)
{
int ans=x+y+z;
return(ans);
}
Here goes Output
Enter the the three numbers:- 1 2 3
Finally the sum is 6

Now We will define the program.
Int add(int, int, int)
As mentioned earlier we should Declare that the function add will be used in the program. we should define the data type it can receive.
Then add(a,b,c) Here the variables a b and c are passed to the functions add and a,b and c are actual arguments.
Then int add(int x, int y, int z) Here the variables value passed are stored in x,y and z. and are known as formal arguments.
return(ans); will return the value to the the function it was called.
sum will now get the value and finally printed out.

After calling a function the calling function wait till the function run out of statements to be excited. If you need any help on programs or any confusion please let us know form the comment box below.

Hello This is Sagar Devkota. Studying Bachelor of Software Engineering at Pokhara University. I know something about Linux, Android, Java, Nodejs, Unity3D and 3 dots :)

0 comments:

Post a Comment