0% found this document useful (0 votes)
0 views

Lecture-SP15-Functions-2

This document is a lecture on functions in C programming, covering topics such as function declarations, calls, and categories of functions based on their parameters and return values. It explains the structure of function declarations, the process of calling functions, and the different types of functions including those with no arguments, with arguments, and those that return multiple values. The lecture is prepared by Md. Mijanur Rahman from Jatiya Kabi Kazi Nazrul Islam University, Bangladesh.

Uploaded by

Kishor Dongare
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Lecture-SP15-Functions-2

This document is a lecture on functions in C programming, covering topics such as function declarations, calls, and categories of functions based on their parameters and return values. It explains the structure of function declarations, the process of calling functions, and the different types of functions including those with no arguments, with arguments, and those that return multiple values. The lecture is prepared by Md. Mijanur Rahman from Jatiya Kabi Kazi Nazrul Islam University, Bangladesh.

Uploaded by

Kishor Dongare
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

CSE 06131223  CSE 06131224

Structured Programming
Lecture 15
Functions in C (2)

Prepared by________________________________
Md. Mijanur Rahman, Prof. Dr.
Dept. of Computer Science and Engineering
Jatiya Kabi Kazi Nazrul Islam University, Bangladesh
www.mijanrahman.com
Contents
FUNCTIONS IN C
• Top-Down Modular Programming using Functions
• Functions in C
• Why do we need function?
• Types of Functions
• Elements of User-defined Function
• Function Definition
• Function Declaration
• Function Calling
• Category of Functions
• Parameters passing to Functions
• Main Function
• Library Functions
Functions in C 2
Function Declarations
• Like variables, all function in a C program must be declared, before they invoked. A
function declaration, also known as function prototype, consists of four parts, such as-
• Function Type (Return Type)
• Function Name
• Parameter List
• Terminating Semicolon (;)

Functions in C 3
Function Declarations
• A function declaration tells the compiler about a function name and how to call the function. The
actual body of the function can be defined separately.
• A function declaration has the following parts:
return-type function-name( parameter list );
• For example:
int max(int num1, int num2);
• Parameter names are not important in function declaration only their type is required, so the
following is also a valid declaration:
int max(int, int);
• Function declaration is required when you define a function in one source file and you call
that function in another file. In such case, you should declare the function at the top of the file
calling the function.

Functions in C 4
Function Declarations
• A few acceptable forms of function declaration are:
int max(int, int);
max(int a, int b);
max(int, int);

• When a function does not take any parameter and does not return any value, its
prototype is written as:
void display(void);

Functions in C 5
Function Calls
• While creating a C function, you give a • For Example:
definition of what the function has to do.
int max(int x, int y)
• To use a function, you will have to call that
{
function to perform the defined task. ------------------------
• When a program calls a function, the program ------------------------
control is transferred to the called function. }
int main()
• A called function performs a defined task {
and when its return statement is executed or int a, b;
when its function-ending closing brace is ------------------------
reached, it returns the program control back max(a,b); //function calling
to the main program. ------------------------
}
Functions in C 6
Function Calls
• There are many different ways to call a function. Listed below are some of the ways the
function mul can be invoked:
mul(10, 5)
mul(m, 5)
mul(10, n)
mul(m, n)
mul(m+5, 10)
mul(10, mul(m, n))
mul(exp1, exp2)

Functions in C 7
Function Calls
• A function which returns a value can be used in expressions like any other variable. Each of
the following statements is valid:
printf(“%d\n”, mul(a, b);
y = mul(a, b) / (a+b);
if (mul(a, b)>total)
print(“large”);

• However, a function cannot be used on the right side of an assignment statement. For
instance,
mul(a, b) = 15; is invalid.

Functions in C 8
//function declaration

Function Calls
• To call a function, we simply need to pass the
required parameters along with the function //function calling
name, and if the function returns a value,
then we can store the returned value.
//function definition

• Example:
• Write a C Program To Swap Two Numbers
using Function.

Functions in C 9
Category of Functions
• A function, depending on whether arguments (or parameters) are present or not and
whether a value is returned or not, may belong to one of the following categories:
• Category 1: Functions with no arguments and no return values.
• Category 2: Functions with arguments and no return values.
• Category 3: Functions with arguments and one return value.
• Category 4: Functions with no arguments, but return a value.
• Category 5: Functions that return multiple values.

Functions in C 10
For example:

Category of Functions
• Category 1: Functions with no arguments
and no return values.

• In C, we can define functions that don't


take any arguments and don't return any
value. Such functions are commonly used
for tasks that don't require input
parameters or for performing actions
without needing to produce any specific
result.
Functions in C 11
Here's an example:

Category of Functions
• Category 2: Functions with arguments
and no return values.

• In C, we can define functions that accept


arguments (also called parameters) but
don't return any value. These functions
are used when we need to perform some
actions or operations on the provided
arguments without needing to produce a
specific result.
Functions in C 12
Here's an example:

Category of Functions
• Category 3: Functions with arguments
and one return value.

• In C programming, functions can have


arguments (parameters) and can also
return a single value. This allows you to
pass data into the function, perform
some operations on that data, and then
return a result back to the calling code.

Functions in C 13
Here's an example:

Category of Functions
• Category 4: Functions with no arguments,
but return a value.

• In C programming, functions can be


defined without any arguments but can
still return a value. This allows you to
perform some computation or task
within the function and then return a
result to the calling code.

Functions in C 14
Category of Functions
• Category 5: Functions that return multiple values.
• In C, functions inherently return only one value. However, we can simulate the
concept of returning multiple values by using pointers or structures.
• Two common approaches to achieve this:
• Using Pointers: Pass pointers to variables to the function, which will modify the
values stored at those memory locations.
• Using Structures: Define a structure that holds multiple values, and return an
instance of that structure from the function.

Functions in C 15
Here's an example:

Category of Functions
• Category 5: Functions that return
multiple values.

• Using Pointers: Pass pointers to


variables to the function

Functions in C 16
Here's an example:

Category of Functions
• Category 5: Functions that return
multiple values.

• Using Structures: Define a


structure that holds multiple
values, and return an instance of
that structure from the function.

Functions in C 17
Here's an example:

Example:
• Function with Array Parameters:

• This C program uses a function


that takes an array of numbers as
a parameter, calculates the sum
and average of those numbers,
and returns the results using
pointers.

Functions in C 18
Here's an example:

Example:
• Function with Parameters and
return:

• This C program uses a function


that calculates the principal
amount after a period of time:
A = P (1+r/100)t

Functions in C 19
?
Functions in C
THE END
20

You might also like