1. C ProgrammingLanguage WorkingwithFunctions1
Agenda:
What is a Functions?
A function is a named, independent section of C code that performs a specific task and optionally returns a value
to the callingprogram
A function is named. Each function has a unique name. By usingthat name in another partof the program, you
can execute the statements contained in the function.This is known as callingthefunction. A function can be
called fromwithin another function.
A function is independent. A function can perform its task without interference from or interferingwith other
parts of the program.
A function performs a specific task. This is the easy partof the definition.A task is a discretejob that your
program must perform as partof its overall operation,such as sendinga lineof text to a printer, sortingan array
into numerical order,or calculatinga cuberoot.
function can return a value to the calling program. When your program callsa function,the statements it
contains areexecuted. If you want them to, these statements can pass information back to the callingprogram.
Benefits of the functions
program
Function Terminology
Function prototype: A function's prototype contains the name of the function,return type, a lis tof variables that
must be passed to it, and the type of variableitreturns, if any..
Function Definition: The function itself is called the function definition.The definition contains the code that will
be executed.
C ProgrammingLanguage WorkingwithFunctions2
2. Function header: The function header is atthe startof a function,and itgives the function's name. The header
also gives the function's return type and describes its arguments.Note that the function header is identical to the
function prototype (minus the semicolon).
Function Body: The statements written within function arecalled function body. Statements are enclosed in
braces.These statements are executed when the function is called.
Arguments: When a function is called,the program can send the function information in the form of one or more
arguments. An argument is programdata needed by the function to perform its task.Arguments are enclosed in
parentheses followingthe function's name.
Return statement: If the function return type is anythingother than void, a return statement should be included,
returning a valuematching the return type.
/* Demonstrates a simple function */
#include<stdio.h>
longcube(long x);
longinput, answer;
main()
{
printf("Enter an integer value: ");
scanf("%d", &input);
answer = cube(input); /*here input is actual argument.
/* Note: %ld is the conversion specifier for */
/* a long integer */
printf("nThe cube of %ld is %ld.n", input, answer);
return 0;
}
/* Function: cube() - Calculates thecubed valueof a variable*/
longcube(long x) /*here x is formal argument*/
{
longx_cubed;
x_cubed = x * x * x;
return x_cubed;
}
How does a Function Works
program.
C ProgrammingLanguage WorkingwithFunctions3
3. m of one or more arguments.
An argument is programdata needed by the function to perform its task.The statements in the function then
execute, performing whatever task each was designed to do.
sses back to the same location in the program that
called the function. Functions can send information back to the program in the form of a return value.
xecution passes
back to the placefrom which the function was called.A function can be called as many times as needed, and
functions can be called in any order.
#include<stdio.h>
void printline(char ch,intn);
int add(inta,intb,int c);
main()
{
int result;
printline('#',70);
result=add(10,3,4);
printf("n resultis %d",result);
getch();
return 0;
}
void printline(char ch,intn)
{
int i;
for(i=0;i<n;i++)
printf("%c",ch);
}
int add(inta,intb,int c)
{ C ProgrammingLanguage WorkingwithFunctions4
4. return a+b+c;
} /* Program to check the given value is vowel */
#include<stdio.h>
void main()
{
char ch;
printf("nEnter any character …');
ch=getch();
if(checkvowel(ch)==1)
printf("n It is a vowel");
else
printf("n try again");
}
int checkvowel(char ch)
{
if(ch=='a'||ch=='c'||ch=='i'||ch=='o'||ch=='u'||ch='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
return (1);
else
return (0);
}