Saturday, December 13, 2008

What???

What is an argument?

An argument is the value that is passed from the program to the function call. This can also be considered as input to the function from the program from which it is called.

How to declare a function passed with argument

Declaring a function:

The general format for declaring the function remains the same as before except the data type passed as arguments in functions are in the same order in which it is defined in function.

The format for declaring a function with arguments is:


return_datatype functionname(datatype1,datatype2,..);


In this example, the data types are the types of data passed in the function definition as arguments to the function. Care must be taken to mention the number of arguments and the order of arguments in the same way as in function definition.

For example, suppose a function named exforsys takes two integer values as arguments in its functions definition and returns an integer value. The function declaration of exforsys would be written:


int exforsys(int,int);


Function Definition:

The function definition has the same syntax as the function definition previously defined, but with added arguments. The general format for defining a function with arguments is written as:


return_datatype functionname(datatype1 variable1,datatype2 variable2,..)
{
………….
Program statements
………….
return( variable);
}


In the above example, the return data type defines the data type of the value returned by the function. The arguments are passed inside the function name after parentheses with the data type and the variable of each argument. Care must be taken to mention the number of arguments and the order of arguments in the same way as in function declaration.


For example, if the function exforsys takes two integer values x and y and adds them and returns the value z the function definition would be defined as follows:


int exforsys(int x,int y)
{
int z;
z=x+y;
return(z);
}


In the above program segment, a return statement takes the general format


return(variable) ;


This value specified in the return as argument would be returned to the calling program. In this example, the value returned is z, which is an integer value, the data type returned by the function exforsys is mentioned as int.


Calling the function:

The calling of function takes the same syntax as the name of the function but with value for the arguments passed. The function call is made in the calling program and this is where the value of arguments or the input to the function definition is given.


The general format for calling the function with arguments is


functionname(value1,value2,…);


In the above exforsys function suppose integer value 5 and 6 are passed, the function call would be as follows:


exforsys(5,6);


As soon as the function call exforsys is made the control, transfers to the function definition and the assignment of 5 to x and 6 to y are made as below.


int exforsys(int x,int y)


exforsys(5,6);


int b;
b = exforsys(5,6);


The above statement assigns the returned value of the function exforsys. The value z is then added to the value of x and y to the variable b. So, variable b takes the value 11.


Let us see the whole program to understand in brief the concept of function with arguments


The output of the above program would be


#include
int exforsys(int,int);
void main()
{
int b;
int s=5,u=6;
b=exforsys(s,u);
cout<<”\n The Output is:”<
}

int exforsys(int x,int y)
{
int z;
z=x+y;
return(z);
}

The Output is:11

This and That

FACTS on Functions and Structured Programming

By using functions in your C programs, you can practice structured programming, in which individual program tasks are performed by independent sections of program code. "Independent sections of program code" sounds just like part of the definition of functions given earlier, doesn't it? Functions and structured programming are closely related.

The Advantages of Structured Programming

Why is structured programming so great? There are two important reasons:

  • It's easier to write a structured program, because complex programming problems are broken into a number of smaller, simpler tasks. Each task is performed by a function in which code and variables are isolated from the rest of the program. You can progress more quickly by dealing with these relatively simple tasks one at a time.
  • It's easier to debug a structured program. If your program has a bug (something that causes it to work improperly), a structured design makes it easy to isolate the problem to a specific section of code (a specific function).

A related advantage of structured programming is the time you can save. If you write a function to perform a certain task in one program, you can quickly and easily use it in another program that needs to execute the same task. Even if the new program needs to accomplish a slightly different task, you'll often find that modifying a function you created earlier is easier than writing a new one from scratch. Consider how much you've used the two functions printf() and scanf() even though you probably haven't seen the code they contain. If your functions have been created to perform a single task, using them in other programs is much easier.

Planning a Structured Program

If you're going to write a structured program, you need to do some planning first. This planning should take place before you write a single line of code, and it usually can be done with nothing more than pencil and paper. Your plan should be a list of the specific tasks your program performs. Begin with a global idea of the program's function. If you were planning a program to manage your name and address list, what would you want the program to do? Here are some obvious things:

  • Enter new names and addresses.
  • Modify existing entries.
  • Sort entries by last name.
  • Print mailing labels.

With this list, you've divided the program into four main tasks, each of which can be assigned to a function. Now you can go a step further, dividing these tasks into subtasks. For example, the "Enter new names and addresses" task can be subdivided into these subtasks:

  • Read the existing address list from disk.
  • Prompt the user for one or more new entries.
  • Add the new data to the list.
  • Save the updated list to disk.

Likewise, the "Modify existing entries" task can be subdivided as follows:

  • Read the existing address list from disk.
  • Modify one or more entries.
  • Save the updated list to disk.

You might have noticed that these two lists have two subtasks in common--the ones dealing with reading from and saving to disk. You can write one function to "Read the existing address list from disk," and that function can be called by both the "Enter new names and addresses" function and the "Modify existing entries" function. The same is true for "Save the updated list to disk."

Already you should see at least one advantage of structured programming. By carefully dividing the program into tasks, you can identify parts of the program that share common tasks. You can write "double-duty" disk access functions, saving yourself time and making your program smaller and more efficient.