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

Function

The document provides an overview of functions in C programming, explaining their importance in breaking down tasks, enhancing code reusability, and improving program organization. It details the structure of functions, including the main function, types of functions (library and user-defined), and key components such as function definition, call, and declaration. Additionally, it covers the significance of return types, parameters, and local variables within functions.

Uploaded by

lordai5016
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)
2 views33 pages

Function

The document provides an overview of functions in C programming, explaining their importance in breaking down tasks, enhancing code reusability, and improving program organization. It details the structure of functions, including the main function, types of functions (library and user-defined), and key components such as function definition, call, and declaration. Additionally, it covers the significance of return types, parameters, and local variables within functions.

Uploaded by

lordai5016
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

Now you can C more…

Function

Bhaskar Jyoti C.
Assistant Professor
Department of Computer Science & Engineering
Rajiv Gandhi University, Rono Hills, Itanagar
1
Function
• Functions break large computing tasks into smaller ones,
• and enable programmers to build on what others have
done instead of starting from the scratch i.e. reusability of
code.
• Ex: we are using scanf() and printf() functions of stdio.h
header file, written by some programmer.
• - to make program more organized and meaningful at the
same time.
• - it also increases understandability of the program by
hiding unnecessary details.
• Better understandability also makes debugging (the act of
finding errors in a program) easy.

Rajiv Gandhi University 2


main()
• “main” is a special function used by the C system to tell
the computer where the program starts.
• i.e. execution of the program starts from here.
• Every program must have exactly one main function.
• More than one main function, confuses the compiler
about starting of the program
• main is immediately followed by an empty pair of
parenthesis ()
• Empty parenthesis means main has no arguments
• Header files, don’t have main function

Rajiv Gandhi University 3


Types of function
• C functions can be classified into two categories:
• 1. Library functions: like printf() and scanf(), which are
already defined and we can directly call these to
perform some task. But, proper header file required to
be addressed at top of the program.
• 2. User defined functions: like main(), display(),
square() etc, which are defined by programmer. So, we
must define these functions before calling it.
• Here main() is a specially recognized user defined
function.

Rajiv Gandhi University 4


main()
• The left brace ‘,‘ marks the start of the body of the
main function and right brace ‘-’ marks the end of the
main function.

• Whatever statements inside these braces will be


executed sequentially
• Syntax:
return_type main(argument)
{
//body
}

Rajiv Gandhi University 5


main()
• If return type is not mentioned then, default is int
• If argument is absent, default is void , it is optional

• Therefore:
• 1. main( ) or int main( ) or main(void) or int main(void) are same
• 2. void main( ) or void main(void) are same

• If return type is int, then last statement of main() must be return 0;


• If return type is void, then last statement of main() may be return ;
(this “return;” is optional, even if you don’t write, program works)

Rajiv Gandhi University 6


Similarities of functions with
variables
• Like the variable names,..function names are
also identifiers. So, it must follow the naming
rules of identifiers.
• Like variables, functions have also type
associated with them.
• Like variables, function names and their types
must be declared and defined before they are
used in a program.

Rajiv Gandhi University 7


Elements of function
1. Function definition
2. Function call
3. Function declaration/prototype
• Each user defined function needs first two of these
elements.
• Third one(ie. function declaration/prototype) depends
upon position of the first i.e. function definition in the
program.
• If function definition is given below the main function, then
function declaration/prototype is also required.
• For library function, only function call is required as
function definition is already present in the appropriate
header file.

Rajiv Gandhi University 8


Function definition
• It represents the task(s) which can be performed by
that function, if it is called.
• Components of function definition:
1. Function name
2. Function type/ Return type function header
3. Function parameter/argument
4. Local Variables
5. Function statements function body
6. Return statement
• Function body does not required in other elements of
function i.e. function call and function prototype

Rajiv Gandhi University 9


1. Function name
2. Function type/ Return type int square(int a)
3. Function {
parameter/argument int temp;
4. Local Variables temp=a*a;
5. Function statements printf(”Square is %d”, temp);
6. Return statement return temp;
}

Rajiv Gandhi University 10


