Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

CSE-125

Computer Programming &


Engineering Analysis

Lecture: 6
Functions
Avishek Das

Department of CSE
CUET
FUNCTIONS
➢Functions in C, can be classified into two types
1. Built-in/Library and
2. User-defined.
Library functions – already defined in header file. Example –
printf(), scanf(), sqrt()
User-defined – needed to be programmed by user. Example –
main(), other functions.
➢Function, also called subprogram that carries out some
specific and well-defined task.

17/11/2022 Department of CSE, Chittagong University of Engineering & Technology 2


➢Modular programming – process of organizing a large program into
small, independent program segments called modules, that are
separately named and individually called programmable units
➢Characteristics -
➢Each module should do only one thing.
➢Communication between module is allowed only by a calling module.
➢A module can be called by one and only one higher module.
➢No communication can take place directly between modules that do not have
calling-called relationship.
➢All modules are designed as single-entry, single-exit systems using control
structures.

17/11/2022 Department of CSE, Chittagong University of Engineering & Technology 3


Function Prototype

return_type function_name (type parameter-name1, type parameter-name1 .….. type parameter-nameN )


{
function body…
Return_Value
}

A prototype declares three attributes associated with a function:


1. Its return type
2. The number of its parameters
3. The type of its parameters

17/11/2022 Department of CSE, Chittagong University of Engineering & Technology 4


17/11/2022 Department of CSE, Chittagong University of Engineering & Technology 5
STRUCTURE OF A FUNCTION

/Arguments

17/11/2022 Department of CSE, Chittagong University of Engineering & Technology 6


USER-DEFINED FUNCTIONS
➢3 elements related to function –
➢Function definition – an independent program module that is specially
written to implement the requirements of the function.
➢Function call – to use it, we need to call it at a required place of the program.
Functions that call the other functions, is called calling function.
➢Function declaration – any functions that are used later should be declared
specifically.
➢Function definition should have –
➢Function name
➢Function type Function header
➢List of parameters
➢Local variable declaration
➢Functions statements Function body
➢Return statement

17/11/2022 Department of CSE, Chittagong University of Engineering & Technology 7


DECLARING PROTOTYPE OF A FUNCTION

17/11/2022 Department of CSE, Chittagong University of Engineering & Technology 8


A simple Function for
number addition
Function for Factorial
Function to find maximum between numbers
Function to Print Odd/Even
Function to Return Odd/Even
Function to find area of triangle
Function to find area of triangle
Array in
Function
Array in
Function

You might also like