×

User Defined Functions in C

C Functions:

A function is defined as a block of code that is used to perform some specific task.

In functions, we use flower brackets ({}) which contain the set of programming statements. The main purpose of using functions is we can reuse and modularise our programs. In the C program, we can call a function multiple times. In other words, we can say that the collection of functions creates a program.

Types of Functions:

In C, programming functions are of two types

  1. Library Functions:
    Library functions are built-in functions directly declared in C header files and placed in a common location called a library. such as scanf(), printf(), gets() etc. All functions have some specific task to be performed.
    scanf () – used to get the input from the user
    printf () – used to print the output to the screen
  2. User-defined Functions:
    User-defined functions are the functions that are created by the user, and the user can reuse them multiple times. A user-defined function mainly has three components
    • Function declaration
    • Function call
    • Function definition

Function Declaration:

Always a function is declared in the global section or globally in a C program. It specifies the compiler about the function name, function parameter, etc.

Syntax:

Return type function name( type1 argument1, type2 argument2, …);

Function Call:

We can call our function throughout the program anywhere. We must always remember that the parameters must be the same as the function declaration. If there are two parameters, then in the function call also, there must be two parameters.

Syntax:

functionname(argument1, argument2, …);      

Different Aspects of Function Calls:

There are four different function protocol styles present in C programming.

  • With parameters and with return values (WPWR)

Example:

//program for finding factorial using Functions with parameter and return value
#include < stdio.h >
#include < conio.h >
int factorial( int x );
void main(void)
{
int n, f;
clrscr();
printf( “Enter n value:” );
scanf( “%d”, &n );
f = factorial(n);
printf(“The factorial of %d is %d”, n, f);
}
int factorial ( int x )
{
int fact = 1, i;
if( x== 0 )
{
return fact;
}
else
{
for( i = x; i>=1; i-- )
{
Fact = fact *I;
}
return fact;
}
}

Output:

Enter n value: 4
The factorial of 4 is 24

Explanation:

Firstly, we have included stdio for standard input and output, conio to clear the output screen. We have taken the protocol style wpwr ( with parameter and with return value ). After that, we declared a function with return type int. In the main method, we have declared two variables, n (to store the given input) and f (to store the result after the function call). We have called the function, and the output will be stored in the variable f. After the function call, the program will redirect to the function body, and the operation will be done there. Once the operations are done, it will return the result to the main method from where it has been called. The 4 factorial is (4*3*2*1 = 24). So the output will be printed as 24.

  • With parameter and without return value (WPWOR)

Example:

//program for finding factorial using Functions with parameter and without return value
#include< stdio.h >
#include< conio.h >
Void factorial(int x);
void main(void)
{
int n;
clrscr();
printf( “Enter n value:” );
scanf( “%d”, &n );
factorial(n); 
}
void factorial ( int x )
{
int fact = 1, i;
if( x== 0 )
{
printf(“Factorial of %d is %d:”, x, fact);
}
else
{
for( i = x; i>=1; i-- )
{
Fact = fact *I;
}
printf(“The factorial of %d is %d”, x, fact);
}
}

Output:

Enter n value: 5
The factorial of 5 is 120

Explanation:

Here we have used the protocol style WPWOR(with parameter and without return value). If we are using without return values, we must give the return type as void( it will not return anything). So we directly print the output in the function body. And the factorial of 5 is (5*4*3*2*1 = 120). So the output will be printed as “The factorial of 5 is 120”.

  • Without parameter and With return values (WOPWR)

Example:

//program for finding factorial using Functions without parameter and with return value
#include < stdio.h >
#include < conio.h >
int factorial( void );
void main(void)
{
int f;
clrscr();
f = factorial();
printf(“The factorial is %d”, f);
}
int factorial( void )
{
int x, fact = 1, i;
printf(“enter the value of x:”);
scanf(“%d”, &x);
if( x == 0 )
{
return fact;
}
else
{
for(i = x;i>=1;i--)
{
fact = fact*I;
}
return fact;
}
}

Output:

Enter the value of x: 8
The factorial is 40320

Explanation:

Here we have used the protocol style WOPWR (without parameter and with return value). If we are using without parameters, we do not pass any parameters. So, we directly declare the variables in the function body. And return the result to the function call. The factorial of 8 is (8*7*6*5*4*3*2*1 = 40320). So, the output will be printed as “The factorial is 40320”.

  • Without parameter and without return value (WOPWOR)

Example:

//program for finding factorial using Functions without parameters and without return value
#include < stdio.h >
#include < conio.h >
Void factorial( void );
Void main( void )
{
clrscr();
factorial();
}
Void factorial( void )
{
int x, fact = 1, i;
printf(“enter the value of x:”);
scanf(“%d”, &x);
if(x == 0)
{
Printf(“The factorial of %d is %d”, x, fact);
}
else
{
for( i=x;i>=1;i--)
{
fact = fact *i;
}
Printf(“The factorial of %d is %d”, x, fact);
}
}

