0% found this document useful (0 votes)
52 views12 pages

C Programming Functions Explained

Module 4 of the Fundamentals of C Programming focuses on functions, including their definition, types (library and user-defined), and advantages. It covers the structure of user-defined functions, function declaration, function calls, and the differences between call by value and call by reference. Additionally, it introduces recursive functions and provides examples of C programs demonstrating these concepts.

Uploaded by

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

C Programming Functions Explained

Module 4 of the Fundamentals of C Programming focuses on functions, including their definition, types (library and user-defined), and advantages. It covers the structure of user-defined functions, function declaration, function calls, and the differences between call by value and call by reference. Additionally, it introduces recursive functions and provides examples of C programs demonstrating these concepts.

Uploaded by

Richu Paul K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Fundamentals of C Programming Module 4

CO4 Make use of functions to solve problems effectively


M4.01 Outline functions, types and advantages. 2 Understanding

M4.02 Illustrate structure of a user defined functions 3 Understanding


Make use of function declaration, function
M4.03 3 Applying
call, arguments, return type
M4.04 Construct different types of functions 2 Applying
Build programs with call by value and call by
M4.05 3 Applying
reference.
M4.06 Make use of recursive functions 2 Applying
Series Test - II 1

Contents:
Functions:Definition of Functions - Structure of user defined
functions- function prototype, function call, arguments, return type - Passing
arguments to a Function: call by value, call by reference, Recursive functions.

Page 1
Fundamentals of C Programming Module 4

Functions
A function in C is a set of statements that when called perform some specific task.

Types of Functions
There are two types of functions in C:
1. Library Functions
2. User Defined Functions

1. Library Function

Library function is also referred to as a “built-in function”.

For Example:
pow(), sqrt(), strcmp(), strcpy() etc.

2. User Defined Function

 Functions that the programmer creates are known as User-Defined


functions.
 User-defined functions can be improved and modified according to the
need of the programmer.

Page 2
Fundamentals of C Programming Module 4

Syntax of User-defined Functions in C


The syntax of function can be divided into 3 aspects:
1. Function Declaration
2. Function Definition
3. Function Calls

1. Function Declaration
A Function declaration(also known as function prototype) consists of four parts
 Function type(return type)
 Function name
 Parameter list
 Terminating semicolon(;)

Syntax

return_type name_of_the_function (parameter_1, parameter_2);

Example

int sum(int a, int b);


int sum(int , int);

Page 3
Fundamentals of C Programming Module 4

A prototype declaration may be placed in two places in a program


 Above all the functions(including main)
 in the global declaration section
 Inside a function definition
 In the local declaration section
[Link] Definition
Function definition has two parts
 Function Header
 Function Body

return_type function_name (para1_type para1_name , para2_type para2_name)


{
// body of the function
}

Function Header
The function header consists of three parts
 Function type(also known as return type): Specifies the type of value(like
float or double) that the function is expected to return to the program
calling the function.
 Function name: Any valid C identifier. The name should be appropriate to
the task performed by the function.
 Formal parameter list: Parameter list declares the variables that will
receive the data sent by the calling program.

Page 4
Fundamentals of C Programming Module 4

Semicolon is not used at the end of the function header.


Function Body
 The function body contains the declarations and statements necessary
for performing the required task.
 The body enclosed in braces contains three parts, in the order given as
follows:
1. Local declarations that specify the variables needed by the
function.
2. Function statements that perform the task of the function.
3. A return statement that returns the value evaluated by the
function.
o The return statement can take one of the following
form:
return;
OR
return expression;
Example:
float mul (float x, float y)
{
float result; /* local variable*/
result= x*y; /*computes the product*/
return(result); /* returns the result*/
}
3. Function Call
 A function call is a statement that instructs the compiler to
execute the function.
 We use the function name and parameters in the function call.
In the below example, the first sum function is called and 10,30 are passed to
the sum function. After the function call sum of a and b is returned and control
is also returned back to the main function of the program.

Page 5
Fundamentals of C Programming Module 4

