0% found this document useful (0 votes)
9 views38 pages

Understanding Functions in C Programming

Uploaded by

venna.v14
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)
9 views38 pages

Understanding Functions in C Programming

Uploaded by

venna.v14
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

Functions

• A function is a block of code that performs a


particular task.

• There are many situations where we might need


to write the same line of code more than once in a
program.

• This may lead to unnecessary repetition of code,


bugs and even becomes boring for the programmer.

• So, C language provides an approach in which


we can declare and define a group of statements
once in the form of a function and it can be
Need of Functions in C
Functions are used because of the following reasons –
• To improve the readability of code.
• Improves the reusability of the code, the same function
can be used in any program rather than writing
the same code from scratch.
• Debugging of the code would be easier if you use
functions, as errors are easy to be traced.
• Reduces the size of the code, duplicate sets of
statements are replaced by function calls.
Classification of Functions

• Standard Library functions also called built-in


functions are those functions which are
already defined in the C library, for
Functions example, printf(), scanf(), etc. You just need
to include appropriate header files to use
these functions. These are already declared
Standard and defined in C libraries.
User-defined
Library
Functions
Functions • User-defined functions, on the other hand,
are those functions that are defined by
the user at the time of writing the
program. These functions are
made for code reusability and for
saving time and space.
Structure of C Function

The basic structure ofa C Function


consists of 3 parts –
1. Function Declaration (Function Prototype)
2. Function Call
3. Function Definition
Structure of C Function
1. Function declaration (prototype)
Syntax:
Return_type function_name (parameters);

• A function may return a value.


• The Return_type is the data type (int, char, float,
double) of the value, the function returns.
• In case your function doesn't return any value,
the return type would be void.

void is the return type here, so the function returns


nothing.
Structure of C Function
1. Function declaration (prototype)
Syntax:
Return_type function_name (parameters);

• This is the actual name of the function.


• The function name is any valid C identifier and
therefore must follow the same naming
rules as other variables in the C language.

my_func is the function name here.


Structure of C Function
1. Function declaration (prototype)
Syntax:
Return_type function_name (parameters) ;

• A parameter is like a placeholder.


• When a function is called, you pass a value to
the parameter. This value is referred to
as an actual parameter.
• The parameter list refers to the type, order, and
number of the parameters of a
function. Parameters are optional, i.e., a
function may contain no parameters.

Here, my_func() holds two parameters.


Structure of C Function
1. Function declaration (prototype)
Syntax:
Return_type function_name (parameters) ;

The function declaration must always be terminated


by a semi-colon.
Structure of C Function
2. Function call
Syntax:
function_name (arguments);
• A function can be called from anywhere in the
program.
• The argument list must not differ in function
calling and function declaration.
• We must pass the same number of parameters
as it is specified in the function declaration.
• The function call must always be terminated
by a semi-colon.
Structure of C Function
2. Function call
Syntax:
function_name (arguments);

• Arguments are the values specified during the


function call, for which the formal
parameters are declared while defining the
function.
• The argument list declares the type and number
of arguments that the function expects
when it is called.
Structure of C Function
3. Function definition
Syntax:
Return_type function_name (parameters)
{
code to be executed (body of the function)
}

Header of the function


• It consists of
• Return_type
• Function_name
• Formal parameters
Structure of C Function
3. Function definition
Body of the function
• The function body contains the
declarations and the statements
(algorithm) necessary for
performing the required task.

• The body is enclosed within


curly braces { ... } and consists
of three parts.
• Local variable declaration (if
required).
• Function statements to
perform the task inside the
function.
Structure of C Function
3. Function definition
Body of the function

• The body is enclosed within curly


braces { ... } and consists
of three parts.
• Local variable declaration
(if required)
• Here, avg is the local variable of
type float required to calculate
the average of the given numbers
Function using Return_type
• In the declaration, the return_type specified in
this example is int. So, the function my_func
returns an integer when it is called in main().

• Since, the function my_func returns an integer


when it is called in main(), the value that is
returned by it must be stored in a variable.
Here, that variable is declared as result and is
defined as shown in the program.

