Functions
A function is a named block of code that performs a specific task.
● It allows you to write a piece of logic once and reuse it wherever
needed in the program.
● This helps keep your code clean, organized, easier to understand
and manage.
#include <stdio.h>
// Void function definition
void hello()
{
printf("Welcomen");
}
// Return-type function definition
int square(int x)
{
return x * x;
}
int main()
{
// Calling the void function
hello();
// Calling the return-type function
int result = square(5);
printf("Square of 5 is: %d", result);
return 0;
}
there are three functions:
● main() function: This is the starting point of every C program. When
the program runs, execution begins from the main function.
● hello() function: This is a user-defined function that does not take
any input and does not return a value. Its purpose is to print
"GeeksforGeeks" to the screen. It is called inside the main function
using hello();.
● square() function: This is another user-defined function, but unlike
hello(), it has a return type. It takes one integer as input and returns
the square of that number. In main(), we call square(5) and store the
returned result in a variable to print it.
Function Declaration vs Definition
It's important to understand the difference between declaring a function and
defining it. Both play different roles in how the compiler understands and
uses your function.
Function Declaration
A declaration tells the compiler about the function's name, return type, and
parameters before it is actually used. It does not contain the function's body.
This is often placed at the top of the program or in a header file.
// function declaration
int add(int a, int b);
Calling a Function
Once a function is defined, you can use it by simply calling its name followed
by parentheses. This tells the program to execute the code inside that
function.
#include <stdio.h>
// Function definition
int add(int a, int b) {
return a + b;
}
int main() {
// Function call
int result = add(5, 3);
printf("The sum is: %d", result);
return 0;
}
Output
The sum is: 8
Types of Function in C
In C programming, functions can be grouped into two main categories:
library functions and user-defined functions. Based on how they handle
input and output, user-defined functions can be further classified into
different types.
1. Library Functions: These are built-in functions provided by C, such as
printf(), scanf(), sqrt(), and many others. You can use them by including
the appropriate header file, like #include <stdio.h> or #include <math.h>.
2. User-Defined Functions: These are functions that you create yourself to
perform specific tasks in your program. Depending on whether they take
input or return a value, they can be of four types:
● No arguments, no return value: The function neither takes input
nor returns any result.
● Arguments, no return value: The function takes input but does
not return anything.
● No arguments, return value: The function does not take input but
returns a result.
● Arguments and return value: The function takes input and returns
a result.
User-Defined Function
A user-defined function is a type of function in C language that is defined
by the user himself to perform some specific task. It provides code
reusability and modularity to our program. User-defined functions are
different from built-in functions as their working is specified by the user
and no header file is required for their usage.
In this article, we will learn about user-defined function, function
prototype, function definition, function call, and different ways in which
we can pass parameters to a function.
How to use User-Defined Functions in C?
To use a user-defined function, we first have to understand the different
parts of its syntax. The user-defined function in C can be divided into
three parts:
1. Function Prototype
2. Function Definition
3. Function Call
C Function Prototype
A function prototype is also known as a function declaration which
specifies the function's name, function parameters, and return type. The
function prototype does not contain the body of the function. It is
basically used to inform the compiler about the existence of the user-
defined function which can be used in the later part of the program.
Syntax
return_type function_name (type1 arg1, type2 arg2, ... typeN
argN);
Or
return_type function_name (type1 , type2 , ... typeN);
C Function Definition
Once the function has been called, the function definition contains the
actual statements that will be executed. All the statements of the function
definition are enclosed within { } braces.
Syntax
return_type function_name (type1 arg1, type2 arg2 .... typeN
argN) {
// actual statements to be executed
// return value if any
}
C Function Call
In order to transfer control to a user-defined function, we need to call it.
Functions are called using their names followed by round brackets. Their
arguments are passed inside the brackets.
Syntax
function_name(arg1, arg2, ... argN);
// C Program to illustrate the use of user-defined function
#include <stdio.h>
// Function prototype
int sum(int, int);
// Function definition
int sum(int x, int y)
{
int sum;
sum = x + y;
return x + y;
}
// Driver code
int main()
{
int x = 10, y = 11;
// Function call
int result = sum(x, y);
printf("Sum of %d and %d = %d ", x, y, result);
return 0;
}
Passing Parameters to User-Defined Functions
We can pass parameters to a function in C using two methods:
1. Call by Value
2. Call by Reference
1. Call by value
In call by value, a copy of the value is passed to the function and changes
that are made to the function are not reflected back to the values. Actual
and formal arguments are created in different memory locations.
// C program to show use of
// call by value
#include <stdio.h>
void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
// Driver code
int main()
{
int x = 10, y = 20;
printf("Values of x and y before swap are: %d, %dn", x,
y);
swap(x, y);
printf("Values of x and y after swap are: %d, %d", x,
y);
return 0;
}
Output
Values of x and y before swap are: 10, 20
Values of x and y after swap are: 10, 20
2. Call by Reference
In a call by Reference, the address of the argument is passed to the
function, and changes that are made to the function are reflected back to
the values. We use the pointers of the required type to receive the
address in the function.
// C program to implement
// Call by Reference
#include <stdio.h>
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
// Driver code
int main()
{
int x = 10, y = 20;
printf("Values of x and y before swap are: %d, %dn", x,
y);
swap(&x, &y);
printf("Values of x and y after swap are: %d, %d", x,
y);
return 0;
}
// C program to implement
// Call by Reference
#include <stdio.h>
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
// Driver code
int main()
{
int x = 10, y = 20;
printf("Values of x and y before swap are: %d, %dn", x,
y);
swap(&x, &y);
printf("Values of x and y after swap are: %d, %d", x,
y);
return 0;
}
Output
Values of x and y before swap are: 10, 20
Values of x and y after swap are: 20, 10

