Sunday 8 March 2015

Passing Variables to Functions in C


You can use as many functions as you need in your programs and you can call a function from another or even from it-self. In order to avoid errors when using functions, you have to have a clear understanding about the mechanism of passing variables from one function to another.

The Scope of a Variable

A variable has a scope, which determines how long it is available to your program (or function) and where it can be accessed from. Variables declared within a block are scoped only to that block; they can be accessed only within that block and go out of existence when the execution of the block is completed.

A variable declared inside a function is called a local variable . Scope of a local variable is limited to the function. Such variables are not seen by any other functions including the main function. When you pass a variable to a function (such as the variable radius in Program-7.1), you are actually passing a copy of the variable (called passed by value), but not the variable it self. This means that the value of the passed variable cannot be changed by any other function. Even if you use another variable with the same name in the function, you still have two local variables isolated from each other. On the other hand you can also define global variables which are accessible from any function within the same source file. The global variable can be defined within the program but anywhere outside function block including the main function. For example you can define a global variable “discount” as follows:

#include <stdio.h>
float discount; //global variable
float sub_total(float total); //function prototype

int main()
{
....
A variable defined in such a manner is visible to the main function and the “sub_total” function. You can modify its value in either place and read it from another place.

A well written C source code should not have any global variables unless they are specifically required since an accidental change to a global variable may produce erroneous results.

Default Parameters

For every parameter you declare in a function prototype and declaration the calling function must pass in a value. The value passed must be of the declared type. Thus if you have a function declared as:

long my_function(int a);

the function must in fact take an integer value as an input. If the function definition differs or if you pass a value of a wrong data type you will get a compilation error. However when you declare the function prototype you can define a default value for the parameter as follows: 

long my_function(int a = 50);


The default value is used if no input is supplied to the function. Similarly a function with many input parameters can have a default value for each parameter.

Exercise 7.1 – Predict the output of each printf function in Program-7.3.

/*Program-7.3 */
#include <stdio.h>
void swap1(int x,int y);
void swap2(int x);
int a;
int main()
{
int b,c;
a=5;
b=10;
c=15;
printf("\nValue before 1st function a= %d, b= %d c= %d" ,a,b,c);
swap1(b,c);
printf("\nValue after 1st function a= %d, b= %d c= %d" ,a,b,c);
swap2(b);
printf("\nValue after 2nd function a= %d, b= %d c= %d" ,a,b,c);
printf("Test");
return 0;
}
void swap1(int x, int y)
{
int z;
z = x;
x = y;
y = z;
}
void swap2(int x)
{
int z;
z = x;
x = a;
a = z;
}


No comments:

Post a Comment