Function definition
1. Function Name
• Like the variable names,..function names are
also identifiers.
• So, it must follow the naming rules of
identifiers.
• In any C program, if any name is followed by a
pair of parenthesis ( ), then that is a function
name.
• The only exception is sizeof(), which is a
keyword and also an operator.
Rajiv Gandhi University 11
Function definition
[Link] type/ Return type
• It represents the type of output that the function
is expected to return to the function that is calling
it.
• Most of the cases, generally main function is the
function that calls a function.
• So, main function gets the output of mentioned
Function type/ Return type.
• If Return type of a function is not written in
definition, then compiler assumes the default
return type as int.
Rajiv Gandhi University 12
Function definition
3. Function parameter/argument
• Every function is created to perform a certain
task and to perform the task it may or mayn’t
need data.
• Ex: If add() function performs addition, then
there must be two numbers over which it will
perform addition.
• But how to provide or input data to function?
• Answer is via function argument/parameter

Rajiv Gandhi University 13


Function definition
3. Function parameter/argument
• It is written inside pair of parenthesis i.e. ( )

• A function may or may not have any parameter.


• An empty pair of parenthesis or void within it indicates no
parameter.
• Ex: void display( ) or int main(void)

• Every parameter has two fields: a data type and a name


• Ex: void display(int a) // “int” and “a” represents one parameter

• If there is more than one parameter, then it must be separated by


comma.
• Ex: void add(int a, int b)

Rajiv Gandhi University 14


Function definition
3. Function parameter/argument
Function parameters has two forms:
1. Formal Parameter and
2. Actual Parameter (discussed in function call)

void square(int a)
{ printf(“\n Square of %d is %d”,a,a*a);
}
• Here, “a” is a placeholder only, it doesn’t create any varible of int
type with name “a”.
• The function definition says that, if we provide an integer value to
square() function, it displays square of it.
• “a” is known as Formal Parameter. A formal parameter appears
only in the function definition.

Rajiv Gandhi University 15


Function definition
4. Local variable
• Local variables are variables defined inside body
of a function. It means it is local to that function
and can’t be accessed by any other function.
• Each time a particular function is called, a set of
new local variables are created.
• These are used as temporary variable to store
some intermediate results. They are temporary
because, as the compiler reaches end of a
function i.e. “-” , it deletes all the local variables
created for that particular function call.
Rajiv Gandhi University 16
Function definition
4. Local variable
int square(int a) // compiler reads programs top to bottom
{ int temp; //but, a “temp” variable is not created when compiler
temp=a*a; //reaches above line. temp is the only local variable of square()
printf(”Square is %d”, temp);
return temp; }
void main()
{int d; //but,in main(), compiler creates “d” variable as it reaches here
d=square(2); //at this point, after getting function call, it creates a “temp”
d=square(3); } // at this point also, it creates another “temp”
• During a function call, control is given back to called function (ie. square) from
calling function (ie. main).
• So, compiler goes back to square() but this time to execute it… so it creates temp,
performs square of 2 operation, returns the value/result to d and deletes temp.
• Next time, it again goes back to square()… so it creates temp, performs square of 3
operation, returns the value/result to d and deletes temp.
Rajiv Gandhi University 17
Function definition
[Link] statements
• All the statements inside body of function are
known as function statements.
• These are enclosed within a pair of brace i.e.{ }
• In case of for, while, do.. while loops and if
statements, we don’t write the , -, if there is
only one statement.
• But, here in function… you have to write the, -
whether zero or one or multiple statements.

Rajiv Gandhi University 18


Function definition
[Link] statement
• A return statement is required, if that function have to
return some calculated value to the calling function.
• It is generally written at the bottom of function body i.e.
just above the closing brace.
• It starts with keyword “return” followed by some variable
name/ constant value/ expression/ function call/ nothing
but only “;” *examples are given below in same order+
• Ex: return a;
• return 0;
• return a*a; //returns square value directly
• return square(a-1); //in recursive functions
• return; // if function return type is void

Rajiv Gandhi University 19


Function definition
[Link] statement
• If a function’s return type is void, programmer may write simply
“return;” or mayn’t write it as return statement becomes optional..
When return type is void…ie. Nothing to return.
• Ex: Both are correct
void display()
{ printf(“Hello World”);
}
--------------------------------
void display()
{ printf(“Hello World”);
return;
}

Rajiv Gandhi University 20


Function definition
[Link] statement
• Every function that has return statement, has generally
only one return statement and ie. also in the bottom.
• But, there can be a function of following type, having
multiple return statements.
• Interesting point is that, only one return statement works
depending upon the value of the condition.
int display(int a)
{ if(a%2==0)
return 3;
else
return 4; // for any value of “a”, only one
} //return statement will work

