0% found this document useful (0 votes)
33 views28 pages

3 Functions and Parameters

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

3 Functions and Parameters

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

3.

FUNCTIONS AND
PARAMETERS
Introduction of Function:
• A function is a Self contained block of one or more statements
or a sub-program which perform a specific task.

• Every C program has at least one function ,which is main() ,and


all other are additional functions.

• C function contain set of instructions enclosed by “ { }” which


performs specific operation in a C program.
Function main():
• main() is a function.
• It forms a crucial part of any c program.
• The main() function in C is the entry point of a program where the execution of a program
starts.
• It is a user-defined function that is mandatory for the execution of a program because when
a C program is executed, the operating system starts executing the statements in
the main() function.
• Syntax:
return_type main() Eg:
{ int main()
{
// Statement 1; }
// Statement 2; or
// and so on.. void main()
return; {
} }
Writing user-defined function:
• In order to use function in the program we need to establish
three elements of functions:
1.Function declaration:For functions the declaration needs to
be before the first call of the function.
2. Function definition:It is Actual code for the function to
perform specific task.
3.Function call(accessing a function): In order to use functions
,we function call to access the function.
“The program or function that calls the function is referred to as
calling program or calling function.
General form of function declaration-function
prototype
Like variable function must be declared before it can be used(called).
Syntax:
Returntype functioname( parameters types);

Returntype –type of value returned by function or void if none.


Functionname-unique name identifying function
Parameters types:Datatypes of parameters or parameters
separated by comma.These parameters are called as formal
parameters.
Eg:

int add(int ,int );


float adddata(int,float,char []);
General form of function definition:
• Function definition syntax is:
returntype functionname(parameters)
{
variable declarations;
statement block;
return statement;
}
Eg:
double average(double x,double y)
{
return (x+y)/2;
}
Accessing functions:
• A function can be accessed i.e. called by specifying its name
,followed by a list of parameters enclosed in parenthesis and
separated by commas.
• When function doesn’t require any argument it is called with
function name followed by pair parenthesis .
• The arguments in the function call are known as actual
parameters.
Syntax:
Functionname(argument list);

Eg:
average(4.55,9.99);
//addition of two numbers using function
#include<stdio.h> int add( )
#include<conio.h> {
int add( ); //declaration int a,b,sum;
printf(“enter 1st number”);
void main() scanf(“%d”,&a);
{ printf(“enter 2nd number”);
int sum; printf(“%d”,&b);
sum=add( ); sum=a+b;
printf(“addition is %d”,sum); return sum;
getch(); }
}
//addition of two numbers using parameterized
function
#include<stdio.h> sum=add(num1,num2);
#include<conio.h> printf(“addition is %d”,sum);
int add(int, int); //declaration getch();
}
void main() int add(int a, int b)
{ {
int num1,num2,sum; int sum;
printf(“enter 1st number”); sum=a+b;
scanf(“%d”,&num1); return sum;
printf(“ }
printf(“%d”,&num2);
Passing argument to a function:
• In C, there are different ways in which parameter data can be
passed into and out of functions or from one function to
another function.
• Let us assume that a function B() is called from another
function A().
• A is called the “caller function” and B is called the “called
function or callee function”.
• The arguments which A sends to B are called actual
arguments and the parameters of B are called formal
arguments.
Methods of parameter passing:
There are two methods of parameter passing:
1.Call by value
2. Call by reference

Call by value:
• The actual parameter is passed to a function.
• A new memory area created for the given parameters can
be used only within the function.
• The actual parameters cannot be modified here.
C program to swap values of two variables using swap() function to
demonstrate Function, Passing Arguments to a Function.
(call by value)
#include<stdio.h> swap(a,b);
#include<conio.h> printf(“\nAfter swapping : ”);
void swap(int ,int); printf(“ \n a=%d \nb=%d”,a,b);
void main() }
{ void swap(int x,int y)
int a,b; {
printf(“Enter value of a and b”); int temp;
scanf(“%d%d”,&a,&b); temp=x;
printf(“\nBefore swapping : ”); x=y;
printf(“\n a=%d \n b=%d”,a,b); Y=temp;
}
Call by reference:
• Instead of copying a variable, a memory address is passed
to function as a parameter.
• Address operator(&) is used in the parameter of the called
function.
• Changes in function reflect the change of the original
variables.
C program to swap values of two variables using swap() function to
demonstrate Function, Passing Arguments to a Function.
(call by reference)
#include<stdio.h> swap(&a,&b);
#include<conio.h> printf(“\nAfter swapping : ”);
void swap(int * ,int *); printf(“ \n a=%d \nb=%d”,a,b);
void main() }
{ void swap(int *x,int *y)
int a,b; {
printf(“Enter value of a and b”); int *temp;
scanf(“%d%d”,&a,&b); *temp=*x;
printf(“\nBefore swapping : ”); *x=*y;
printf(“\n a=%d \n b=%d”,a,b); *y=*temp;
}
Recursion
• In C ,function can call itself .In this case ,the function is said to
be recursive.
• Recursion is the process where function calls itself
eg: int add()
{
int sum;
sum=add();
}
C program to find the factorial of a number using a recursive
function.
Storage classes
• Declaration of a variable’s datatype is not the complete
definition of variable .
• For complete definition of variable we have to specify the
storage class of the variable along with its datatype.
• There are four storage classes in c :
1. Auto-automatic
2. External
3. Static
4. Register.
Automatic storage class
• By default,variables in c use the auto storage class.
• This is called automatic because,the variables are automatically
created when needed and deleted when they fall out of the
scope.
• We can specify a variable to have auto storage class by
prefixing the variables declaration with auto keyword.
Syntax:
auto datatype variable;

Eg:
int count;
auto int month;
• Automatic variables are declared at the start of a block(A block
is all statements enclosed within the pair of curly braces)
• Memory is allocated automatically upon entry to a block and
freed automatically upon exit from the block.
• The scope of the variable is local to the block in which they are
declared ,including any blocks nested within that block. Hence
they are called as local variable.
• No block outside the defining block may have direct access to
automatic variables i.e by name.
Illustrates the scope and the life of the variable.
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main() void main()
{ {
auto int a=10; auto int a=10;
{ {
{ auto int a=20;
printf(“\t%d”,a) {
} auto int a=30;
printf(“\t%d”,a); printf(“\t%d”,a)
} }
} printf(“\t%d”,a);
Output:10 10 }
printf(“\t%d”,a);
}
}
Output: 30 20 10
External variables(global)-extern
• Automatic variable has limited scope and limited lifetime.
• In some applications it may be useful to have data which is
accessible from within any block and remains in existence for
entire execution of program.Such variables are called as global
variable.
• C language provides two classes to meet these requirements:
1. Extern
2. Static
• External variables are declared by specifying its type and name
,in then similar manner as an ordinary variable declaration.
• No storage class specifier is used.
• Memory for such variable is allocated when the program begins
execution and remains allocated until the program terminates.
• For most C implementations ,every byte of memory allocated for
an external variable is initialized to zero.
/*Demonstrate the scope of external variables*/ int b=1; //accessible by next1() but not by
#include<stdio.h> main()

#include<conio.h> void next1()

void next1(void); {

int a=1;//external variable,global char a='a'; //local


//scope,scope-main(),next() printf("\nb=%d",b);
void main() b=3; //global
{ printf("\na=%c",a);
a=2; printf("\nb=%d",b);
clrscr(); printf("\n a=%d",a);
printf("a=%d",a); }
next1(); Output:a=2
printf("\na=%d",a); b=1
getch(); a=a
} b=3
a=97
Static variables
• The storage class static similarly provides a lifetime over the
entire program,however it provides a way to limit the scope of
such variables.
• Static storage class is declared with the keyword static.
• These variables are automatically initialized to zero upon
memory allocation just as external variables are.
• Static storage class can be specified for automatic as well as
external variables.
• For static variables,initialization is done only once at compile
time when memory is allocated for the static variable.
//program demonstrate auto //storage class //program demonstrate auto //storage class
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void incr(void) void incr(void)
{ {
auto int a=1; static int a=1;
printf(“%d\t”,a); printf(“%d\t”,a);
a=a+1; a=a+1;
} }
void main() void main()
{ {
incr(); incr();
incr(); incr();
incr(); incr();
getch(); getch();
} }
Output: 1 1 1 Output: 1 2 3
Register variables-register
• Almost all computer processors contain CPU registers ,storing
data there gets rid of the overhead of retrieving the data from
normal memory.
• This memory is quite small compared to normal memory so only
few variables can be stored there.
• Register storage class is declared with the keyword register as
the class specifier when the variable is defined.
• Every type of variable cannot be stored in register , for e.g. if
processor has 16 bit register ,then float ,double value cannot be
stored in register.
• One problem with placing a variable into CPU register is that we
cannot get a pointer to it. As pointers can only point to normal
memory.
• Defining register variable doesn’t mean that the variable will be
stored in a register . If CPU registers are busy in doing some
other task , variable works as if its storage class is auto.
• Eg:
register int count;
Storage classes memory Initial value scope life

auto Memory Garbage Within block End of block

extern Memory 0 Global,multiple Till the end of


files program

static Memory 0 Within block Till the end of


program(value of
variable persists
between different
function calls)

register register Garbage Within block End of block

You might also like