KEMBAR78
Functions in C.pptx
Marathwada Mitramandal’s
COLLEGE OF ENGINEERING
Karvenagar, Pune
Accredited with ‘A++’ Grade by NAAC
“Functions In C”
By
Mrs. Ashwini Raut
Department of Computer Engineering
Session
Outline
• What is Function
• Advantages of using function
• Types of function
• Function Prototype
• Defining function
• Calling Function
• Return Statement
• Local and global Variables
• Categories Of Function
• Recursion
What is
function
• A function is a group of statements that together
perform a task.
• Every c program has at least one function called
main()
Advantages
of Using
Function
• The use of functions makes a program more readable. It's
frequently difficult to read a large program. Breaking the
code down into smaller functions keeps the program
structured, understandable, and reusable.
• The function can be reused countless times after it is
defined.
• Using a function, it is possible to reduce the size of a
program by calling and using the function at different
places in the program.
• Functions help in code modularity, which means that the
entire code is divided into separate blocks, each of which is
self-contained and performs a different task. This makes
each block implementation and debugging much easier.
Types of
function
Types of
function
1. Build in Function /Library Functions:
• These functions are grouped together and
placed in common folder called library.
• E.g: printf(),scanf()
2.User Defined Functions:
• These functions are created by user
according to programming need are called as
user defined functions.
• E.g: Function to perform addition of two int
numbers
Elements of
User Defined
Functions
1.Function Prototype
2.Function Call
3.Function Arguments and parameters
4.Function Definition
Function
Prototype
• It is defined in the beginning before the
function call is made.
• Syntax:
return_type name_of_function(list_of_arguments);
E.g: int sum(int a ,int b);
Function Call
• Function call can be made by specifying name and
list of arguments enclosed in parenthesis and
separated by comma.
• If there are no arguments empty parenthesis are
placed after function name.
• Syntax:
name_of_function(list_of_arguments);
• If function return a value then function call can be
written as :
c=sum(x,y);
Function
arguments
and
parameters
• Arguments are also called as actual parameters.
• Arguments are written within parenthesis at the time
of function call.
• Parameters are also called as Formal parameters.
• Parameters are written within parenthesis at the time
of function definition.
Function
Definition
• It is dependant on program module.
• We can write actual logic into function definition
• First line of the function is called function declaration
and rest line inside {} are called function body.
Return
statement
• It is the last statement of the function that return
certain value where the function is called.
• Syntax:
return(variable name or constant);
• E.g: return(c);
Local and
Global
Variable
• Local Variable: The variable whose scope lies inside a
function or a block in which they are declared.
• Global Variable: The variable that exists outside of all
functions. It is the variable that is visible from all other
scopes.
Categories
of Function
1.Function with arguments and with return type
#include <stdio.h>
void main()
{
int sub(int,int); //function with return value and arguments
int x=10,y=7;
int res = sub(x,y);
printf("x-y = %d",res);
}
int sub(int a,int b) //function with return value and arguments
{
return(a-b); // return value
}
Categories
of Function
2. Functions with arguments and without return values
#include <stdio.h>
int main()
{
void sum(int,int); //function with arguments and no return value
int x=10,y=7;
sum(x,y);
}
void sum(int a,int b) //function with arguments and no return value
{
int z = a+b;
printf("x + y = %d",z);
}
Categories
of Function
3. Functions without arguments and with return values
#include<stdio.h>
int main()
{
int sum();
int c = sum();
printf("Sum = %d",c);
}
int sum() //function with no arguments and return data type
{
int x=10,y=20,z=5;
printf("x = %d ; y = %d ; z = %d n",x,y,z);
int sum = x+y+z;
return(sum);
}
Categories
of Function
4. Functions without arguments and without return values
#include<stdio.h>
int main()
{
void sum();
sum();
}
void sum() //function with no arguments and return data type
{
int x=15,y=35,z=5;
printf("x = %d ; y = %d ; z = %d n",x,y,z);
int sum = x+y+z;
printf("Sum = %d",sum);
}
Call By Value
• In call by value method of parameter passing, the values
of actual parameters are copied to the function’s formal
parameters.
• There are two copies of parameters stored in different
memory locations.
• One is the original copy and the other is the function
copy.
• Any changes made inside functions are not reflected in
the actual parameters of the caller.
Call By
Reference
• In call by reference method of parameter passing, the
address of the actual parameters is passed to the
function as the formal parameters.
• Both the actual and formal parameters refer to the same
locations.
• Any changes made inside the function are actually
reflected in the actual parameters of the caller.
Recursion in C
Recursion is the technique of
making a function call itself.
#include<stdio.h>
int sum(int k);
int main() {
int result = sum(10);
printf("%d", result);
return 0;
}
int sum(int k) {
if (k > 0) {
return k + sum(k - 1);
} else {
return 0;
}}
Thank You
Any Questions?

