Lecture-SP14-Functions-1
Lecture-SP14-Functions-1
Structured Programming
Lecture 14
Functions in C (1)
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
• Parameters passing to Functions
• Main Function
• Library Functions
Functions in C 2
Top-Down Modular Programming using Functions
• In top down approach, we use following approach to
solve any problem:
1. First we will make a high level design of a problem
statement.
2. After that we will write the main function.
3. From the main function we will call the sub functions.
Generally, the large program is divided into small sub-
functions (each function will do a specific task) which
improves the modularity of a program.
4. Later we will implement all the sub functions based on
requirements.
• That's why C is called the top down approach.
Functions in C 3
Top-Down Modular Programming using Functions
• Example: Write a c program to implement a simple calculator.
• High-Level Design:
• declare two integers
• scan the values from the user
• call addition
• call subtraction
• call multiplication
• call division
• addition, subtraction, multiplication, division are sub functions.
Functions in C 4
Top-Down Modular Programming using Functions
• Write main function and call sub functions.
Functions in C 6
Functions
• How a function works in C programming?
• Declaration: Before using a function in the program, we need
to declare it. A function declaration tells the compiler about
the function's name, return type, and parameters (if any). This
is also called a function prototype. Example:
int add(int a, int b);
• Definition: The function definition contains the actual
implementation of the function. It specifies what the function
does when called. Example:
int add(int a, int b) {
return a + b;
}
Functions in C 9
Types of Functions
• Standard library functions: The standard library functions are built-in
functions in C programming.
• These functions are defined in header files. For example, The printf() is a
standard library function to send formatted output to the screen (display output
on the screen). This function is defined in the stdio.h header file.
• Hence, to use the printf() function, we need to include the stdio.h header file
using #include <stdio.h>.
• Another example: The sqrt() function calculates the square root of a number.
The function is defined in the math.h header file.
Functions in C 10
How user-defined function works?
Types of Functions
• User-defined function:
• You can also create functions as per your need. Such
functions created by the user are known as user-
defined functions.
Functions in C 11
Elements of User-defined Function
• There are three aspects of a C function: function declaration, function definition, and function
call.
• Function declaration:
• A function must be declared globally in a c program to tell the compiler about the function name,
function parameters, and return type.
• Function call:
• Function can be called from anywhere in the program. The parameter list must not differ in function
calling and function declaration. We must pass the same number of functions as it is declared in the
function declaration.
• Function definition:
• It contains the actual statements which are to be executed. It is the most important aspect to which
the control comes when the function is called. Here, we must notice that only one value can be
returned from the function.
Functions in C 12
Elements of User-defined Function
• There are three aspects of a C function:
Functions in C 13
Defining a Function
• The general form of a function definition in C programming language is as follows −
Functions in C 14
Defining a Function
• A function definition in C programming consists of a function header and a function body. Here
are all the parts of a function:
• Return Type: A function may return a value. The return_type is the data type of the value the function returns.
Some functions perform the desired operations without returning a value. In this case, the return_type is the
keyword void.
• Function Name: This is the actual name of the function. The function name and the parameter list together
constitute the function signature.
• Parameters: A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter.
This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and
number of the parameters of a function. Parameters are optional; that is, a function may contain no
parameters.
• Function Body: The function body contains a collection of statements that define what the function does.
Functions in C 15
C Program using function:
Defining a Function
• Example: This is a simple C
program to demonstrate functions.
Functions in C 16
Examples of Function Definition
1. Function with no parameters and no return value:
• This function takes two integers as parameters, multiplies them, and prints the result.
Functions in C 17
Examples of Function Definition
3. Function with parameters and return value:
• This function takes two integers as parameters, adds them, and returns the result.
4. Function with no parameters and return value:
Functions in C 18
Examples of Function Definition
5. Function with array parameters:
• This function takes an array and its size as parameters and prints all the elements of
the array.
Functions in C 19
Examples of Function Definition
6. Function with pointer parameters:
• This function takes two integer pointers as parameters and swaps the values they
point to.
Functions in C 20
Examples of Function Definition
7. Function with multi-dimensional array parameters:
• This function takes a 2D array and its dimensions as parameters and prints all the
elements of the array.
Functions in C 21
?
Functions in C
THE END
22