• Since, the function my_func returns an integer,


a return statement to return the result
evaluated by the function has to be defined.
Function using Return_type
• While declaring the function, we have
declared two parameters of type int.

• Therefore, while calling that function, we need


to pass two arguments, else we will get a
compilation error.

• And the two arguments passed should be


received in the function definition, which
means that the function header in the function
definition should have the two parameters to
hold the argument values.
Types of User – defined Functions
So a function either can take some arguments, or nothing is taken. Similarly, a
function can return something, otherwise does not return anything. So we can
categorize them into four types.
Function with Function with
no arguments arguments
and a return and no return
value value

Function with Function with


no arguments arguments
and no return and a return
value
User- value
defined
functions
Types of User – defined Functions
Function with no arguments and no return value:

• In the program, we have a function


named my_func which has no argument and
no return type (void is the return type - that
means, the function will not return
anything).

• Within the function my_func, we are


declaring two variables a, b, calculating
sum of these elements, and printing the
sum.
Output:
Enter two integers: 3 6
3+6=9
Types of User – defined Functions
Function with no arguments and a return value:

• In the program, we have a function


named my_func which has no argument but
a return type (int is the return type - that
means, the function will return an integer).

• Within the function my_func, we are


declaring two variables a, b, calculating
sum of these elements, and returning the
sum to the main function where
my_func is called.
Output:
Enter two integers: 3 6
Sum = 9
Department of E E E
Types of User – defined Functions
Function with arguments and no return value:

• In the program, we have a function


named my_func which has two arguments of
type int but no return type (void is the return
type - that means, the function will not
return anything).

• Within the function my_func, we are


calculating the sum of the elements
received as arguments x, y, and print
the resultant sum (x+y).
Output:
Enter two integers: 3 6
3+6=9
Types of User – defined Functions
Function with arguments and a return value:

• In the program, we have a function


named my_func which has two
arguments of type int and a return type
(int is the return type - that means, the
function will return an integer).

• Within the function my_func, we are


calculating the sum of the elements
received as arguments x, y, and return
the resultant sum (x+y) to the main
function
my_func iswhere
called and the result is printed in
Output:
the main function. Enter two integers: 3 6
3+6=9
Types of Function Calls
• Functions are called by their names.
• If the function does not have any arguments, then to call a function we can
directly use its name.
• But for functions with arguments, we can call a function in two different
ways, based on how we specify the arguments.

Call by
Function Value
Calls Call by
Reference
Types of Function Calls
1. Call by Value:

• Inthis parameter passing method, of


values
parameters are copied to the function’s
actual
formal parameters.

• Different memory locations are allotted for both


formal and actual parameters.

• So any changes made in formal parameters (inside


functions) are not reflected in the actual parameters
of the calling function.
Types of Function Calls
1. Call by Value:
#include <stdio.h>

Void modify(int);
int main() {
int a = 5; Output:
printf("Before function call: a = %d\n", a); Before function call: a = 5
Inside function: x = 15
modify(a); // Call by value After function call: a = 5

printf("After function call: a = %d\n", a);


return 0;
}
// Function that uses call by value
void modify(int x) {
x = x + 10;
printf("Inside function: x = %d\n", x);
}
Types of Function Calls
Address of Operator (&)

The "Address Of" Operator (&) is a unary


operator, which returns the address of a variable.

x Variable (x)
X = 10
10 Value of (x)
(&X) = 3108
3108 Address of (x)

Pointer is a special kind variable which stores the


of address of a variable.
ptr Pointer for (x)
X = 10 3108 Value of pointer
ptr = (&X) = 3108 3112 Address of pointer
Types of Function Calls
Address of Operator (&)
x Variable (x) ptr Pointer for (x)
10 Value of (x) 3108 Value of pointer
3108 Address of (x) 3112 Address of pointer
Types of Function Calls
2. Call by Reference:

• In this method, the address of the variable is passed


to the function as a parameter.

• Same memory is used for both formal and


actual parameters since the only address is
used by both parameters.

• So any changes made in formal parameters


