0% found this document useful (0 votes)
28 views33 pages

Course Code: MCT321 Course: Computer Applications L: 3 HRS., T: 1 HRS., Per Week Total Credits: 07

The document describes a computer applications course with the following details: - Course Code: MCT321 - Course Name: Computer Applications - Credits: 7 - Lectures: 3 hours per week - Tutorials: 1 hour per week

Uploaded by

Jay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views33 pages

Course Code: MCT321 Course: Computer Applications L: 3 HRS., T: 1 HRS., Per Week Total Credits: 07

The document describes a computer applications course with the following details: - Course Code: MCT321 - Course Name: Computer Applications - Credits: 7 - Lectures: 3 hours per week - Tutorials: 1 hour per week

Uploaded by

Jay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Course Code : MCT321

Course: Computer Applications


L: 3 Hrs., T: 1 Hrs., Per week
Total Credits: 07

Dr. Sachin Upadhye


Asst. Professor,
Department of Computer Application, RCOEM
Unit - I

Function in C
• A function is a block of code that performs a
particular task.
• C language provides an approach in which you
can declare and define a group of statements
once in the form of a function and it can be
called and used whenever required.
• C functions can be classified into two categories,
– Library functions
– User-defined functions
Unit - I

Library functions are


those functions which
are already defined in C
library, example printf(),
scanf(), strcat() etc.

You just need to include


appropriate header files
to use these functions.
These are already
declared and defined in
C libraries
Unit - I

A User-defined functions
on the other hand, are
those functions which
are defined by the user
at the time of writing
program.

These functions are


made for code
reusability and for
saving time and space.
Unit - I
Benefits of Using Functions
 It provides modularity to your program's
structure.
 It makes your code reusable. You just have to call
the function by its name to use it, wherever
required.
 In case of large programs with thousands of code
lines, debugging and editing becomes easier if
you use functions.
 It makes the program more readable and easy to
understand.
Unit - I

Function Declaration / Function Prototyping


General syntax for function declaration is,

A function must also be declared before its used.

Function declaration consists of 4 parts.


• Return type
• function name
• parameter list
• terminating semicolon
Unit - I

returntype
• When a function is declared to perform some sort of
calculation or any operation and is expected to
provide with some result at the end, in such cases, a
return statement is added at the end of function body.
• Return type specifies the type of value (int, float, char,
double) that function is expected to return to the
program which called the function.

• Note: In case your function doesn't return any value,


the return type would be void.
Unit - I

functionName
• Function name is an identifier and it specifies
the name of the function.
• The function name is any valid C identifier and
therefore must follow the same naming rules
like other variables in C language.
Unit - I

parameter list
• The parameter list declares the type and
number of arguments that the function expects
when it is called.
• The parameters in the parameter list receives
the argument values when the function is
called. They are often referred as formal
parameters.
Unit - I
Example
Unit - I
functionbody
• The function body contains the declarations and the
statements(algorithm) necessary for performing the
required task.
• The body is enclosed within curly braces { ... } and
consists of three parts.
– local variable declaration(if required).

– function statements to perform the task inside the function.

– a return statement to return the result evaluated by the


function(if return type is void, then no return statement is
required).
Unit - I
Calling a function
• When a function is called, control of the
program gets transferred to the function.
• In the example above, the statement
multiply(i, j);
inside the main() function is function call.
Unit - I
Passing Arguments to a function
• Arguments are the values specified during the
function call, for which the formal parameters
are declared while defining the function.
Unit - I
a

Passing Arguments to a function Returning a value from function


Unit - I
Type of User-defined Functions in C
There can be 4 different types of user-defined
functions, they are:
1. Function with no arguments and no return
value
2. Function with no arguments and a return value
3. Function with arguments and no return value
4. Function with arguments and a return value
Unit - I
Function with no arguments and no return value
Unit - I
Function with no arguments and a return value
Unit - I
Function with arguments and no return value
Unit - I
Function with arguments and a return value
Unit - I
Nesting of Functions
C language also allows nesting of functions i.e to
use/call one function inside another function's
body.
Lets consider that inside the main() function,
function1() is called and its execution starts,
then inside function1(), we have a call for
function2(), so the control of program will go
to the function2(). But as function2() also has a
call to function1() in its body, it will call
function1(), which will again call function2(),
and this will go on for infinite times, until you
forcefully exit from program execution.
Unit - I
What is Recursion?
Recursion is a special way of nesting functions,
where a function calls itself inside it. We must
have certain conditions in the function to break
out of the recursion, otherwise recursion will
occur infinite times.
Unit - I
a
Unit - I
Types of Function calls in C
we can call a function in two different ways, based
on how we specify the arguments, and these
two ways are:
• Call by Value
• Call by Reference (At the time of pointer)
Unit - I
Call by Value
• Calling a function by value means, we pass the
values of the arguments which are stored or
copied into the formal parameters of the
function.
• Hence, the original values are unchanged only
the parameters inside the function changes.
Unit - I
a
Unit - I
But we can change this program to let the
function modify the original x variable, by
making the function calc() return a value, and
storing that value in x.
Unit - I
• Call by Reference (At the time of pointer)
Unit - I
How to pass Array to a Function in C
There are two possible ways to do so, one by using
call by value and other by using call by
reference.
Unit - I
Returning an Array from a function
• We don't return an array from functions, rather
we return a pointer holding the base address of
the array to be returned. But we must, make
sure that the array exists after the function ends
i.e. the array is not local to the function.
Unit - I
Passing a single array
element to a function
we will declare and
define an array of
integers in our main()
function and pass one
of the array element
to a function, which
will just print the
value of the element.
Unit - I
Passing a complete One-dimensional array to a
function
• We will only send in the name of the array as
argument, which is nothing but the address of
the starting element of the array, or we can say
the starting memory address.
Unit - I
a
Unit - I
Passing a Multi-dimensional array to a function

You might also like