Rajiv Gandhi University 21


Function definition
[Link] statement
• The most important point to be noticed about return statement is that…
• Type of the value returned by return statement must match with the
Return type of the function.
Ex: Here both are correct as Return type and Type of the value returned are
matching.

char display()
{
return ‘A’;
}
---------------------------------------------------------------------------------------------
float display()
{
return 2.3;
}

Rajiv Gandhi University 22


Function call
• Out of the six components of function definition, a function
call needs only first three i.e. only the function header
required.
• Out of these three, Function type/return type remains
hidden;
• Ex:
void display()
{ printf(“Hello World”);
}
void main()
{display(); //no need to write function return type as void.
} // function with void return type can be called directly

Rajiv Gandhi University 23


Function call
• But, functions that returns int, char, float etc values
must be called with same type of variable with “=“ to
save the returned result.
• Ex:
int display()
{ return 5;
}
void main()
{ int a;
a=display(); //here both “a” and display()’s return
- // type is same… i.e. int

Rajiv Gandhi University 24


Function call
Functions that returns int, char, float etc can be called as a parameter
inside other function’s function call also without the need of “=“.
Ex:
int display()
{ return 5;
}
void main()
{
printf (“Number is %d”,display()); //here printf() is called
- // and inside its, parameter list… display() is called

Above function call doesn’t give error as type of format specifier


(ie.%d) and return type of display() are same ie. int

Rajiv Gandhi University 25


Function call
Direct call
• A function doesn’t perform any task, until it is directly
or indirectly called by main() function.
• Ex. Direct Call:
void main()
{int b=5;
square(b) ; // here b (i.e. 5) is a
square(2); // here 2 is a
square(b+3); // here b+3(ie 8) is a
}
• Above function displays 25, 4, 64 as square respectively
Rajiv Gandhi University 26
Function call
Indirect call
• Ex. Indirect call: Consider display() function uses square() as defined
below
void display(int a)
{ printf(“\n Given no. is %d”,a);
square(a);
}
void main()
{ display(5);
}
• Here main() is calling display function, which is calling square(). So, main()
indirectly calls to square().
• OUTPUT:
Given no. is 5
Square of 5 is 25

Rajiv Gandhi University 27


Function call
Actual Parameter
Now, lets discuss the parameters of function call of following example
again.
void main()
{int b=5;
square(b) ; // here b (i.e. 5) is a
square(2); // here 2 is a
square(b+3); // here b+3(ie 8) is a
}
Here, 5,2 and 8 are the value at the time of function call and these are
known as Actual Parameters i.e. the parameters over which we
need to perform the function actually.
The values passed to a function during a function call(both direct and
indirect) are known as Actual Parameter.

Rajiv Gandhi University 28


Function Declaration/Prototype
• A function declaration or function prototype is
required, if function definition is written after main()
function, in the program.
• Components of function prototype
1. Function name
2. Function type/ Return type
3. Function parameter/argument
4. Terminating semicolon

• Syntax of Function prototype


return_type function_name(parameter_list);

Rajiv Gandhi University 29


Function Declaration/Prototype
No need to write prototype as Need prototype as square() is
defined above main() defined below main()
int square(int a) int square(int a);
{ return a*a; void main()
} { int b;
b=square(5);
}
void main()
{ int b;
int square(int a)
b=square(5); { return a*a;
} }

Rajiv Gandhi University 30


Function Declaration/Prototype
• If a function definition is written after main() function,
in the program and function prototype is not written,
then compiler will generate error, when it reaches a
function call of it inside the main().
• To inform compiler that the particular function is
defined below the main(), we need to place function
prototype above the body of main();
• The data types of parameters are required in same
order, same number as in function definition.
• The parameter names are not mandatory, so writing it
same or different or even if you don’t write… it does
not create any error.

Rajiv Gandhi University 31


Function Declaration/Prototype
• Ex.
int square(int a)
{ return a*a;
}
Function Prototypes of above function can be any of the followings:
int square(int);
int square(int a);
int square(int Q);
As the return type is int, we can omit this.. Like we can for function definition.
If return type of a function is not written in function definition or function prototypes,
compiler assumes it as int.
So, each of the following prototypes are also correct for given function.
square(int);
square(int a);
square(int Q);

Rajiv Gandhi University 32


Thank you

Rajiv Gandhi University 33

You might also like