You are on page 1of 11

Lecture 7

Functions And Arrays


C Programming Functions

 A function is a block of code that performs a


specific task.
 Suppose, a program related to graphics needs

to create a circle and color it depending upon


the radius and color from the user. You can
create two functions to solve this problem:
 create a circle function
 color function
Types of functions in C programming

 Depending on whether a function is defined


by the user or already included in C
compilers, there are two types of functions in
C programming
 There are two types of functions in C

programming:
 Standard Library Function
 User Defined Functions
Standard Library Function
 The standard library functions are built-in
functions in C programming to handle tasks
such as mathematical computations, I/O
processing, string handling etc.
 These functions are defined in the header file.

When you include the header file, these


functions are available for use.
 For Example-

printf , scanf , getchar , getch etc these all are


available in stdio.h header file
User Defined Functions
 C languages allow its programmers to define their own functions according to
their requirement and simplicity of the program. Such type of functions created
by the user are called as user-defined functions.
 How user-defined function works?

#include <stdio.h>
void functionName()
{
……..
}
int main()
{
…..
functionName();
……
……

}
How Functions works in C?

 The execution of a C program begins from


the main() function.
 When the compiler encounters 

functionName(); 
inside the main function, control of the program jumps
to
void functionName()
And, the compiler starts executing the codes inside the
user-defined function.
 The control of the program jumps to statement next

to functionName(); once all the codes inside the


function definition are executed.
Types of user defined functions
In C programming, user defined functions are
defined in four types.
They are-
 Functions with no argument and no return

value.
 Functions with no argument and with return

value
 Functions with argument and with return

value
 Functions with argument and no return value
Arrays in C Language
 An array is a collection of data that holds
fixed number of values of same type. 
 For Example:

If you want to store marks of 100 students, you


can create an array for it.
float marks[100];
How to declare an array in C?
data_type array_name[array_size];
Types of Array
 Arrays are classified into two types-
(1) One Dimensional Array
(2) Multi Dimensional Array
Few important notes:
 Arrays have 0 as the first index not 1.
 If the size of an array is n, to access the last

element, (n-1) index is used.
Thank You

You might also like