Programming Functions in C part two

Previously we had discussed regarding the usage functions in C programming and this is a continuation for it.

Use of Functions in C programming

Writing functions avoids rewriting the same code over and over.

Explanation : Suppose we have a section of code in your program that calculates area of a triangle. If later in the program we want to calculate the area of a different triangle, we nee d not write the entire code once again and we would prefer to jump to a ‘section of code’ that calculates area and then jump back to the place from where you left off. This section of code is nothing but a function.

Using functions it becomes easier to write programs and keep track of what they are doing. If the operation of a program can be divided into separate activities, and each activity placed in a different function, then each could be written and checked more or less independently. Separating the code into modular functions also makes the program easier to design and understand.

We have to break a program into small units and write functions for each of isolated sub divisions. We can even write functions that are called only once and these functions perform some logically isolated task.

How to Pass Values between Functions ?

The mechanism used to convey information to the function is the ‘argument’. We use the arguments in the printf( ) and scanf( ) functions; the format string and the list of variables used inside the parentheses in these functions are arguments. The arguments are sometimes also called ‘parameters’.

Sending and receiving values between functions Example

Consider the following program where, in main( ) we receive the values of a, b and c through the keyboard and then output the sum of a, b and c.

The calculation of sum is done in a different function called calsum( ). If sum is to be calculated in calsum( ) and values of a, b and c are received in main( ), then we must pass on these values to calsum( ), and once calsum( ) calculates the sum we must return it from calsum( ) back to main( ).

main( )

{

int a, b, c, sum ;

printf ( "\nEnter any three numbers " ) ;

scanf ( "%d %d %d", &a, &b, &c ) ;

sum = calsum ( a, b, c ) ;

printf ( "\nSum = %d", sum ) ;

}

calsum ( x, y, z )

int x, y, z ;

{

int d ;

d = x + y + z ;

return ( d ) ;

}


And here is the output... Enter any three numbers 10 20 30 Sum = 60

Explanation

1.In this program, from the function main( ) the values of a, b and c are passed on to the function calsum( ), by making a call to the function calsum( ) and mentioning a, b and c in the parentheses: sum = calsum ( a, b, c ) ; In the calsum( ) function these values get collected in three variables x, y and z:

calsum ( x, y, z )

int x, y, z ;

2.The variables a, b and c are called ‘actual arguments’, whereas the variables x, y and z are called ‘formal arguments’. Any number of arguments can be passed to a function being called. The type, order and number of the actual and formal arguments must always be same.

3.The return statement serves two purposes:
1. On executing the return statement it immediately transfers the control back to the calling program.
2. It returns the value present in the parentheses after return, to th3e calling program.

4.There is no restriction on the number of return statements that may be present in a function. Also, the return statement need not always be present at the end of the called function.

5.Whenever the control returns from a function some value is definitely returned. If a meaningful value is returned then it should be accepted in the calling program by equating the called function to some variable.

6.If we want that a called function should not return any value, we must mention so by using the keyword void .

void display( )
{
printf ( "\nHeads I win..." ) ;
printf ( "\nTails you lose" ) ;

7.A function can return only one value at a time.

OTHER PROGRAMMING COURSES:
INTRODUCTION TO C PROGRAMMING

Programming with C an introduction part two

Data types for C programming


C PROGRAMMING CHARACTER SET

CONSTANTS IN C PROGRAMMING

PROGRAMMING C VARIABLES

C PROGRAM INSTRUCTIONS

COMPILATION AND EXECUTION OF C PROGRAM

C PROGRAMMING RULES PART ONE

C PROGRAMMING RULES PART TWO

COMPILATION AND EXECUTION OF C PROGRAM

INSTRUCTIONS TO WRITE C PROGRAM

ARITHMETIC INSTRUCTIONS TO WRITE C PROGRAM

CONVERSION OF CONSTANTS IN C PROGRAM

PRIORITY OF AR THEMATIC OPERATIONS IN C

OPERATORS ASSOCIATIVITY IN C

IF STATEMENT

MULTIPLE STATEMENTS IN IF

IF AND ELSE

NESTED IF AND ELSE


BREAK

CONTINUE AND DO WHILE IN C LANGUAGE

SWITCH IN C PROGRAMMING

FUNCTIONS IN C PROGRAMMING


Functions and usage in C part two

Coding in C functions


No comments:

Post a Comment