Output:

Enter the value of x: 6
The factorial of 6 is 720

Explanation:

Here we have used the protocol style WOPWOR (without parameter and without return value). If we are using without parameters and return values, we must give the return type as void (it will not return anything), and we will not give any parameters. So, we directly declare the variable and print the output in the function body. And the factorial of 6 is (6*5*4*3*2*1 = 720). So, the output will be printed as “The factorial of 6 is 720”.

Function Definition:

This block is more important in functions because all the executable statements are written here. When we call the function, it will be redirected to function definition (function body ). After all the operations, it will return only one value.

Syntax:

return type function name(argument 1, argument 2, argument 3, ….){function body;}

Advantages of Functions:

  • By using functions, we can divide a complex problem into small blocks.
  • We can call the function anywhere in the program and any number of times.
  • Using functions, we can avoid writing of same code again and again in the program.
  • The Main purpose of using functions is we can reuse the code/reusability of the code in the programs.

Related Topics

What is Linked List in C

Linked list Similar to an array, a linked list is a linear data structure that stores a chain of nodes with two fields of memory in each node. Where first memory...

7 minutes read.

Pre-increment and Post-increment in C

The rich set of operators is supported by the c language. In c there are several operators used. The mathematical operations in the programming language are done with the operators...

4 minutes read.

How to create a binary file in C?

Creating a binary file in C language is very simple, requiring only some specific functions to be implemented. There are also other binary modes: read, and write, which will be...

7 minutes read.

Call by Value and Call by Reference in C

Call by reference and call by value are two different ways of passing arguments to a function in C programming language. Call by reference means that the called function is...

4 minutes read.

GCD program in C

C language : Dennis Ritchie developed the general-purpose computer language C at Bell Laboratories in 1972. Despite being an ancient language, it is extremely popular. It is among the most widely used...

4 minutes read.

Multithreading in C interview questions

Q1. What exactly is a thread? Ans: A thread is a brief sequence of instructions intended to be planned and carried out by the CPU apart from the parent process. Q2. Can...

4 minutes read.

Recursion in C

Recursion in C: In the C programming language, the concept known as recursion exists that is a technique in which a function calls itself either directly or indirectly. It allows...

4 minutes read.

Difference between Pre-increment and Post-increment in C

Increment operators are the kind of operators that are used to increment the cost of the operand by way of 1. In different words, the increment operator is an operator...

4 minutes read.

10 Best IDEs for C or C++ Developers in 2024

Nobody can deny the fact that C and C++ were the first programming languages used by significant developers worldwide. Even now, newcomers who want to start programming are most frequently...

6 minutes read.

GCD program of two numbers in C

GCD stands for Greatest Common Divisor, which is also known as Highest Common Factor, which can be abbreviated as HCF. Mathematically it can be defined as any two positive integers...

3 minutes read.

How to convert a number to words in C

There are many ways available for converting an integer or a number to its word form. For example, consider a number as 12345. This number can be represented in two...

3 minutes read.

C Program for Mean and Median of an Unsorted Array

In this tutorial, we will look at how to determine the mean and median of a given unsorted array. To determine the Mean: To get the average, mean is determined. The formula...

2 minutes read.

C Program Swap Numbers in cyclic order Using Call by Reference

The three integers that the user entered in cyclical sequence are swapped in this tutorial using call by reference. C Program: #include <stdio.h> void cyclic_Swap ( int *X, int *Y, int *Z ); int...

2 minutes read.

C program that does not Suspend when Ctrl+Z is Pressed

In Programming when a program breakdown and runs surprisingly in the terminal compiler the developer has the ability to prevent the program from running unequivocally. For expressly halting the program,...

3 minutes read.

How to Calculate Time Complexity in C?

What is time complexity? An algorithm's time complexity measures how long it takes to complete a task in relation to the size of the input. It should be noted that the...

5 minutes read.

Prime Number code in C

Prime Number Programs in C language In this tutorial, we will learn how to check whether the given number is a prime number or not, and how to print all the...

4 minutes read.

Long int in C

We use data type to avoid the type of data to be stored for a variable at the time of declaration.So, we can say that the variable type is known...

3 minutes read.

Purpose of a Function Prototype in C

A function prototype in C is a declaration of a function that specifies the function's name, return type and parameters. It has the following syntax: return_type function_name(parameter_list); For example, the prototype for...

4 minutes read.

C program to Store Information of Students Using Structure

What is the Structure in C? User-defined data types include structures. Structures aid your ability to combine things of various categories into a single group. Like arrays, it operates similarly. A...

3 minutes read.

memcpy() in C

Introduction: Here we discuss about the memcpy() function in C. The memcpy() function is also called the Copy Memory Block function. Used to create a copy of a specific drawing...

4 minutes read.