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

Function

This document provides an introduction to functions in C programming, explaining their definition, types (library and user-defined), and the process of function declaration, definition, and calling. It emphasizes the importance of functions for code modularity, reusability, and organization, while also covering concepts like passing arguments and return types. Additionally, it includes examples and exercises to reinforce understanding of function usage.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Function

This document provides an introduction to functions in C programming, explaining their definition, types (library and user-defined), and the process of function declaration, definition, and calling. It emphasizes the importance of functions for code modularity, reusability, and organization, while also covering concepts like passing arguments and return types. Additionally, it includes examples and exercises to reinforce understanding of function usage.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Introduction to Function

Module-3
Function
• A function is a self contained block of codes or sub programs with a
set of statements that perform some specific task or coherent task
when it is called.
• Suppose you have a task that is always performed exactly in the same
way-say a bimonthly servicing of your motorbike. When you want it
to be done, you go to the service station and say, “ it’s time, do it
now”. You don’t need to be told how the job is done. You assume the
bike would be serviced in the usual way, the mechanic does it.
• That is function is just like hiring a person to do a specific job for you.
Types of function
Any ‘C’ program contain at least one function i.e main().
There are basically two types of function those are
1. Library function
2. User defined function
1. Library function or System defined function
• System defined function can’t be modified, it can only read and can
be used. These function are supplied with every C compiler. Source
of these library function are pre complied and only object code get
used by the user by linking to the code by linker
System defined function description:
• Function definition : predefined, precompiled, stored in the library
• Function declaration : In header file with or function prototype.
• Function call : By the programmer
Steps to convert source code into executable code
2. User defined function

The user defined functions defined by the user according to its


