Computer Programming C: UNIT III: Functions and Pointers

Simple Sample C Programs Using Functions

C Programming

C Programs Using Functions - Computer Programming C: Functions

CASE STUDIES

(Programs Using Functions)

 

1. Write a user defined function that computes 'x' raised to the power of 'y'.

The problem 'x' raised to the power of 'y' means xy, for this the input values are 'x' and 'y'.

Example: 102 Means 10*10 Evaluates 100

 /* Programs to computes 'x' raised to the power of 'y' */

#include <stdio.h>

#include <conio.h>

main()

{

int x,y;

int power(int, int);

clrscr();

printf("Enter x and y values....");

scanf("%d %d", &x, &y);

printf("%d to the power of %d is.. %f",x,y,power(x,y));

} /* main */

int power(int x, int y) /* Power is a function with arguments */

{

float p=1.0;

if(y >=0)

       while(y‒‒)

       p*=x;

else

       while(y++)

       p/=x;

return(p);

} /* power */

OUTPUT

Enter x and y values.... 10 2

10 to the power of 2 is.. 100

EXPLANATION: The above program calculates the value of 'x' to the power of y using the user defined function power(), which has two parameters of the type int and these can be taken from the input device.

 

2. Example for nesting of function.

Nesting of functions is the process of writing the function within another

Example: Calculate the ratio for the given x,y,z values using x/(y‒z).

* Program to calculate the ratio for the given x,y,z values */

#include <stdio.h>

#include <conio.h>

{

main()

/* Local definition */

int x,y,z,

float ratio (int, int, int);

/* Statements */

printf("Enter x,y,z values.....");

scanf("%d %d %d", &x, &y, &z);

printf("Ratio is..... %f\n", ratio(x,y,z));

} /* main */

float ratio(int x, int y, int z) /* ratio() is a function with arguments */

{

int diff(int, int);

if(diff(y,z))

          return(x/(y‒z));

else

          return(0);

} /* ratio() */

diff(int m,int n) /* diff() is a function with arguments */

{

if(m!=n)

      return(1);

else

       return(0);

} /* diff() */

OUTPUT

Enter x,y,z values... 10 10 5

Ratio is..... 2.000000

EXPLANATION: In the program the main function reads the values for x, y, z and calls the ratio() function to calculate x/(y‒z). The ratio can not be evaluated if (y‒z)=0, then the ratio function calls another function called diff() to find the difference between (y‒z) is zero or not.

 

3. Demonstration of Global variable

The multifunction program given below, demonstrates the global variable. The variable declared before the main() function is the global variable, the scope and availability of this variable is entire program.

/* Program to demonstration of global variable */

#include <stdio.h>

int a; /* global variable */

main()

{

a=39;

int f1();

int f2();

int f3();

printf("A..... %d\n", a);

printf("A..... %d\n", fl());

printf("A..... %d\n", f2());

printf("A..... %d\n",f3());

} /* main */

f1()

{

        a=a+10;

        return(a);

} /* fl */

f2()

{

        a=a+20;

        return(a);

} /* f2 */

f3()

{

        a=a+30;

        return(a);

} /* f3 */

OUTPUT

A.....39

A.....49

A.....69

A.....99

EXPLANATION: In the above program the variable a is declared as global declaration. This variable a is available any where in the program. The user‒defined functions f1(), f2(), and f(3) are accessed the a and executed the body of the statement then, it returns to the main program. Note that in all user‒defined functions the value of a does not change, hence it is global variable.

 

Computer Programming C: UNIT III: Functions and Pointers : Tag: Computer Science : C Programming - Simple Sample C Programs Using Functions


Computer Programming C: UNIT III: Functions and Pointers



Under Subject


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation



Related Subjects


English Essentials I

EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


தமிழர் மரபு - Heritage of Tamils

UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Calculus

MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I

PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Chemistry I

CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Makerspace

ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming Python

CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Fundamentals of Electrical and Electronics Engineering

EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Mechanical Engineering

ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Civil Engineering

CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing

CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I Laboratory

PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Applied Chemistry I Laboratory

CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming Python Laboratory

CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Engineering Drawing

ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electronics and Electrical Engineering

EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing - Laboratory

CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation