0% found this document useful (0 votes)
3 views30 pages

Module 4 Notes

This document provides an introduction to user-defined functions in C programming, explaining their importance, structure, and advantages such as modular programming and code reusability. It covers the elements of function definition, function calls, and the distinction between library functions and user-defined functions. Additionally, it outlines the rules for parameter matching and the flow of control during function execution.

Uploaded by

Santosh Patil
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)
3 views30 pages

Module 4 Notes

This document provides an introduction to user-defined functions in C programming, explaining their importance, structure, and advantages such as modular programming and code reusability. It covers the elements of function definition, function calls, and the distinction between library functions and user-defined functions. Additionally, it outlines the rules for parameter matching and the flow of control during function execution.

Uploaded by

Santosh Patil
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

@azdocuments

INTRODUCTION TO
C PROGRAMMING
SUBJECT CODE: 1BPLC205E/105E
MODULE-4

Name:

USN:

College:

AZ Documents
Your Engineering Study Partner
[Link]
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

INTRODUCTION TO C PROGRAMMING
1BPLC205E/105E
MODULE-04
User-defined Functions: Introduction, Need for User-defined Functions, A Multi-
functional Program, Elements of User-defined Functions, Definition of Function, Return
Values and their Types, Function Calls, Function Declaration, No Arguments and no
Return Values, Arguments but no Return Values, Nesting of Functions.
Textbook: Chapter 10.1 to 10.8, 10.10 to 10.14

FUNCTIONS IN C

One of the most powerful features of the C programming language is the use of functions.
Functions make programs:

• easier to write

• easier to understand

• easier to debug

• easier to maintain

In earlier programs, we have already used some functions such as:

• main()
• printf()

• scanf()

However, C provides the ability to create our own functions.

These functions are called user-defined functions.

What is a Function?

A function is a block of code that performs a specific task.

It is a self-contained program segment that can be executed whenever it is called.


Once a function is written, it can be reused multiple times in the program.

Example

int sum(int a,int b)

[Link] 1
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

return a+b;

This function calculates the sum of two numbers.

CATEGORIES OF FUNCTIONS IN C
Functions in C are divided into two types.

1 Library Functions

Library functions are predefined functions available in the C library.

Examples

• printf()

• scanf()
• sqrt()

• strlen()
• cos()

• strcat()

These functions are already written by programmers and stored in the C library.

Programmers can use them directly.

Example

printf("Hello World");

2 User-Defined Functions

Functions created by the programmer are called user-defined functions.

Example

int add(int a,int b)

return a+b;

}
Programmers create such functions to perform specific tasks.

[Link] 2
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

Need for User-Defined Functions

Every C program must contain the function main() because execution begins from the main function.

However, writing the entire program inside the main() function is not recommended.

This leads to several problems:

• Program becomes too long

• Program becomes difficult to understand


• Debugging becomes complicated

• Maintenance becomes difficult

To overcome these problems, the program is divided into smaller parts.

Each part performs a specific task.

These parts are implemented as functions.

Advantages of User-Defined Functions

Using user-defined functions provides several advantages.


1 Modular Programming

Functions allow the program to be divided into smaller modules.

Each module performs a specific task.

This method is called modular programming.

2 Code Reusability

A function can be reused multiple times in the same program or in different programs.
Example

A function to calculate factorial can be used many times.

3 Easier Debugging

If an error occurs, it can be located easily because each function performs a specific task.

4 Reduced Program Size


Instead of repeating code many times, the function can be called whenever required.

[Link] 3
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

5 Better Program Organization

Functions improve program readability and structure.

Top-Down Modular Programming

In top-down programming, the problem is solved in stages.

The programmer first solves the main problem, then divides it into smaller tasks.

Each task is implemented as a function.

Example

Suppose we are developing a student result program.

The tasks may be divided as follows:

1 Read student data


2 Calculate total marks
3 Calculate average
4 Display result

