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

Function 6

A function is a named block of code that performs specific tasks and can be reused throughout a program, enhancing modularity and maintainability. Functions can be user-defined or built-in, and they consist of declarations, definitions, and calls, with parameters passed by value. The document also covers the importance, advantages, and various aspects of functions in programming, including scope, returning values, and examples of usage.

Uploaded by

haroonsadiq331
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Function 6

A function is a named block of code that performs specific tasks and can be reused throughout a program, enhancing modularity and maintainability. Functions can be user-defined or built-in, and they consist of declarations, definitions, and calls, with parameters passed by value. The document also covers the importance, advantages, and various aspects of functions in programming, including scope, returning values, and examples of usage.

Uploaded by

haroonsadiq331
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Function

Function
 A function is a named block of code that
performs some action or specific task .the
statement written in a function are executed
when it is called by its name.
 A function is a group of statements that is
executed when it is called from some point of
the program. Each function has a unique
name. functions are the building blocks of c
programs that encapsulate piece of code to
perform specified operations .
 The functions are used to accomplish the
similar kinds of task again and again without
writing the same code again.
The function provide a structured
programming approach .it is modular way
of writing programs .the whole program
logic is divided into a number of smaller
modules or functions.
The format of function
 type name ( parameter1, parameter2, ...)
 { statements }
 where:
 · type is the data type specifier of the data returned by
the function.
· name is the identifier by which it will be possible to
call the function.
 · parameters (as many as needed): Each parameter
consists of a data type specifier followed by an
 identifier, like any regular variable declaration (for
example: int x) and which acts within the function as a
regular local variable. They allow to pass arguments to
the function when it is called. The different parameters
are separated by commas.
 · statements is the function's body. It is a block of
statements surrounded by braces { }.
Importance of function
A program may need to repeat the same
piece of code at various places. It may be
required to perform certain tasks
repeatedly. the program may become very
large if function are not used .the piece of
code that is executed repeatedly is stored
in a separate function. the real reason of
using functions is to divide the program
into different parts. these parts of a
program can be managed easily.
Advantage of function
1.Easier to code: a lengthy program can
be divided into small functions it is
easier to write small functions instead of
writing on long program .a function is
written to solve a particular problem.
2.Easir to modify: each function has a
unique name and is written as an
independent block . If there is any error
in the program the change is made to
particular function in which error exists.
A small function is easier to modify than
a large program.
3.Reusability : the code written in functions can
be reused as and when required.
4. a program may consist of many functions
these functions are written as independent
programs. different programmers can work on
different functions at the same time it takes
far less time to complete the program.
Types of functions
User defined functions
A type of function written by programmer
is known as user defined function.
Built-in functions
A type of function that is available as a
part of language is known as built-in
function or library function . These
functions are ready made programs and
these functions are stored in different
header files .built-in function make
program easer and faster to write.
User defined function
A user defined function consists of the
followings
Function declaration
Function definition
Function declaration or function prototype

 Function declaration is a model of a function it


is also called function prototype . It provides
information to compiler about the structure of
the function to be used in program .
 It ends with a ; and are written at the
beginning of the source file just before the
main function. Function declaration consists of
the following parts
 Function name
 Function return type
 Number and type of parameters
Syntax
Return type func-name (parameters);
Function declaration
#include<iostream.h>
Void show ( void);
Int sum(int a,int b);
Void main()
{
Statements;
}
Return type : it indicates the type of value
that will be returned by function .if the
function return no value the keyword void
is used.
Function name: it indicates the name of
function that is to be created.
Parameters: parameters are the values
that are provided to a function when the
function is called. parameters are given in
a parenthesis . If there are many
parameters these are separated by
commas. if there is no parameters empty
parenthesis are used are void key word is
The parameters are given in two way
Only data type of the parameters are
written in prototype as
int add( int ,int )
Both data type and names of the
parameters are written in prototype as
int add (int a, int b)
Void show(void)
Int cube(int)
Int add (int a,int b,int c)
Void line(char)
Function definition
A set of statements that explains what a
function does is called function definition
The function definition can be written at the
following places
-before main function
-after main function
Function declaration is not required if the
function is written before main function.
function declaration is compulsory if the
function definition is after main function.
The function definition consists of two types
Function header
Function body
Return type function name(parameters)
{
Statemenst;
}
Function defination
void show()
{
Printf(“example of function”);
}

Int sum(int x,int y)


{
Return x+y;
}
Function call
The statements that activates a function
is known as function call .a function can
be called with its name. the following
steps take place when a function is
called
The control moves to the function that is
called
All statements in the function body are
executed
The control return back to the calling
function
Void main()
{ Void func()
{
Func()
}

}
Scope of function
The area in which a function can be
accessed is known as the scope of a
function.
Global function
A type of function that is declared outside
of any function is called global function.
Which can be
Called from any part of program.
Local function
A type of function that is declared within
another function is called local function.
A local function can be called within the
function.
 C program is a collection of one or more functions.
 A function gets called when the function name is followed by a
