0% found this document useful (0 votes)
27 views

Functions in C

The document discusses functions in C programming. It explains what functions are, the benefits of using functions, types of functions including built-in and user defined functions. It also covers function prototypes, definitions, calls, and how variables can have local, global or static scope.

Uploaded by

Samyak Anand
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Functions in C

The document discusses functions in C programming. It explains what functions are, the benefits of using functions, types of functions including built-in and user defined functions. It also covers function prototypes, definitions, calls, and how variables can have local, global or static scope.

Uploaded by

Samyak Anand
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 28

FUNCTIONS IN C

FUNCTIONS
• A function is a group of statements that together perform a task. Every C
program has at least one function, which is main(), and all the most trivial
programs can define additional functions.

• Suppose, you need to create a program to create a circle and color it. You can
create two functions to solve this problem:
• create a circle function
• create a color function

• Dividing a complex problem into smaller chunks makes our program easy to
understand and reuse.
Benefits of using the FUNCTIONS

• The function provides modularity.


• The function provides reusable code.
• In large programs, debugging and editing tasks is easy
with the use of functions.
• The program can be modularized into smaller parts.
• Separate function independently can be developed
according to the needs.
Types of FUNCTIONS
Built-in(Library) Functions

• The system provided these functions and stored in the library.


Therefore it is also called Library Functions.
• e.g. scanf(), printf(), sqrt(), etc.
• To use these functions, you just need to include the appropriate C
header files.

User Defined Functions These functions are defined


by the user at the time of writing the program.
Library FUNCTIONS
Library Functions
Parts of Function
• Function Prototype (function declaration)
• Function Definition
• Function Call
Function Prototype

• Function prototypes are always declared at the


beginning of the program indicating the name of the
function, the data type of its arguments which is
passed to the function and the data type of the
returned value (by default-int) from the function.
Function Definition
Calling a function in C
Summarization
C Program is a collection of one or more functions.

A function gets called when its name is followed by a


semicolon.
e.g. main( )
{
fun( );
}

fun( )
{
statement1;
statement2;
statement3;
}
Your Turn…
Write a program that:
• prompts the user for an integer
• calls a function to print all numbers less than the user’s
integer that are divisible by 5
Variable Scope
• A scope is a region of the program, and the scope of
variables refers to the area of the program where the
variables can be accessed after its declaration.
• In C every variable defined in scope. You can define scope
as the section or region of a program where a variable has its
existence; moreover, that variable cannot be used or
accessed beyond that region.
• In C programming, variable declared within a function is
different from a variable declared outside of a function.
Variable Scope
Local variables
• Accessible within code block/function where defined

Global variables
•Variables that are declared outside of a function block and can be accessed
inside the function is called global variables.

•Global variables are defined outside a function or any specific block, in most
of the case, on the top of the C program. These variables hold their values all
through the end of the program and are accessible within any of the functions
defined in your program.

•Any function can access variables defined within the global scope, i.e., its
availability stays for the entire program after being declared.
Local Variables
Variables that are declared inside a function or block are called local variables.
They can be used only by statements that are inside that function or block of
code. Local variables are not known to functions outside their own. The
following example shows how local variables are used.
Variable Scope
main()
{
int a = 2;
printf (“%d\n”,a); // 2
{
int a = 5;
printf (“%d\n”,a); // 5
}
printf (“%d\n”,++a); // 2
}
Global Variables
Global Variables
A program can have same name for local and global variables but the value of
local variable inside a function will take preference. Here is an example:
Initializing Local and Global Variables
When a local variable is defined, it is not initialized by the system, you must
initialize it yourself. Global variables are initialized automatically by the system
when you define them as follows:

:
Static Variables
Static variables have a property of preserving their value even after they are
out of their scope!

Hence, static variables preserve their previous value in their previous scope
and are not initialized again in the new scope.

Syntax:

static data_type var_name = var_value;


Static Variables (Cont.)
Following are some interesting facts about static variables in C.

•A static int variable remains in memory while the program is running. A


normal or auto variable is destroyed when a function call where the variable
was declared is over.

•Static variables are allocated memory in data segment, not stack segment.

•Static variables (like global variables) are initialized as 0 if not initialized


explicitly. For example in the below program, value of x is printed as 0, while
value of y is something garbage.
TYPES OF FUNCTION CALLS
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.
Example
Write a program to calculate and print the area and the perimeter of a circle. Note that
the radius is to be entered by the user. (Use Call by value approach)

#include<stdio.h>
#define pi 3.14
float area(float);
float perimeter(float); //function prototype
main( )
{
float r, a, p;
printf(“Enter the radius\n”);
scanf(“%f”,&r);
a = area(r); //function calling
p = perimeter(a);
printf(“The area = %.2f, \n The Perimeter = %.2f”, a, p);
}

float area(float x) //function definition


{
return pi*x*x;
}

float perimeter(float y)
{
return 2.0*pi*y;
}
TYPES OF FUNCTION CALLS
Call by Reference:

When a function is called by an argument/parameter


which is a pointer (address of the argument) the copy of
the address of the argument is passed to the function.
Therefore a possible change on the data at the
referenced address change the original value of the
argument.
Pointers as Function Argument
• Call by reference with pointer arguments
• Pass address of argument using & operator
• Allows you to change actual location in memory
• Arrays are not passed with & because the array name is
already a pointer
• * operator
• Used as alias/nickname for variable inside of function
void double( int *number )
{
*number = 2 * ( *number );
}
• *number used as nickname for the variable passed
Notice that the function
prototype takes two pointer
void main()
to an integer (int *).
{
int a,b; Notice how the address
void swap(int *x,int *y) of number is given -
a=10; swap expects two
b=20;
pointer (an address of
printf(“Value of a before function call: %d\n”,a);
variables).
Output
printf(“Value of b before function call: %d\n”,b);
Value of a before function call : 10
swap(&a,&b); Value of b before function call : 20
printf(“Value of a after function call: %d\n”,a); Value of a during function : 20
printf(“Value of b after function call: %d\n”,b); Value of b during function : 10
} Value of a after function call : 20

void swap(int *x,int *y) Value of b after function call : 10

{
int temp;
temp=*x; *x=*y; *y=temp; Inside swap, *x,*y is
printf(“Value of a during function:%d\n”,*x); used (*x,*y are
printf(“Value of b during function:%d\n”,*y); numbers).
}
Difference between call by value and call by
reference
Difference between Argument and Parameter

You might also like