(inside functions) are reflected in the actual
parameters of the calling function.
Types of Function Calls
2. Call by Reference:
#include <stdio.h>

Void modify(int);
int main() {
int a = 5;
printf("Before function call: a = %d\n", a);
Output:
modify(&a); // Pass address of 'a' Before function call: a = 5
Inside function: x = 15
printf("After function call: a = %d\n", a); After function call: a = 15
return 0;
}
// Function that uses call by reference via pointer
void modify(int *x) {
*x = *x + 10; // Modify value at the address pointed by x
printf("Inside function: x = %d\n", *x);
}
Recursion
• Recursion is a special way of nesting functions, where a function calls itself
inside it.

• We must have certain conditions in the function to break out of


the recursion, otherwise recursion will occur infinite times.

function1( )
{
// function1 body here
function1();
// function1 body here
}
Sum of first n natural numbers using Recursion
#include<stdio.h>
int sum(int n);
void main()
{
int number, result;
printf("Enter an integer:”);
scanf("%d", &number);
result = sum(number);
printf(“Sum is %d “, result);
}
int sum(int n)
{
if(n != 0)
return n + sum(n-1);
else
return n;
}
Library Functions in C
⚫ Library functions are built-in functions that are
grouped together and placed in a common location called a
library.
⚫ Each function performs a specific operation. We can
use this library function to get the pre-defined output.
⚫ All C standard library functions are declared by using
many header files.
⚫ These library functions are created at the time of
designing the compilers.
⚫ We include the header files in our C program
by using #include<filename.h>.
⚫ Whenever the program is run and executed, the
related files are included in the C program.
Header File Functions
Header Files

stdio.h − It is a standard i/o header file in which Input/output


functions are declared
math.h − All functions related to mathematics are in
this header file.
ctype.h − This file contains functions that are useful for testing
and mapping characters.

string.h − All string related functions are in this header file.

time.h − This file contains time and clock related functions.


Built functions in stdio.h
<stdio.h>
printf()
This function is used to print the all char, int, float,
string etc., values onto the output screen.
scanf()
This function is used to read data from keyboard.
putchar()
It writes a character to screen.
getchar()
It reads character from keyboard.
puts()
It writes line to o/p screen.
gets()
It reads line from keyboard.
Built functions in math.h
Built functions in math.h
Program on Built functions in math.h

#include<stdio.h>
#include<math.h> Output:
void main() 3.162278
{ 54.598150
printf("%f\ 1.386294
n",sqrt(10.0))
; 2.000000
printf("%f\ 5.200000
n",exp(4.0)); 5.000000
printf("%f\ -5.000000
n",log(4.0)); 2.000000
printf("%f\
n",log10(100. 0.500000
0)); 0.000000
printf("%f\ 1.000000
n",fabs(-5.2)); 0.000000
printf("%f\
n",ceil(4.5));
printf("%f\
Built functions in ctype.h
[Link] Function Description Return Values

Returns 0 if the passed argument is a non


1 This function identifies the – alphanumeric character
isalnum() alphanumeric characters Returns non-zero value if the passed
argument is an alphanumeric character

Returns 0 if the passed argument is not


This function identifies the an alphabet
2 isalpha() alphabets from other Returns non-zero value if the passed
characters argument is an alphabet

Returns 0 if the passed argument is not


This function identifies a number
3 isdigit() numbers in character. Returns nonzero value if the passed
argument is a number
Built functions in ctype.h
[Link] Function Description Return Values
Returns 0 if the passed argument is not
This function identifies the a lowercase alphabet
4 islower() lowercase alphabets. Returns nonzero value if the passed
argument is a lowercase alphabet

Returns 0 if the passed argument is not


This function identifies the an uppercase alphabet
5 isupper() uppercase alphabets. Returns nonzero value if the passed
argument is an uppercase alphabet

6 This function converts uppercase Returns lowercase alphabet of the


tolower() alphabet to lowercase alphabet. corresponding uppercase alphabet

This function convert lowercase Returns Uppercase alphabet of the


7 toupper() alphabet to uppercase alphabet. corresponding lowercase alphabet

You might also like