Each task can be written as a separate function.

Multi-Function Program

A C program may contain many functions.

Example structure

main()
function1()

function2()

[Link] 4
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

function3()

Each function performs a specific task.

The main function controls the program execution.

Example of Multi-Function Program

#include<stdio.h>
void printline()

int i;

for(i=0;i<39;i++)

printf("-");

printf("\n");

int main()
{

printline();

printf("This illustrates the use of C functions\n");

[Link] 5
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

printline();

return 0;

Output

---------------------------------------

This illustrates the use of C functions


---------------------------------------

Flow of Control in Functions

Program execution always begins from the main() function.

Steps of execution:

1 Program starts from main().


2 main() encounters a function call.
3 Control transfers to the called function.
4 The function executes its statements.
5 Control returns back to main().

Function Calling Rules

Important points about function calls:

1 A function can call another function.


2 A function can call itself (recursion).
3 A function can be called multiple times.
4 A called function may call another function.

Modular Programming

Modular programming is a technique used in software development.

It involves dividing a large program into smaller modules.

Each module performs a single task.

Characteristics of Modular Programming

1 Each module performs one specific task.


2 Communication between modules occurs through function calls.

[Link] 6
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

3 Modules are independent.


4 Programs become easier to debug and maintain.
5 Each module has one entry and one exit point.

Elements of User-Defined Functions

To use a user-defined function, three important elements are required.

1 Function Definition
2 Function Call
3 Function Declaration

1 Function Definition

Function definition contains the actual code of the function.

General syntax

return_type function_name(parameter_list)

statements;
}

Example

int add(int a,int b)

int sum;

sum = a + b;

return sum;

}
Explanation

• int → return type

• add → function name

• a,b → parameters

• return sum → value returned to calling function

[Link] 7
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

2 Function Call

A function call is used to execute the function.

Example

sum = add(10,20);

Here:

• add() is called
• values 10 and 20 are passed

• function returns the sum

3 Function Declaration (Function Prototype)

Before using a function, it must be declared.

This informs the compiler about:


• function name

• return type
• number of parameters

Syntax

return_type function_name(parameter_types);

Example

int add(int,int);

Example Program Using All Three Elements


#include<stdio.h>

int add(int,int); // function declaration

int main()

int result;

result = add(5,10); // function call

printf("Sum = %d",result);
return 0;

[Link] 8
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

int add(int a,int b) // function definition

return a+b;

Output
Sum = 15

Functions are an essential feature of the C programming language. They allow programs to be divided
into smaller modules, making them easier to develop, test, and maintain. Functions can be categorized
as library functions and user-defined functions. To use a user-defined function, three elements are
required: function definition, function call, and function declaration. Functions enable modular
programming and promote code reusability.

FUNCTION DEFINITION IN C

A function definition (also called function implementation) describes how a function works.
It contains the complete instructions that specify the task performed by the function.

A function definition generally contains six elements:

1. Function name
2. Function type (return type)

3. List of parameters

4. Local variable declarations

5. Function statements

6. Return statement

These six elements are grouped into two parts.

1. Function Header
Contains the first three elements:

• Function type

• Function name

• Parameter list

2. Function Body

[Link] 9
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

Contains the remaining elements:

• Local variable declarations

• Function statements

• Return statement

GENERAL FORMAT OF FUNCTION DEFINITION


return_type function_name(parameter_list)

local_variable_declarations;

function_statements;

return expression;

}
Explanation:

• return_type → data type of value returned


• function_name → name of the function

• parameter_list → list of input variables

• local_variable_declarations → variables used inside the function

• function_statements → instructions performed by the function

• return expression → value returned to calling function

FUNCTION HEADER
The function header consists of three components:

1. Function type (return type)

2. Function name

3. Parameter list

Important rule:

There is no semicolon at the end of the function header.

Example:
float mul(float x, float y)

[Link] 10
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

Here:

• float → return type

• mul → function name