Functions in C.pptx

  • 1.
    Marathwada Mitramandal’s COLLEGE OFENGINEERING Karvenagar, Pune Accredited with ‘A++’ Grade by NAAC “Functions In C” By Mrs. Ashwini Raut Department of Computer Engineering
  • 2.
    Session Outline • What isFunction • Advantages of using function • Types of function • Function Prototype • Defining function • Calling Function • Return Statement • Local and global Variables • Categories Of Function • Recursion
  • 3.
    What is function • Afunction is a group of statements that together perform a task. • Every c program has at least one function called main()
  • 4.
    Advantages of Using Function • Theuse of functions makes a program more readable. It's frequently difficult to read a large program. Breaking the code down into smaller functions keeps the program structured, understandable, and reusable. • The function can be reused countless times after it is defined. • Using a function, it is possible to reduce the size of a program by calling and using the function at different places in the program. • Functions help in code modularity, which means that the entire code is divided into separate blocks, each of which is self-contained and performs a different task. This makes each block implementation and debugging much easier.
  • 5.
  • 6.
    Types of function 1. Buildin Function /Library Functions: • These functions are grouped together and placed in common folder called library. • E.g: printf(),scanf() 2.User Defined Functions: • These functions are created by user according to programming need are called as user defined functions. • E.g: Function to perform addition of two int numbers
  • 7.
    Elements of User Defined Functions 1.FunctionPrototype 2.Function Call 3.Function Arguments and parameters 4.Function Definition
  • 8.
    Function Prototype • It isdefined in the beginning before the function call is made. • Syntax: return_type name_of_function(list_of_arguments); E.g: int sum(int a ,int b);
  • 9.
    Function Call • Functioncall can be made by specifying name and list of arguments enclosed in parenthesis and separated by comma. • If there are no arguments empty parenthesis are placed after function name. • Syntax: name_of_function(list_of_arguments); • If function return a value then function call can be written as : c=sum(x,y);
  • 10.
    Function arguments and parameters • Arguments arealso called as actual parameters. • Arguments are written within parenthesis at the time of function call. • Parameters are also called as Formal parameters. • Parameters are written within parenthesis at the time of function definition.
  • 11.
    Function Definition • It isdependant on program module. • We can write actual logic into function definition • First line of the function is called function declaration and rest line inside {} are called function body.
  • 12.
    Return statement • It isthe last statement of the function that return certain value where the function is called. • Syntax: return(variable name or constant); • E.g: return(c);
  • 15.
    Local and Global Variable • LocalVariable: The variable whose scope lies inside a function or a block in which they are declared. • Global Variable: The variable that exists outside of all functions. It is the variable that is visible from all other scopes.
  • 17.
    Categories of Function 1.Function witharguments and with return type #include <stdio.h> void main() { int sub(int,int); //function with return value and arguments int x=10,y=7; int res = sub(x,y); printf("x-y = %d",res); } int sub(int a,int b) //function with return value and arguments { return(a-b); // return value }
  • 18.
    Categories of Function 2. Functionswith arguments and without return values #include <stdio.h> int main() { void sum(int,int); //function with arguments and no return value int x=10,y=7; sum(x,y); } void sum(int a,int b) //function with arguments and no return value { int z = a+b; printf("x + y = %d",z); }
  • 19.
    Categories of Function 3. Functionswithout arguments and with return values #include<stdio.h> int main() { int sum(); int c = sum(); printf("Sum = %d",c); } int sum() //function with no arguments and return data type { int x=10,y=20,z=5; printf("x = %d ; y = %d ; z = %d n",x,y,z); int sum = x+y+z; return(sum); }
  • 20.
    Categories of Function 4. Functionswithout arguments and without return values #include<stdio.h> int main() { void sum(); sum(); } void sum() //function with no arguments and return data type { int x=15,y=35,z=5; printf("x = %d ; y = %d ; z = %d n",x,y,z); int sum = x+y+z; printf("Sum = %d",sum); }
  • 21.
    Call By Value •In call by value method of parameter passing, the values of actual parameters are copied to the function’s formal parameters. • There are two copies of parameters stored in different memory locations. • One is the original copy and the other is the function copy. • Any changes made inside functions are not reflected in the actual parameters of the caller.
  • 22.
    Call By Reference • Incall by reference method of parameter passing, the address of the actual parameters is passed to the function as the formal parameters. • Both the actual and formal parameters refer to the same locations. • Any changes made inside the function are actually reflected in the actual parameters of the caller.
  • 23.
    Recursion in C Recursionis the technique of making a function call itself. #include<stdio.h> int sum(int k); int main() { int result = sum(10); printf("%d", result); return 0; } int sum(int k) { if (k > 0) { return k + sum(k - 1); } else { return 0; }}
  • 24.