0% found this document useful (0 votes)
9 views15 pages

PPA-Unit 6

The document provides an overview of functions in C programming, covering their declaration, definition, types (standard library and user-defined), and parameter passing methods (call by value and call by reference). It also discusses storage classes and recursion, explaining their significance and usage in programming. Key concepts include the structure of a function, the importance of reusability, and the implications of different parameter passing techniques.

Uploaded by

priyvanshr
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)
9 views15 pages

PPA-Unit 6

The document provides an overview of functions in C programming, covering their declaration, definition, types (standard library and user-defined), and parameter passing methods (call by value and call by reference). It also discusses storage classes and recursion, explaining their significance and usage in programming. Key concepts include the structure of a function, the importance of reusability, and the implications of different parameter passing techniques.

Uploaded by

priyvanshr
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/ 15

Unit 6

Functions
Basic types of function, Declaration
and definition, Function call, Types
of function, Parameter passing, Call
by value, Call by reference, Scope of
variable, Storage classes, Recursion.
Functions
• A function is a block of code which only runs
when it is called. You can pass data, known as
parameters, into a function.
• Functions are used to perform certain actions,
and they are important for reusing code.
• Define the code once, and use it many times.
Declaration and Initialization
// Create a function

void myFunction()
{
printf("I just got executed!");
}

void main()
{
myFunction(); // call the 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.
Types of function

There are two types of function in C


programming:
• Standard library functions
• User-defined functions
Library Functions
• C programming language provides some library functions to
perform some predefined tasks. These functions are also
called the built-in or predefined function in the C header files
whose meaning cannot change.
• When we use these functions on any program, we call the
function name with appropriate header files because these
functions are defined inside the header files.
• In other words, we did not require to write the complete code
to perform a specific task; instead, we can directly call the
function in our program whenever it is required.
• For example: printf(), scanf(), getch(), etc., are the predefined
library functions.
User Defined Functions
• A user-defined function is a function written by the user
to write any program code and execute specific actions.
• These user-defined functions can be modified and
execute according to the requirement of the
programmer.
• A programmer can change the user-defined function,
but these functions are not defined in the C header files.
• A user-defined function is made up using the function
declaration, function definition, and the function call.
Call by Value
• In call by value method, the value of the actual parameters is
copied into the formal parameters. In other words, we can say
that the value of the variable is used in the function call in the
call by value method.
• In call by value method, we can not modify the value of the
actual parameter by the formal parameter.
• In call by value, different memory is allocated for actual and
formal parameters since the value of the actual parameter is
copied into the formal parameter.
• The actual parameter is the argument which is used in the
function call whereas formal parameter is the argument which
is used in the function definition.
#include <stdio.h>
void swap(int , int);
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b);
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b);
}
Call by Reference
• In call by reference, the address of the variable is
passed into the function call as the actual parameter.
• The value of the actual parameters can be modified by
changing the formal parameters since the address of
the actual parameters is passed.
• In call by reference, the memory allocation is similar for
both formal parameters and actual parameters. All the
operations in the function are performed on the value
stored at the address of the actual parameters, and the
modified value gets stored at the same address.
• #include <stdio.h>
• void swap(int *, int *);
• int main()
• {
• int a = 10;
• int b = 20;
• printf("Before swapping the values in main a = %d, b = %d\n",a,b);
• swap(&a,&b);
• printf("After swapping values in main a = %d, b = %d\n",a,b);
• }
• void swap (int *a, int *b)
• {
• int temp;
• temp = *a;
• *a=*b;
• *b=temp;
• printf("After swapping values in function a = %d, b = %d\n",*a,*b);
• }
Storage Class
• Storage classes in C are used to determine the
lifetime, visibility, memory location, and initial
value of a variable. There are four types of
storage classes in C:
• Automatic
• External
• Static
• Register
Storage Storage Place Default Value Scope Lifetime
Classes

auto RAM Garbage Local Within


Value function
extern RAM Zero Global Till the end
of the main
program
Maybe
declared
anywhere in
the
program
static RAM Zero Local Till the end
of the main
program,
Retains
value
between
multiple
functions
call
register Register Garbage Local Within the
Value function
Recursion
• Recursion is the process of defining a problem (or
the solution to a problem) in terms of (a simpler
version of) itself.
• Recursion is the technique of making a function
call itself. This technique provides a way to break
complicated problems down into simple problems
which are easier to solve.
• Recursion may be a bit difficult to understand. The
best way to figure out how it works is to
experiment with it.
• Recursion is a widely used phenomenon in
computer science used to solve complex
problems by breaking them down into simpler
ones.
• Recursion is a process by which a function calls
itself directly or indirectly. The corresponding
function is called as recursive function.
• The C programming language supports
recursion, i.e., a function to call itself. But while
using recursion, programmers need to be careful
to define an exit condition from the function,
otherwise it will go into an infinite loop.

You might also like