• (float x, float y) → parameter list

FUNCTION TYPE (RETURN TYPE)


The function type specifies the type of value that the function returns to the calling program.

Example:

int sum(int a, int b)

This function returns an integer value.

Common return types:

• int
• float

• double
• char

If the function does not return any value, the return type should be:

void

Example:

void display()

Important note:

If the return type is not specified, C automatically assumes it to be int.


However, it is good programming practice to explicitly mention the return type.

FUNCTION NAME

The function name is a valid identifier used to identify the function.

Rules for function names:

• Must begin with a letter or underscore

• Cannot contain spaces


• Cannot be a keyword

[Link] 11
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

• Should describe the function’s task

Example:

calculate_sum

display_result

find_average

FORMAL PARAMETER LIST

The parameter list declares variables that receive values from the calling function.

These variables are called:

• formal parameters

• arguments

Example:
int sum(int a, int b)

Here:
• a and b are formal parameters.

They receive values from the calling function.

Example call:

sum(10,20)

Here:

• 10 and 20 are actual parameters.

RULES FOR PARAMETER LIST

1. Parameters must be separated by commas.

2. Each parameter must have its data type specified.

3. Combined declarations are not allowed.

Correct:

int sum(int a, int b)

Incorrect:
int sum(int a, b)

[Link] 12
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

FUNCTIONS WITHOUT PARAMETERS

Some functions do not require input values.

Such functions use void in parameter list.

Example:

void printline(void)

{
printf("----------");

This function:

• receives no parameters

• returns no value

Some compilers allow:


void printline()

But the recommended practice is:


void printline(void)

FUNCTION BODY

The function body contains statements needed to perform the task.

It consists of three parts:

1. Local variable declarations

2. Function statements
3. Return statement

Example:

int mul(int x,int y)

int p;

p = x * y;

return p;
}

[Link] 13
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

Explanation:

• int p → local variable

• p = x*y → function statement

• return p → return value

LOCAL VARIABLES
Variables declared inside a function are called local variables.

Properties:

• Accessible only within the function

• Cannot be used outside the function

Example:

int sum(int a,int b)


