Computer Programming C: UNIT III: Functions and Pointers

Function Prototypes

C Programming

The functions are classified into the following types depending on whether the arguments are present or not and whether a value is returned or not. These are also called function prototypes.

FUNCTION PROTOTYPES

 

INTRODUCTION

The functions are classified into the following types depending on whether the arguments are present or not and whether a value is returned or not. These are also called function prototypes.

In 'C', while defining user‒defined function, it is a must to declare its prototype. A prototype statement keeps the compiler to check the return type and arguments types of the function.

A function prototype declaration consists of the function's return type, name and arguments list. It is always terminated with semicolon. If the programmer makes mistake, the compiler flags an error message. The following. are the function prototypes.

i)  Function with no arguments and no return value.

ii) Function with arguments and no return value.

iii) Function with arguments and with return value.

iv) Function with no arguments and with return value.

 

1. Function with no arguments and no return values

In this prototype, no data transfer takes place between the calling function and the called function. i.e., the called program does not receive any data from the calling program and does not send back any value to the calling program.

The function is only executed and nothing is obtained. Such functions are used to perform only operation, they act independently. They read data values and print result in the same block.

Syntax:


The dotted line indicates that, there is only transfer of control but no data transfer.

Example 1: Addition of two numbers.

/* Program to addition of two numbers */

#include <stdio.h>

main()

{

      void add(void);

      add(); /* add is a function with no arguments */

} /* main **/

void add()

{

      int a, b, c ; /** Local definitions */

      /* Statements */

      printf("Enter two numbers...");

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

      c=a+b:

      printf("Sum is... %d",c);

} /* add */

OUTPUT

Enter two numbers... 10 20

Sum is... 30

EXPLANATION: In the above program, user‒defined function add() is defined. The main function calls the add() function. The user‒defined function add() executed the block of statements and prints the output in same block. There is no argument sent from main function and no values are returned from the add() function.

Example 2: Program to print addition, subtraction and multiplication values using functions.

/* Program to print addition, subtraction and multiplication values */

#include <stdio.h>

#include <conio.h>

void main()

{

void add(), sub(), mul(); /*Local definitions */

/* Statements */

clrscr();

add(); /*function call */

sub(); /* function call */

mul(); /*function call */

} /*main */

void add()

{

int a=10,b=5;

printf("The addition value is: %d\n", a+b);

} /*add */

void sub()

{

int a=10,b=5;

printf("The subtract value is : %d\n",a‒b);

} /*sub */

void mul()

{

int a=10,b=5;

printf("The multiplication value is : %d\n", a*b);

getch();

} /*mul */

OUTPUT:

The addition value is: 15

The subtract value is : 5

The multiplication value is: 50

EXPLANATION: In the above program three user defined function add(), sub(), mul(). The main function calls these three functions. These functions are called as per the calling sequence and print the result as output.

 

2. Function with arguments and no return values

In this prototype, data is transferred from calling function to called function. i.e., The called program receives some data from the calling program and does not send back any values to calling program. (One way communication). Such functions are partly dependent on the calling function.

Syntax:


The continuous line indicates data transfer and dotted line indicates transfer of control.

Example 1: Addition of two numbers.

 /* Program for addition of two numbers */

#include <stdio.h>

main()

{

        void add(int, int); /* Local definition */

        int a,b;

        printf("Enter two values: ");

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

        add(a,b); /* add is a function with arguments */

} /* main */

void add(int x, int y)  /* add is a function with no return values */

{

        /* Local definition */

        int z;

        /* Statements */

        z=x+y;

        printf("Sum is..... %d", z);

} /* add */

OUTPUT

Enter two values: 10 20

Sum is..... 30

EXPLANATION: In the program, the arguments a and b are passed to the add() function. Passed arguments are executed in the calling function block and print the result in the same block. The add() function did not return any values to the main function, but the variables a and b are read from the main function.

Example 2: Program to send value to user‒define function and display the result.

/* Program to send value to user‒define function and display the result */

#include <stdio.h>

#include <conio.h>

void main()

{

/* Local definitions */

int date(int, int, int);

int dd,mm,yy;

/* Statements */

clrscr();

printf("Enter the Employee joining date .\n");

printf("Enter date,month,year : ");

scanf("%d %d %d", &dd, &mm, &yy);

date (dd,mm, yy); function call */

} /* main */