Functions and User-Defined Functions in C Programming Language

  • 1.
    Functions A function isa named block of code that performs a specific task. ● It allows you to write a piece of logic once and reuse it wherever needed in the program. ● This helps keep your code clean, organized, easier to understand and manage. #include <stdio.h> // Void function definition void hello() { printf("Welcomen"); } // Return-type function definition int square(int x) { return x * x; } int main() { // Calling the void function hello(); // Calling the return-type function int result = square(5); printf("Square of 5 is: %d", result); return 0; } there are three functions:
  • 2.
    ● main() function:This is the starting point of every C program. When the program runs, execution begins from the main function. ● hello() function: This is a user-defined function that does not take any input and does not return a value. Its purpose is to print "GeeksforGeeks" to the screen. It is called inside the main function using hello();. ● square() function: This is another user-defined function, but unlike hello(), it has a return type. It takes one integer as input and returns the square of that number. In main(), we call square(5) and store the returned result in a variable to print it. Function Declaration vs Definition It's important to understand the difference between declaring a function and defining it. Both play different roles in how the compiler understands and uses your function. Function Declaration A declaration tells the compiler about the function's name, return type, and parameters before it is actually used. It does not contain the function's body. This is often placed at the top of the program or in a header file. // function declaration int add(int a, int b); Calling a Function
  • 3.
    Once a functionis defined, you can use it by simply calling its name followed by parentheses. This tells the program to execute the code inside that function. #include <stdio.h> // Function definition int add(int a, int b) { return a + b; } int main() { // Function call int result = add(5, 3); printf("The sum is: %d", result); return 0; } Output The sum is: 8 Types of Function in C In C programming, functions can be grouped into two main categories: library functions and user-defined functions. Based on how they handle input and output, user-defined functions can be further classified into different types. 1. Library Functions: These are built-in functions provided by C, such as printf(), scanf(), sqrt(), and many others. You can use them by including the appropriate header file, like #include <stdio.h> or #include <math.h>. 2. User-Defined Functions: These are functions that you create yourself to perform specific tasks in your program. Depending on whether they take input or return a value, they can be of four types: ● No arguments, no return value: The function neither takes input nor returns any result.
  • 4.
    ● Arguments, noreturn value: The function takes input but does not return anything. ● No arguments, return value: The function does not take input but returns a result. ● Arguments and return value: The function takes input and returns a result. User-Defined Function A user-defined function is a type of function in C language that is defined by the user himself to perform some specific task. It provides code reusability and modularity to our program. User-defined functions are different from built-in functions as their working is specified by the user and no header file is required for their usage. In this article, we will learn about user-defined function, function prototype, function definition, function call, and different ways in which we can pass parameters to a function. How to use User-Defined Functions in C? To use a user-defined function, we first have to understand the different parts of its syntax. The user-defined function in C can be divided into three parts: 1. Function Prototype 2. Function Definition 3. Function Call C Function Prototype A function prototype is also known as a function declaration which specifies the function's name, function parameters, and return type. The
  • 5.
    function prototype doesnot contain the body of the function. It is basically used to inform the compiler about the existence of the user- defined function which can be used in the later part of the program. Syntax return_type function_name (type1 arg1, type2 arg2, ... typeN argN); Or return_type function_name (type1 , type2 , ... typeN); C Function Definition Once the function has been called, the function definition contains the actual statements that will be executed. All the statements of the function definition are enclosed within { } braces. Syntax return_type function_name (type1 arg1, type2 arg2 .... typeN argN) { // actual statements to be executed // return value if any } C Function Call In order to transfer control to a user-defined function, we need to call it. Functions are called using their names followed by round brackets. Their arguments are passed inside the brackets. Syntax function_name(arg1, arg2, ... argN); // C Program to illustrate the use of user-defined function #include <stdio.h>
  • 6.
    // Function prototype intsum(int, int); // Function definition int sum(int x, int y) { int sum; sum = x + y; return x + y; } // Driver code int main() { int x = 10, y = 11; // Function call int result = sum(x, y); printf("Sum of %d and %d = %d ", x, y, result); return 0; } Passing Parameters to User-Defined Functions We can pass parameters to a function in C using two methods: 1. Call by Value 2. Call by Reference 1. Call by value
  • 7.
    In call byvalue, a copy of the value is passed to the function and changes that are made to the function are not reflected back to the values. Actual and formal arguments are created in different memory locations. // C program to show use of // call by value #include <stdio.h> void swap(int a, int b) { int temp = a; a = b; b = temp; } // Driver code int main() { int x = 10, y = 20; printf("Values of x and y before swap are: %d, %dn", x, y); swap(x, y); printf("Values of x and y after swap are: %d, %d", x, y); return 0; } Output Values of x and y before swap are: 10, 20 Values of x and y after swap are: 10, 20 2. Call by Reference
  • 8.
    In a callby Reference, the address of the argument is passed to the function, and changes that are made to the function are reflected back to the values. We use the pointers of the required type to receive the address in the function. // C program to implement // Call by Reference #include <stdio.h> void swap(int* a, int* b) { int temp = *a; *a = *b; *b = temp; } // Driver code int main() { int x = 10, y = 20; printf("Values of x and y before swap are: %d, %dn", x, y); swap(&x, &y); printf("Values of x and y after swap are: %d, %d", x, y); return 0; } // C program to implement // Call by Reference #include <stdio.h>
  • 9.
    void swap(int* a,int* b) { int temp = *a; *a = *b; *b = temp; } // Driver code int main() { int x = 10, y = 20; printf("Values of x and y before swap are: %d, %dn", x, y); swap(&x, &y); printf("Values of x and y after swap are: %d, %d", x, y); return 0; } Output Values of x and y before swap are: 10, 20 Values of x and y after swap are: 20, 10