Home | Lab 1 | Lab 2 | Lab 3 | Lab 4 | Lab 5 | Lab 6 | Lab 7 | Lab 8 | Lab 9 | Lab 10 | Lab 11 | Lab 12

Linked at EntranceNepal.com, i-geek blog & Ekendra's Share World

Object I. To write Program to perform arithmetic assignments using function

Object II. To write to display reverse of a given number using function

Object III. To write a program to calculate factorial of a given number using recursive function

Download this full program | Lab 10 | in pdf format

Background Theory:

Functions: A function is simply a chunk of C code (statements) that you have grouped together and given a name. The value of doing this is that you can use that "chunk" of code repeatedly simply by writing its name. A function is a self-contained program segment that carries out some specific, well defined tasks. Every C program consists of one or more functions. One of these functions must be called main. Execution of the program will always begin by carrying out the instructions in main. Additional functions will subordinate to main, and perhaps to one another.

 A function that you define without parameter information is compatible with a function prototype that specifies:

  a compatible return type

  the same (fixed) number of arguments

  a parameter of promoted type for each parameter in the definition

 A function will carry out its intended action whenever it is accessed ie a function calling, from some other portion of the program. The same function can be accessed from several different places within a program. Once the function has carried out its intended action, control will be returned to the point from which the function was accessed.

Generally, a function will process information that is passed to it from the calling portion of the program, and return a single value. Information is passed to the function via special identifiers called arguments (= parameters) and returned via the return statement. Some functions however, accept information but do not return anything (as, the library function printf), whereas other functions (eg, the library function scanf) return multiple values.

 When you call a function, control passes to the first statement within the block art of a function definition. Except for the jump statements (break, continue, goto, and return), each statement within a block passes control (after it has completed its execution) to the next statement within the block. Some statements can execute a contained statement repeatedly, and some can execute a contained statement only when a certain condition is true, but all these statements pass control to the next statement within the block, if any. If a next statement is not within the block, control passes to the statement following the block.

 Because no statement follows the block part of a function definition, the translator inserts a return statement (without an expression) at the end of that block. A return statement returns control to the expression that invoked the function.

 Recursion: This is a process by which a function calls itself repeatedly, until some specified condition had been satisfied. The process is used for repetitive computations in which each action is stated in terms of a previous result. Many iterative/repetitive problems can be written in this form.

 

Code:

#include<stdio.h>

#include<conio.h>

add(int a, int b)      //global function declarations//

{

            return a+b;

}

 

sub(int a, int b)

{

            return a-b;

}

 

mult(int a, int b)

{

            return a*b;

}

 

div(int a, int b)  

{

            return a/b;

}

 

void main()

{

            int sum, pro, division, a, b, reduction;

            clrscr();

            printf("\nEnter any two numbers separated by a comma:");

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

            printf("\nYou entered a= %d and b= %d\n", a, b);

            sum=add(a,b);

            printf("Sum= %d", sum);

            reduction=sub(a,b);

            printf("\nReduction= %d", reduction);

            division=div(a,b);

            printf("\nDivision= %d", division);

 

            printf("\nMultiplication= %d", mult(a,b));

            getch();

}

#include <stdio.h>

#include <conio.h>

{

            reverse=rev(n);//function call//

            printf("\nReverse of given number: %d",reverse);

            getch();

}

 

void main()

 

int rev(int n)

{

            Printf(Enter a number\t):

            int i, j=0;

 

            do

{

            n=n%10;

            j=j*10+n;

            n=n/10;

}

            while(n>0);

            return j;

}

#include<stdio.h>

#include<conio.h>

void main()

{

            int num;

            long factorial, fact();

            clrscr();

            printf("\nEnter a number: ");

            scanf("%d", &num);

 

            factorial=fact(num);    //function call/

            printf("\nFactorial of %d= %ld", num, factorial);

            getch();

}

            long fact(int num)

            {

                        int i;

                        long factorial=1;

                        for(i=1;i<=num;i++)

                        factorial=factorial*i;

                        return (factorial);

            }

To Download this full program | Lab 10 | in pdf format you must have PDF reader as Adobe Acrobat Reader 5 and above in your computer,

click here to download now.

Search yourself for more such codes

Google

Home | Lab 1 | Lab 2 | Lab 3 | Lab 4 | Lab 5 | Lab 6 | Lab 7 | Lab 8 | Lab 9 | Lab 10 | Lab 11 | Lab 12

Protected under Creative Commons License.
Written by Ekendra Lamsal, www.ekendralamsal.com
for comments email at gmail address:
gearShifts@gmail.com