date(int x, int y, int z)

{

printf("Employee Joined date is : %d/%d/%d",x,y,z);

getch();

}

OUTPUT

Enter the Employee joining date.

Enter date,month, year: 11 12 1981

Employee Joined date is: 11/12/1981

EXPLANATION: In this program, three values are passed to date() function. The date() function receives the arguments from main() function and prints the date in a format and the date function did not return any values to the main function.

 

3. Function with arguments and with return value

In this prototype, the data is transferred between the calling function and called function. i.e., The called program receives some data from the calling program and send back a value return to the calling program. (Two way data communication).

Syntax:


Here, the continuous line indicates that, the data transfer takes place in between calling program and called program.

Example 1: Addition of two numbers.

* Program for addition of two numbers */

#include <stdio.h>

main()

{

int add(int, int); /* Local definition */

int a,b,c;

/* Statements */

printf("Enter two numbers :");

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

c=add(a,b); /* add is a function with arguments */

printf("Result is ...%d", c);

} /* main */

int add(int x, int y) /* add is a function with return value to main */

{

/* Local definition */

int z;

/* Statements */

z=x+y;

return(z);

} /* add */

OUTPUT

Enter two numbers: 10 20

Result is ...30

EXPLANATION: The main() function calls and sends arguments to the user defined function add(). The add() function performs calculation and the values are returned back to the main function through the return() function. The reading and writing the values are done in the main function.

Example 2: Program to send values to user‒define function and receive the values from user‒define function.

/* Program to send and receives the values from function */

#include <stdio.h>

#include <conio.h>

void main()

{

int mul(int, int), m, n,sum; /* Local definitions */

/* Statements */

clrscr();

printf("Enter the two numbers: ");

scanf("%d %d", &m,&n);

sum=mul(m,n); /* call function */

printf("Multiplication of entered values is: %d", sum);

getch();

} /* main */

mul(int m, int n)

{

return(m*n);

} /* mul */

OUTPUT

Enter the two numbers: 30 4

Multiplication of entered values is: 120

EXPLANATION: In this program the function mul() receives three values from main function. The mul() function calculates the multiplication of two values and return the result to the main program.

 

4. Function with no arguments and with return value

In this prototype, one way data communication takes place. i.e., the calling program can not pass any arguments to the called program but, the called program may send some return value to the calling program.

The called function is independent. It reads values from the keyboard or generates from initialization and returns the values. Here both the calling and called functions are partly communicated with each other.

Syntax:


Example 1: Addition of two numbers.

/* Function with no arguments and with return value */

#include <stdio.h>

main()

{

int add();   /* Local definition */

c=add(); /* add is a function with no arguments */

printf("Results in ..%d", C);

} /* main */

int add()/* add is a function with return value */

{

 /* Local definition */

int a,b,c;

/* Statements */

printf ("Enter two numbers: ");

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

c=a+b;

return (c);

} /* add */

OUTPUT

Enter two numbers: 10, 20

Result is …. 30

EXPLANATION: In the above program, the main function did not send any arguments to the user defined function add(). But the user defined function sending back some values to the main function. The variable a and b are read and executed in add() function but the result is returned back to the main function.

Example 2: Program to multiply three numbers using function.

/* Multiplication of three numbers */

#include <stdio.h>

#include <conio.h>

void main()

{

int *mul(), *s; /* Local definitions */

/* Statements */

clrscr();

s=mul(); /* function call */

printf("Multiplication of three numbers is = %d",*s);

getch();

} /* main */

*mul()

{

int a,b,c,i;

printf("\n Enter the three values: ");

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

i=a*b*c;

return(&i);

} /* mul */

OUTPUT

Enter the three values: 4 8 2

Multiplication of three numbers is = 64

EXPLANATION: The function* mul() is declared as a pointer function i.e., the function declared as pointer always returns the reference. The reference returned by the function *mul() is assigned to a pointer *s The pointer *s prints the multiplication value.

 

Computer Programming C: UNIT III: Functions and Pointers : Tag: Computer Science : C Programming - Function Prototypes


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