0% found this document useful (0 votes)
67 views12 pages

Functions Mode For Bahria University Presentation

Functions in C allow programmers to group statements together to perform a specific task. A function receives parameters, performs some computation, and returns a value. Functions make code reusable, readable, and maintainable by breaking programs into smaller sub-units. Key aspects of functions include function prototypes, definitions, calls, return types, and passing arguments. Functions help programmers apply the principles of modularity and abstraction.

Uploaded by

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

Functions Mode For Bahria University Presentation

Functions in C allow programmers to group statements together to perform a specific task. A function receives parameters, performs some computation, and returns a value. Functions make code reusable, readable, and maintainable by breaking programs into smaller sub-units. Key aspects of functions include function prototypes, definitions, calls, return types, and passing arguments. Functions help programmers apply the principles of modularity and abstraction.

Uploaded by

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

FUNCTIONS

in C Language

Nauman Ahmad Tariq


Functions
 The function is one of the most important things to
understand in C programming.

 Groups a number of program statements into a unit and


gives it a name. This unit can then be invoked(called)
from other parts of the program.

 A function is a sub-unit of a program which performs a


specific task.

 Functions are modules in c.

 Functions take arguments(constants values or variables)


and may return a value.
// Addition of two integers without
Function
#include<stdio.h>
intmain()
{
intnum1, num2, add_result;
printf("Enter first number\n");
scanf("%d", &num1);
printf("Enter second number\n");
scanf("%d", &num2);
add_result= num1 + num2;
printf("Addition result is %d\n",add_result);
getchar();
return 0;
}
// Addition of two integers using
Function
#include<stdio.h> int addnumbers(intx, inty)
Int addnumbers(intx, inty); {
Int main() intz;
{ z = x +y;
int num1, num2, add_result; return z;
printf("Enter first number\n"); }
scanf("%d", &num1);
printf("Enter second number\n");
scanf("%d", &num2);
add_result= addnumbers(num1,
num2);
printf("Addition result is
%d\n",add_result);
getchar(); return 0;
}
Benefits of Function
 Divide and Conquer
 Construct a program from smaller pieces or components
 Modularize a program (These smaller pieces are called
modules)
 Each piece more manageable than the original program
 Manageable program development
 Software reusability
 Use existing functions as building blocks for new programs
 Abstraction -hide internal details (library functions)
 Avoid code repetition
Functions
 A function receives zero or more parameters,
performs a specific task, and returns none or one
value.

 A function is invoked by its name and parameters.


 No two functions have the same name in your C program.
 The communication between the function and invoker is
through the parameters and the return value.

 Functions make programs reusable and readable.


Function Terminologies
 Function prototype

 Function definition
 Function header

 Calling a function

 Passing arguments

 Returning a value
Function Prototype
#include<stdio.h> int addnumbers(intx, inty)
Int addnumbers(intx, inty); {
Int main() intz;
{ z = x +y;
int num1, num2, add_result; return z;
printf("Enter first number\n"); }
scanf("%d", &num1);
printf("Enter second number\n");
scanf("%d", &num2);
add_result= addnumbers(num1,
num2);
printf("Addition result is
%d\n",add_result);
getchar(); return 0;
}
Function Definition
#include<stdio.h> int addnumbers(intx, inty)
Int addnumbers(intx, inty); {
Int main() intz;
{ z = x +y;
int num1, num2, add_result; return z;
printf("Enter first number\n"); }
scanf("%d", &num1);
printf("Enter second number\n");
scanf("%d", &num2);
add_result= addnumbers(num1,
num2);
printf("Addition result is
%d\n",add_result);
getchar(); return 0;
}
Function Call
#include<stdio.h> int addnumbers(intx, inty)
Int addnumbers(intx, inty); {
Int main() intz;
{ z = x +y;
int num1, num2, add_result; return z;
printf("Enter first number\n"); }
scanf("%d", &num1);
printf("Enter second number\n");
scanf("%d", &num2);
add_result= addnumbers(num1,
num2);
printf("Addition result is
%d\n",add_result);
getchar(); return 0;
}
Return Type
#include<stdio.h> int addnumbers(intx, inty)
Int addnumbers(intx, inty); {
Int main() intz;
{ z = x +y;
int num1, num2, add_result; return z;
printf("Enter first number\n"); }
scanf("%d", &num1);
printf("Enter second number\n");
scanf("%d", &num2);
add_result= addnumbers(num1,
num2);
printf("Addition result is
%d\n",add_result);
getchar(); return 0;
}
THANKS!

You might also like