Working of the C function can be broken into the following steps as mentioned
below:
1. Declaring a function
2. Defining a function
3. Calling the function
4. Executing the function
5. Returning a value

Write a C program to perform addition of two numbers using functions


#include<stdio.h>
int add(int,int);
int main()
{
int num1,num2,res;
printf(“Enter the two numbers:”);

Page 6
Fundamentals of C Programming Module 4

scanf(“%d %d”,&num1,&num2);
res=add(num1,num2);
printf(“Sum of the two numbers is: %d”,res);
return 0;
}
int add(int a, int b)
{
int sum;
sum=a+b;
return sum;
}

Passing arguments to a Function


 Functions can be invoked in two ways: Call by Value or Call by Reference.
 These two ways are generally differentiated by the type of values passed
to them as parameters.
Formal Parameter: A variable and its type as they appear in the prototype of the
function .
Actual Parameter: The variable or expression corresponding to a formal
parameter that appears in the function call.

Call By Value
In call by value method of parameter passing, the values of actual parameters
are copied to the function’s formal parameters.
 There are two copies of parameters stored in different memory locations.
 One is the original copy and the other is the function copy.
 The called function works on the copy and not on the original values of the
actual parameters.

Page 7
Fundamentals of C Programming Module 4

C program to illustrate call by value

#include <stdio.h>
void swap(int x, int y); // Function Prototype
int main() // Main function
{
int a = 10, b = 20;

// Pass by Values
swap(a, b); // Actual Parameters
printf("In the Caller:\na = %d b = %d\n", a, b);
return 0;
}

// Swap functions that swaps


// two values
void swap(int x, int y) // Formal Parameters
{
int t;
t = x;
x = y;
y = t;
printf("Inside Function:\nx = %d y = %d\n", x, y);
}

Output
Inside Function:
x = 20 y = 10
In the Caller:
a = 10 b = 20
Call by Reference
 In call by reference method of parameter passing, the address of the
actual parameters is passed to the function as the formal parameters.
 Any changes made inside the function are actually reflected in the actual
parameters of the caller.

Page 8
Fundamentals of C Programming Module 4

C program to illustrate call by reference

#include <stdio.h>
void swap(int *x, int* y); // Function Prototype
int main() // Main function
{
int a = 10, b = 20;
swap(&a,& b); // Actual Parameters
printf("In the Caller:\na = %d b = %d\n", a, b);
return 0;
}

// Swap functions that swaps


// by references
void swap(int* x, int *y) // Formal Parameters
{
int t;
t = *x;
*x =* y;
*y = t;
printf("Inside Function:\nx = %d y = %d\n", *x, *y);
}

Output
Inside Function:
x = 20 y = 10
In the Caller:
a = 20 b = 10

Recursive Functions
A function that calls itself is known as a recursive function. And, this technique is
known as recursion.

Page 9
Fundamentals of C Programming Module 4

Syntax
void recursive_fun() //recursive function
{
Base_case; // Stopping Condition

recursive_fun(); //recursive call


}

int main()
{

recursive_fun(); //function call

}
Base_case is the stopping condition for the recursive function.

How does Recursion in C work?

 The recursive function or method has two main parts in its body, i.e., the
base case and the recursive case.
 While the recursive method is executed, first, the base case is checked by
the program. If it turns out true, the function returns and quits; otherwise,
the recursive case is executed.
 Inside the recursive case, we have a recursive call that calls the function
inside which it is present.

Page 10
Fundamentals of C Programming Module 4

recursive_function()
{
if base_case = true; //base case

return;

else
//recursive case
return code_for_recursion; //includes recursive call
}

Write a C program to find the factorial of a number using recursive function

#include<stdio.h>

long int factorial(int);

void main()

long int fact;

int num;

printf(“Enter the number:”);

scanf(“%d”,&num);

fact= factorial(num);

printf(“The factorial of %d is %ld”,num,fact);

long int factorial(int n)

long int fact;


Page 11
Fundamentals of C Programming Module 4

if(n==1)

return (1);

else

fact=n*factorial(n-1);

return (fact);

Page 12

You might also like