Sunday 8 December 2013

Fundamental of computer programming


Fundamental of Computer Programming

Objective

Functions, Pointers

Functions:
A function is a self-contained block of statements that perform a task of some kind.
Arguments are passed to the function, the function performs a task using the arguments, and the function returns a value back to the main program. Functions break a program into multiple parts, with each part performing a specific task.

Parts of a Function
The program multiplies two integers. Although multiplying two integers in C is very easy, a function is used here to demonstrate their use with a simple code example. The different parts of a function are commented.

Code Example:

#include <stdio.h>
#include<conio.h>
int mult(int n1, int n2);            // function declaration (function prototype)
int main()
{
int a,b,output;
printf("Enter integer a: ");
scanf("%d",&a);
printf("Enter integer b: ");
scanf("%d",&b);
output = mult(a,b);     //  function call
printf("a*b = %d\n",output);
getch();
return 0;
}

int mult(int n1, int n2)     // function definition
{
    int result;
    result=n1*n2; 
    return result;    // function return value
}

Declaration:
The function declaration informs the compiler about relevant properties of the function (much like a variable declaration). The declaration includes the type of value it returns (such as int or float), the function name, and a list of input parameters it expects.
It is possible for a function to not return a value. Such a function must use the "void" type. In a void function, the "return" statement is optional.
void message( );   //function declaration which return no value and takes no parameters
Call:
The function call is where the main ( ) calls the mult function. The variable input is passed to the function. Note that input is an int, which is what mult was expecting (as seen in the function declaration). The main ( ) becomes the “calling” function and mult becomes the “called” function.

Definition:
This is where the actual code for the function is located. Note that the first line in the definition of function matches the function declaration except for the semicolon.

Return Value:
This is the result that is passed back to the caller function. The return value must have the same type as the function; in this case the function mult has type int, so the return value must be of type int.

Pointers:
‘&’ is called address operator. Pointer operator available in C is “*”, called ‘value at address’ operator. It gives the value stored at a particular address. The “value at address” operator is also called “indirection” operator.
int main( )
{
int i = 3 ;
printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nValue of i = %d", i ) ;
printf ( "\nValue of i = %d", *( &i ) ) ;
getch( );
return 0;
}
The output of the above program would be:
Address of i = 65524
Value of i = 3
Value of i = 3
Note that printing the value of *(&i) is same as printing the value of i.
The expression &i gives the address of the variable i. This address can be collected in a variable, by saying,
int *j;
j = &i ;
But remember that j is not an ordinary variable like any other integer variable. It is a variable that contains the address of other variable (i in this case). Since j is a variable the compiler must provide it space in the memory.
Pass by reference and pass by value
Whenever we called a function and passed something to it we have always passed the ‘values’ of variables to the called function. Such function calls are called ‘calls by value’. By this we mean is, on calling a function we are passing values of variables to it. The examples of call by value are shown below:
sum = calsum ( a, b, c ) ;

Instead of passing the value of a variable, pass the location number (also called address) of the variable to a function. Such functions calls are called ‘call by reference’.

Code Example:

//Call by value
void swapv(int x , int y);
int main( )
{
int a = 10, b = 20 ;
swapv ( a, b ) ;
printf ( "\na = %d b = %d", a, b ) ;
getch( );
return 0;
}

void swapv ( int x, int y )
{
int t ;
t = x ;
x = y ;
y = t ;
printf ( "\nx = %d y = %d", x, y ) ;
}
The output of the above program would be:
x = 20 y = 10
a = 10 b = 20
Note that values of a and b remain unchanged even after exchanging the values of x and y.
//call by reference
       void swapv(int x , int y);
int main( )
{
int a = 10, b = 20 ;
swapr ( &a, &b ) ;
printf ( "\na = %d b = %d", a, b ) ;
getch( );
return 0;
}
swapr( int *x, int *y )
{
int t ;
t = *x ;
*x = *y ;
*y = t ;
}
The output of the above program would be:
a = 20 b = 10
Note that this program manages to exchange the values of a and b using their addresses stored in x and y.



TASKS:

Write a function to calculate the factorial value of any integer entered through keyboard. Function receive integer value in main ( ) and prints results in function.


Create a simple calculator using functions. Write separate functions for addition, multiplication, subtraction, division, modulus and power function. Each function takes three arguments, two of the arguments are integers and third argument is an integer pointer argument which stores the result of operation on first two arguments. Each function declaration and definition contain return-type void.

No comments:

Post a Comment