Functions in C
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
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 variables are allocated memory in data segment, not stack segment.
#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 perimeter(float y)
{
return 2.0*pi*y;
}
TYPES OF FUNCTION CALLS
Call by Reference:
{
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