0% found this document useful (0 votes)
7 views22 pages

Types of Functions

Uploaded by

vtilokani1981
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views22 pages

Types of Functions

Uploaded by

vtilokani1981
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Types of Functions in C

Functions in C programming are classified into two types:

1. Library Functions:

1. Also referred to as predefined functions, library functions are


already defined in the C libraries.
2. This means that we do not have to write a definition or the
function’s body to call them.
3. We can simply call them without defining them as they are
already defined.
4. However, we need to include the library at the beginning of
the code for calling a library function.
5. We can then use the proper syntax of the function to call
them.
6. Printf(), scanf(), ceil(), and floor() are examples of library
functions.
2. User-Defined Functions

1. These are the functions that a developer or the user declares,


defines, and calls in a program.
2. This increases the scope and functionality, and reusability of C
programming as we can define and use any function we want.
3. A major plus point of C programming is that we can add a user-
defined to any library to use it in other programs.
C Library Function

1. stdio.h: This library is use to use the printf() function, the header
file <stdio.h> should be included in the program.

// C program to implement
// the above approach
#include <stdio.h>

// Driver code
int main()
{
printf("GEEKS FOR GEEKS");
return 0;
}

Output: GEEKS FOR GEEKS


2. math.h– To perform any operation related to mathematics, it is
necessary to include math.h header file.
Example 1: sqrt()

Syntax-
double sqrt(double x)
// C program to implement
// the above approach Output:
#include <math.h> Square root of 12.50 = 3.54
#include <stdio.h>

// Driver code
int main()
{
double number, squareRoot;
number = 12.5;

// Computing the square root


squareRoot = sqrt(number);
printf("Square root of %.2lf = %.2lf",
number, squareRoot);
return 0;
}
3. string.h: For using string functions, it is necessary to include
string.h header file in the program.

Example 1: strcat(): In C programming, the strcat() functions are


used to concatenate(join) two strings. This function
concatenates the destination string and the source string, and
the result is stored in the destination string.

Syntax-
char *strcat(char *destination, const char *source)
// C program to implement
// the above approach
#include <stdio.h> Output:
#include <string.h> Geeks For Geeks

// Driver code
int main()
{
char str1[100] = "Geeks ",
str2[100] = " For Geeks";

// Concatenates str1 and str2


strcat(str1, str2);

// Resultant string is stored


// in str1
puts(str1);

return 0;
}
User-Defined Function in C

1. A user-defined function is a type of function in C language that is


defined by the user himself to perform some specific task.
2. It provides code reusability and modularity to our program.
3. 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.
4. 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
1. C Function Prototype
2. A function prototype is also known as a function declaration which
specifies the function’s name, function parameters, and return
type.
3. The function prototype does not contain the body of the function.
4. 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);


2. C Function Definition

1. Once the function has been called, the function definition contains
the actual statements that will be executed.
2. 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 }

Note: If the function call is present after the function definition, we


can skip the function prototype part and directly define the function.
3. C Function Call
1. In order to transfer control to a user-defined function, we need to
call it.
2. Functions are called using their names followed by round brackets.
3. Their arguments are passed inside the brackets.

Syntax
function_name(arg1, arg2, ... argN);
/ C Program to illustrate the use of user-
defined function // Function call
int result = sum(x, y);
#include <stdio.h> printf("Sum of %d and %d = %d
", x, y, result);
// Function prototype
int sum(int, int); return 0;
}
// Function definition
int sum(int x, int y)
{
int sum; Output
sum = x + y;
return x + y; Sum of 10 and 11 = 21
}

// Driver code
int main()
{
int x = 10, y = 11;
• Components of Function Definition

There are three components of the function definition:

1. Function Parameters
2. Function Body
3. Return Value
1. Function Parameters

2. Function parameters (also known as arguments) are the values that are
passed to the called function by the caller.
3. We can pass none or any number of function parameters to the
function.
4. We have to define the function name and its type in the function
definition and we can only pass the same number and type of
parameters in the function call.

Example

int foo (int a, int b);Here, a and b are function parameters.

Note: C language provides a method using which we can pass variable


number of arguments to the function. Such functions are called variadic
function.
2. Function Body

1. The function body is the set of statements that are enclosed within
{ } braces.
2. They are the statements that are executed when the function is
called.

Example

int foo (int a, int b) { int sum = a + b; return sum; }Here, the statements
between { and } is function body.
3. Return Value

1. The return value is the value returned by the function to its caller.
2. A function can only return a single value and it is optional.
3. If no value is to be returned, the return type is defined as void.
4. The return keyword is used to return the value from a function.

Syntax

return (expression);Example
int foo (int a, int b) { return a + b; }

Note: We can use pointers or structures to


return multiple values from a function in C.
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.

Note: Values aren’t changed in the call by value since they aren’t passed by
reference.
// C program to show use of
// call by value Output:
#include <stdio.h>
Values of x and y before swap are: 10, 20
void swap(int a, int b) Values of x and y after swap are: 10, 20
{
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, %d\n", x,
y);
swap(x, y);
printf("Values of x and y after swap are: %d, %d", x,
y);
return 0;
}
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 Output
#include <stdio.h> Values of x and y before swap are: 10, 20
Values of x and y after swap are: 20, 10
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, %d\n", x,
y);
swap(&x, &y);
printf("Values of x and y after swap are: %d, %d", x,
y);
return 0;
}

You might also like