requirement.
When user gets his own function three thing he/she has to know,
these are.
• Function declaration
• Function definition
• Function call
Example
#include<stdio.h>
void message();//function prototype declaration
void main()
{

printf("I am in main function\n");


message(); // function call
printf(“Return to main function”);
}
void message()//function definition
{
printf(“Have a Nice Day!\n");
}
Explanation
• Function prototype declaration:
void message(); -> This prototype declaration indicates that void message() is a function
which after completing its execution does not return anything is indicated by using the
keyword void.
• Function definition
void message()// -> This is the function definition which contain a block of statement of user
defined function.
• Function call
message(); -> Here the function message() being called by main() it means that the control
passes to the function message(). The activity of the main() is temporarily suspended; it falls
asleep while the message() function wakes up and goes to work. When the message() runs
out of statements to execute, the control returns to main(), which comes to life again and
begins executing its code at the exact point where it left off.
• Thus main() becomes the calling function whereas message() becomes the called function.
Question
• Name the different parts of user defined function
• The main() is known as ………………. function and user_defined
function is known as…………………… function.
Home work
• WAP to add two number using user defined function.
Class-2
• A function gets called when the function name is followed by a
semicolon. For example
void main()
{
printf("\n I am in main");
a();
}
• A function is defined when function name is followed by a pair
of braces in which one or more statements may be present.
For example
void c()
{
printf("\n I am in c");
}
Example
• Any function can be called from any other function. Even main() can be called from other functions.
• For example
#include<stdio.h>
void message();
void main()
{

message();
}
void message()
{
printf(“have a Nice Day!\n");
main();

}
• A function can be called any number of times.
• For example
#include<stdio.h>
void message();
void main()
{

printf("I am in main function\n");


message();
message();
message();
printf("Return to main function");
}
void message()
{
printf("Wish you all Happy Ram Navami!\n");

}
• A function can call itself called as recursion.
• A function can be called from another function but a function can not be defined in another function.
#include<stdio.h>
void message();
void main()
{

printf("I am in main function\n");


void message()
{
printf("Wish you all happy Ram Nabami!\n");

}
}
Why Use functions
• Writing functions avoids rewriting the same code over and over.
Suppose you have a section of code in your program that
calculates area of a triangle. If later in the program you want to
calculate the area of a different triangle, you won’t like it if you
are required to write the same instructions all over again. Instead,
you would prefer to jump to a ‘section of code’ that calculates
area and then jump back to the place from where you left off. This
section of code is nothing but a function.
Why Use functions
• Using functions it becomes easier to write programs and keep
track of what they are doing. If the operation of a program can
be divided into separate activities, and each activity placed in a
different function, then each could be written and checked
more or less independently. Separating the code into modular
functions also makes the program easier to design and
understand.
Notes
• Don’t try to cram the entire logic in one function. It is a very
bad style of programming. Instead, break a program into small
units and write functions for each of these isolated
subdivisions. Don’t hesitate to write functions that are called
only once. What is important is that these functions perform
some logically isolated task.
#include<stdio.h>
Control flow in function example
void a();
void b();
void c();
void main()
{
printf("\n I am in main");
a();
printf("\n I am finally back in main");
}
void a()
{
printf("\n I am in a");
b();
printf("\n I am back in a");
}
void b()
{
printf("\n I am in b");
c();
printf("\n I am in back b");
}
void c()
{
printf("\n I am in c");
}
Question
1. A function called itself is known as………..
Question
Main() function can be called by other function. True/False
Question
A function may be called more than once from any other
function. True/False
Question
It is necessary for a function to return some value
Passing Values between Functions
• The mechanism used to convey information to the function is
the ‘argument’ or parameter.
• In the printf( ) and scanf( ) functions; the format string and the
list of variables used inside the parentheses in these functions
are arguments.
Passing Values between Functions
1. In this program, from the function
main( ) the values of a, b are
passed on to the function swap( ),
by making a call to the function swap( )
and mentioning a and b in the parentheses:
swap( a, b) ; In the swap( )
function these values get collected in two variables
x and y: void swap( x, y ) int x, y ;
Passing Values between Functions
2. The variables a and b are called ‘actual arguments’, whereas
the variables x and y are called ‘formal arguments’. Any number
of arguments can be passed to a function being called. However,
the type, order and number of the actual and formal arguments
must always be same.
Instead of using different variable names x, y and z, we
could have used the same variable names a, b and c. But the
compiler would still treat them as different variables since they
are in different functions.
Passing Values between Functions

3. swap ( int x, int y) .This method is called ANSI method and is


more commonly used these days.
Passing Values between Functions
4. If we want that a called function should not return any value,
in that case, we must mention so by using the keyword void as
shown below.
Swapping two number using function
Function Call
• When the function get called by the calling function then that is
called, function call. The compiler executes these functions when
the semicolon is followed by the function name.
• Example:-
function(arg1,arg2,arg3);
Actual argument
• The arguments which are mentioned or used inside the function
call is knows as actual argument and these are the original
values and copy of these are actually sent to the called function.
• It can be written as constant, expression or any function call like
• Function (x);
• Function (20, 30);
• Function (a*b, c*d);
Formal Arguments

• The arguments which are mentioned in function definition are


called formal arguments or dummy arguments. These
arguments are used to just hold the copied of the values that
are sent by the calling function through the function call.
• These arguments are like other local variables which are created
when the function call starts and destroyed when the function
ends.
• Order number and type of actual arguments in the function call
should be match with the order number and type of the formal
arguments.
Difference between the formal argument and local variable :

1. The formal argument are declared inside the parenthesis


whereas the local variable declared at the beginning of the
function block.
2. The formal argument are automatically initialized when the copy
of actual arguments are passed while other local variable are
assigned values through the statements.
Return type
It is used to return value to the calling function. It can be used in two
way as:
return
Or return(expression);
Ex:- return (a);
return (a*b);
return (a*b+c);
Here the 1st return statement used to terminate the function without
returning any value.
Advantage of function
• By using function large and difficult program can be divided in to
sub programs and solved.
• When we want to perform some task repeatedly or some code is
to be used more than once at different place in the program,
then function avoids this repetition or rewritten of code over and
over.
• Due to reducing size, modular function is easy to modify and test
Notes:-
• C program is a collection of one or more function.
• A function is get called when function is followed by the
semicolon.
• A function is defined when a function name followed by a pair
of curly braces.
Question-1
When the function get called by the calling function then that is
called…………
Question-2
………………arguments are mentioned or used inside the function
call.
Question-3
The arguments which are mentioned in function definition are
called……………. Arguments.
ANY QUESTION?
THANK YOU

You might also like