semicolon. For example,
main( )
{
argentina( ) ;
}
 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,
argentina( )
{
statement 1 ;
statement 2 ;
statement 3 ;
}
A function can be called any number of times.
For example,
main( )
{
message( ) ;
message( ) ;
}
message( )
{
printf ( "\nJewel Thief!!" ) ;
}
 The order in which the functions are defined in a program and the order in which
they get called need not necessarily be same. For example,
main( )
{
message1( ) ;
message2( ) ;
}
message2( )
{
printf ( "\nBut the butter was bitter" ) ;
}
message1( )
{
printf ( "\nMary bought some butter" ) ;
}
 Here, even though message1( ) is getting called before message2( ), still,
message1( ) has been defined after message2( ). However, it is advisable to
define the functions in the same order in which they are called. This makes the
program easier to understand.
 A function can be called from other function, but a
function cannot be defined in another function. Thus,
the following program code would be wrong, since
argentina( ) is being defined inside another function,
main( ).
main( )
{
printf ( "\nI am in main" ) ;
argentina( )
{
printf ( "\nI am in argentina" ) ;
}
}
#include <stdio.h>
#include<conio.h>
void show(void);
void display(void);
main()
{
show();
display();
show();
}
void show(void)
{ ();
printf("This is show function \n");
display();
}
void display()
{
printf("This is diplay function \n");
}
Passing parameters to
function
 Parameters are the values that are provided to
a function when the function is
called .parameter are given in the parenthesis.
 The sequence and type of parameter in
function call must be same as the sequence
and type of parameters in function
declaration .
 Parameters in function call are called actual
parameter
 Parameters in called function are called formal
parameters.
When a function call is executed the value of
actual parameters are copied to formal
parameters.
Pass by value
A parameter passing mechanism in which
the value of actual parameters is copied to
formal parameters of called function is
known as pass by value. If the function
makes any changes in formal parameter it
does not affect the value of actual
parameters .it is the default mechanism for
passing parameters to functions.
#include<stdio.h>
#include<conio.h>
void show();
void main()
{
Printf(“the calling of function show \n“);
show();
getch();
}

void show()
{
Printf(“this is show function”);
}
example
#include <stdio.h>
void sum();
void add(int,char,int);
main()
{

int a,b;
char opr;
printf(" enter no opr and no");
scanf("%d %c %d",&a,&opr,&b);
sum();
add(a,opr,b);
add(12,'-',2);
}
Cont…
sum()
{
int a,b,res;
printf("Enter two nos");
scanf("%d %d",&a,&b);
res=a+b;
printf("res=%d",res);
add(res,'-',30);
}

void add(int x, char y, int z)


{
if(y=='+')
printf("x+z=%d",x+z);
else if (y=='-')
printf("X-z=%d",x-z);
else
printf("invalid operator");
}
#include<stdio.h>
#include<conio.h>

void max (int a,int b);


int main()
{
int x,y;
printf("enter two number");
scanf("%d %d",&x,&y);
max(x,y);

return 0;
}

void max(int a,int b)


{
if(a>b)
printf("a is greater then b");
else
printf(" b is greater then a");
}

This example explain that we input two number in main function pass these
number to a function and the function display the maximum number .
Write a program that inputs a number in main function and passes the number to a
function the function displays table of that number
#include<stdio.h>
#include<conio.h>

void table(int n);


int main()
{
int no;
printf("enter no to find out its table");
scanf("%d",&no);

table(no);
return 0;
}

void table(int n)
{
int a;
for( a=1;a<=10;a++)

printf("%d * %d=%d \n",a,n,a*n);


}
Write a program that inputs a number and displays its processor and successor number
using function
#include<stdio.h>
#include<conio.h>

void number(int );
int main()
{
int num;
printf("Enter any no");
scanf("%d",&num);

number(num);
}

void number(int a)
{
int p,n;
p=a-1;
n=a+1;

printf("The number before %d is %d \n",a,p);

printf("The number after %d is %d \n",a,n);


}
Returning Values from Functions

When a function completes its execution, it


can return a single value to the calling
program.
Usually this return value consists of an
answer to the problem the function has
solved. The
#include<stdio.h>
/* function declaration */
int addition();
int main()
{
/* local variable definition */
int answer;
/* calling a function to get addition value */
answer = addition();
printf("The addition of two numbers is: %d\n",answer);
return 0;
}
/* function returning the addition of two numbers */
int addition()
{
/* local variable definition */
int num1 = 10, num2 = 5;
return num1+num2;
}
#include <stdio.h>

int adder(int x, int y)


{
return x + y;
}

int main()
{
int value1 = 5, value2 = 10;

printf("%i + %i = %i", value1, value2,


adder(value1, value2));

return 0;
}

You might also like