KEMBAR78
functions in C | PPTX
M E H W I S H M E H M O O D
• A function is a group of statements that together perform
a task. Every C program has at least one function, which
is main(), and all the most trivial programs can define
additional functions.
• You can divide up your code into separate functions.
How you divide up your code among different functions
is up to you, but logically the division usually is so each
function performs a specific task.
A function in C can have a value, a side
effect, or both
• The side effect occurs before the value is returned.
• The function’s value is the value of the expression in the
return statement.
• A function can be called for its value, its side effect, or both.
• The general syntax of a function in C is :
• The type_specifier specifies the data type of the value, which
the function will return.
• A valid function name is to be assigned to identify the
function
• Arguments appearing in parentheses are also termed as
formal parameters.
//Prototype declaration
void greeting();
int main()
{
greeting();
return 0;
} // main
Void greeting()
{
printf(“HELLO WORLD”);
return ;
} //greeting()
Declaration
is coded
first
Definition is
after the call
Call is in the
statement
sections
The type of the expression in the return
statement must match the return type in the
function header.
. Only one value can be returned by a
function
• Formal parameters are variables that are declared in the header of
the function definition.
• Actual parameters are the expressions in the calling statement.
• The formal and actual parameters must match exactly in type, order,
and number. Their names, however, do not need to be the same.
int square(int x);
Int main()
{
Int I
For(i=1;i<10; i++)
{
printf(“n square of %d is %d:”, i, square(i));
}
int square(int x)
{
int j;
j=x*x;
return(j);
}
Actual
argument
Formal
argument
• Scope Rules - Rules that govern whether one piece of
code knows about or has access to another piece of
code or data
• The code within a function is private or local to that
function
• Two functions have different scopes
• One function cannot be defined within another function
 Call by value
 Call by reference
 When arguments are
passed to the called
function, the values
are passed through
temporary variables
• In call by
reference, the
function is
allowed access
to the actual
memory
location of the
argument and
therefore can
change the
value of the
arguments of
the calling
routine
main()
{
.
.
palindrome();
.
.
}
palindrome()
{
.
.
getstr();
reverse();
cmp();
.
.
}
• A pointer variable can be passed as a parameter to a function
either by value or by reference.
• In the function pointer Parameters, both p and q are pointers.
The parameter p is a reference parameter; the parameter q is
a value parameter.
• Furthermore, the function pointer Parameters can change the
value of *q, but not the value of q. However, the function
pointer Parameters can change the value of both p and *p.
functions in C
functions in C

functions in C

  • 1.
    M E HW I S H M E H M O O D
  • 2.
    • A functionis a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions. • You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division usually is so each function performs a specific task.
  • 3.
    A function inC can have a value, a side effect, or both • The side effect occurs before the value is returned. • The function’s value is the value of the expression in the return statement. • A function can be called for its value, its side effect, or both.
  • 5.
    • The generalsyntax of a function in C is : • The type_specifier specifies the data type of the value, which the function will return. • A valid function name is to be assigned to identify the function • Arguments appearing in parentheses are also termed as formal parameters.
  • 6.
    //Prototype declaration void greeting(); intmain() { greeting(); return 0; } // main Void greeting() { printf(“HELLO WORLD”); return ; } //greeting() Declaration is coded first Definition is after the call Call is in the statement sections
  • 8.
    The type ofthe expression in the return statement must match the return type in the function header. . Only one value can be returned by a function
  • 9.
    • Formal parametersare variables that are declared in the header of the function definition. • Actual parameters are the expressions in the calling statement. • The formal and actual parameters must match exactly in type, order, and number. Their names, however, do not need to be the same.
  • 10.
    int square(int x); Intmain() { Int I For(i=1;i<10; i++) { printf(“n square of %d is %d:”, i, square(i)); } int square(int x) { int j; j=x*x; return(j); } Actual argument Formal argument
  • 11.
    • Scope Rules- Rules that govern whether one piece of code knows about or has access to another piece of code or data • The code within a function is private or local to that function • Two functions have different scopes • One function cannot be defined within another function
  • 12.
     Call byvalue  Call by reference
  • 13.
     When argumentsare passed to the called function, the values are passed through temporary variables
  • 14.
    • In callby reference, the function is allowed access to the actual memory location of the argument and therefore can change the value of the arguments of the calling routine
  • 16.
  • 18.
    • A pointervariable can be passed as a parameter to a function either by value or by reference. • In the function pointer Parameters, both p and q are pointers. The parameter p is a reference parameter; the parameter q is a value parameter. • Furthermore, the function pointer Parameters can change the value of *q, but not the value of q. However, the function pointer Parameters can change the value of both p and *p.