{

int s;
s = a + b;

return s;

Here:

s is a local variable.

RETURN STATEMENT
The return statement is used to send a value back to the calling function.

Syntax:

return expression;

Example:

return x*y;

Important points:

• A function can return only one value at a time.


• A function may contain multiple return statements.

[Link] 14
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

TYPES OF RETURN STATEMENTS

1 Plain Return

Used when the function does not return a value.

Example:

return;

Example usage:
if(error)

return;

This immediately transfers control to the calling function.

2 Return with Expression

Used when a value is returned.


Example:

return x*y;
Example function:

int mul(int x,int y)

return x*y;

DEFAULT RETURN TYPE


By default, C assumes the return type of a function as int.

Example:

sum(a,b)

But good programming practice is to always specify the return type.

FUNCTION CALL

A function call is used to invoke or execute a function.


General syntax:

[Link] 15
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

function_name(argument_list);

Example:

y = mul(10,5);

Steps during function call:

1. Control transfers from calling function to called function.

2. Actual parameters are passed to formal parameters.


3. Function executes its statements.

4. Return statement sends value back to calling function.

5. Control returns to calling function.

EXAMPLE OF FUNCTION CALL

#include<stdio.h>
int mul(int x,int y)

{
return x*y;

int main()

int result;

result = mul(10,5);

printf("Result = %d",result);
return 0;

Output

Result = 50

DIFFERENT WAYS TO CALL FUNCTIONS

Example function:
int mul(int x,int y)

[Link] 16
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

Possible function calls:

mul(10,5)

mul(m,5)

mul(10,n)

mul(m,n)

mul(m+5,10)
mul(10,mul(a,b))

mul(expression1,expression2)

FUNCTION CALL IN EXPRESSIONS

If a function returns a value, it can be used in expressions.


Examples:

printf("%d", mul(p,q));
y = mul(p,q) / (p+q);

if(mul(m,n) > total)

printf("large");

INVALID FUNCTION USAGE

A function cannot appear on the left side of an assignment operator.

Invalid statement:
mul(a,b) = 15;

This produces a compiler error.

FUNCTIONS WITHOUT RETURN VALUE

Functions with return type void do not return any value.

Example:

void printline()
{

[Link] 17
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

printf("----------");

Function call:

printline();

Note:

Semicolon is required after function call.

PARAMETER MATCHING RULES

Actual parameters must match formal parameters in:

1. Number

2. Order

3. Data type
Example:

Function definition:
int sum(int a,int b)

Correct call:

sum(10,20)

Incorrect call:

sum(10)

sum(10,20,30)

Functions in C are essential building blocks of programs. A function definition contains a header and
body, which together define the behavior of the function. The header specifies the function name, return
type, and parameters, while the body contains declarations, statements, and return statements. Functions
are executed through function calls, where actual parameters are passed to formal parameters. Functions
help create modular, reusable, and well-structured programs.

[Link] 18
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

FUNCTION DECLARATION IN C

Before a function is used in a program, it must be declared.


A function declaration is also called a function prototype.

A function declaration informs the compiler about:

• function name

• return type

• number of parameters

• data types of parameters

This helps the compiler verify that the function is used correctly.

PARTS OF FUNCTION DECLARATION

A function declaration contains four parts:

1. Function type (return type)

2. Function name

3. Parameter list

4. Terminating semicolon

GENERAL FORMAT

return_type function_name(parameter_list);

Example

int mul(int m, int n);

This means:

• function name → mul


• return type → int
• parameters → two integers

[Link] 19
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

IMPORTANT POINTS ABOUT FUNCTION PROTOTYPE

1. Parameters must be separated by commas.

Example

int sum(int a, int b);

2. Parameter names in prototype and function definition need not be the same.

Example
Prototype

int sum(int x, int y);

Definition

int sum(int a, int b)

This is valid.

3. Data types of parameters must match in:


• number

• order
• type

4. Parameter names are optional.

Example

int sum(int, int);

5. If a function has no parameters, use

void display(void);

6. Return type is optional if the function returns int.


However, it is better to specify the return type explicitly.

7. If the function returns no value, return type must be void.

Example

void printline(void);

8. If declared types do not match the function definition, the compiler produces an error.

[Link] 20
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

WHERE FUNCTION PROTOTYPES ARE PLACED

Function declarations may be placed in two places.

1 Global Prototype

Placed before all functions.

Example

int sum(int,int);
int main()

These declarations are accessible to all functions.

2 Local Prototype
Placed inside a function definition.

Example
main()

int sum(int,int);

This declaration is accessible only inside that function.

SCOPE OF FUNCTION
The region where a function can be accessed is called the scope of the function.

It depends on where the function prototype is declared.

Best practice:

Declare all prototypes before main().

This improves:

• readability

• documentation
• flexibility

[Link] 21
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

PROTOTYPES: ARE THEY NECESSARY?

Technically, prototypes are not mandatory.

If a function is used without declaration, C assumes:

• return type = int

• parameter types match

If these assumptions are wrong, errors occur during linking.


Therefore, it is recommended to always include function prototypes.

PARAMETERS IN FUNCTIONS

Parameters are used in three places.

1. Function declaration (prototype)

2. Function definition
3. Function call

FORMAL AND ACTUAL PARAMETERS

Formal Parameters

Parameters used in:

• function definition

• function prototype

Example

int sum(int a,int b)


Here:

a and b → formal parameters

Actual Parameters

Parameters used in function call.

Example

sum(10,20);
Here:

[Link] 22
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

10 and 20 → actual parameters

RULES FOR PARAMETERS

Formal and actual parameters must match in:

• number

• order
• data type

However, their names need not match.

CATEGORIES OF FUNCTIONS

Functions in C can be divided into five categories.

1. Functions with no arguments and no return value


2. Functions with arguments and no return value

3. Functions with arguments and return value


4. Functions with no arguments but return value

5. Functions returning multiple values

1 FUNCTIONS WITH NO ARGUMENTS AND NO RETURN VALUE

These functions:

• do not receive any data

• do not return any value


Only control transfer occurs between functions.

Example

void printline()

printf("------------");

Function call
printline();

[Link] 23
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

This function simply performs a task.

CHARACTERISTICS

• No data communication

• Used for tasks like printing messages

2 FUNCTIONS WITH ARGUMENTS BUT NO RETURN VALUE

These functions:

• receive data from calling function

• do not return any value

Example

void display(int x)
{

printf("%d",x);
}

Function call

display(10);

DATA COMMUNICATION

Data flows in one direction:

Calling Function → Called Function

EXAMPLE

void value(float p, float r, int n)

Actual call

value(500,0.12,5);

Assignment occurs internally

p = 500
r = 0.12

[Link] 24
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

n=5

IMPORTANT NOTE

Only copies of actual arguments are passed.

Changes inside function do not affect original variables.

3 FUNCTIONS WITH ARGUMENTS AND RETURN VALUE


These functions:

• receive input data

• perform computation

• return result to calling function

This allows two-way communication.

Example
int sum(int a,int b)

{
return a+b;

Function call

result = sum(5,10);

Data flow

main → function (arguments)

function → main (return value)


FUNCTION EXECUTION PROCESS

Example

amount = value(principal,inrate,period);

Steps:

1 Control transfers to function


2 Actual arguments assigned to formal parameters
3 Function executes statements
4 Return statement sends value back
5 Value assigned to variable amount

[Link] 25
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

RETURNING FLOAT OR DOUBLE VALUES

By default, functions return int.

If the function should return float or double, specify it explicitly.

Example

double power(int,int);

Incorrect type matching may produce unpredictable results.

4 FUNCTIONS WITH NO ARGUMENTS BUT RETURN VALUE

These functions:

• do not take input parameters

• return a value

Example
int getvalue()

{
int x;

scanf("%d",&x);

return x;

Example call

num = getvalue();

Example from Library

getchar() function

• takes no arguments

• returns a character

5 FUNCTIONS RETURNING MULTIPLE VALUES

Normally, functions return only one value.


However, multiple values can be returned using pointers.

[Link] 26
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

This is studied later with pointer concepts.

NESTING OF FUNCTIONS

C allows functions to call other functions.

Example structure

main → function1 → function2 → function3


This is called function nesting.

Example

main()

ratio();

}
ratio() may call another function

difference();

EXAMPLE PROGRAM LOGIC

We want to calculate

a / (b - c)

Steps

1 main reads values


2 main calls ratio()
3 ratio calls difference()
4 difference checks whether (b - c) = 0

If zero

ratio returns 0

Otherwise

ratio returns a/(b-c)

[Link] 27
INTRODUCTION TO C PROGRAMMING MODULE-04 AZ Documents

NESTED FUNCTION CALLS

Functions can also be nested inside expressions.

Example

P = mul(mul(5,2),6);

Evaluation

mul(5,2) = 10
mul(10,6) = 60

Result

P = 60

IMPORTANT RULE

Nesting means calling functions inside functions, not defining one function inside another.
Defining a function inside another function is illegal in C.

[Link] 28
Thank You
We’re glad to be part of your engineering journey.
Keep exploring, keep innovating, and keep growing.

AZ Documents
Your Engineering Study Partner
[Link]

Quality notes, simplified explanations, and


student-focused resources for every semester.

Connect With Us:


[Link] [Click Here]

@azdocuments [Click Here]


Join Our Student Community: [Click Here]

© 2025 AZ Documents. All rights reserved.


This material is intended for educational purposes only.
Redistribution without permission is prohibited.

You might also like