Lecture-SP15-Functions-2
Lecture-SP15-Functions-2
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.
Category of Functions
• Category 2: Functions with arguments
and no return values.
Category of Functions
• Category 3: Functions with arguments
and one return value.
Functions in C 13
Here's an example:
Category of Functions
• Category 4: Functions with no arguments,
but return a value.
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.
Functions in C 16
Here's an example:
Category of Functions
• Category 5: Functions that return
multiple values.
Functions in C 17
Here's an example:
Example:
• Function with Array Parameters:
Functions in C 18
Here's an example:
Example:
• Function with Parameters and
return:
Functions in C 19
?
Functions in C
THE END
20