Lecture 8
Lecture 8
Applications
for Engineers
Functions in C
I
• A C program is made of one or more functions, one and only one of which
•
must be named main.
amarinist
In general, the purpose of a function is to receive zero or more pieces of data,
operate on them, and return at most one piece of data.
• There are basically two types of function those are
1. Library function
2. User defined function
2
Why functions?
• Break longer jobs rinto conceptually smaller jobs which are precisely defined.
4
Structure Chart for a C Program
5
User-Defined Functions
6
Function Definitions,
Declarations, and Calls
#include <stdio.h>
void prn_message(void); /* function declaration */
/* tells the compiler that this */
/* function takes no arguments */
/* and returns no value. */
int main(void)
{
prn_message(); /* function Call */
}
void prn_message(void) /* function definition */
{
printf(“A message for you: “);
printf(“Have a nice day!\n”);
}
Form of a Function Definition
type function_name ( parameter type list )
{
declarations
statements
}
Some Terminology
Header: Everything before the first brace.
Body: Everything between the braces.
Type: Type of the value returned by the function.
Parameter List: A list of identifiers that provide information
for use within the body of the function.
Also called formal parameters.
Function definition:-
• Function definition consists of the whole description and
code of the function.
• It tells about what function is doing what are its inputs and
what are its out put
• It consists of two parts function header and function body
expla
decurion
need netword msn.unmiymeint.int
return;
2
return expression;
It is used to return value to the calling function. It can be used in two way as
return
Or return(expression);
Ex:-
return (a);
return (a*b);
return (a*b+c);
Here the 1st return statement used to terminate the function without returning any
value
diferion of fun
calling fun
If There is No return
• Control is passed back to the calling environment when the closing
brace of the body is encountered.
Eample:-
/*summation of two values*/
int sum (int a1, int a2);
main( )
int a,b;
printf(“enter two no”);
scanf(“%d%d”,&a,&b);
int s=sum(a,b);
calling
printf(“summation is = %d”,s);
}
• The number and type of arguments that are to be passed to the function.
00
void f(char, int);
The Keyword void
• void is used if:
• A function takes no arguments.
• If no value is returned by the function.
Function Call
• As we have seen, a function is invoked (or called) by writing its name and an
appropriate list of arguments within parentheses.
• The arguments must match in number and type the parameters in the parameter
list of the function definition.
e
Input arguments
• This is the arguments which are mentioned or used inside the function call.
• It can be written as constant, expression or any function call like
Function (x);
Function (20, 30);
Function (a*b, c*d);
Function(2,3,sum(a, b));
Sample Program with Subfunction
21
Sample Program with Subfunction
17,21 7 21
17 21
17821 is 357
22
Sample Program with Subfunction
23
Function Definition
24
void Function with a Parameter
25
void Function with a Parameter
26
void Function with a Parameter
27
Non-void Function without Parameters
28
Calling a Function That Returns a Value
29
Read a Number and Square It
30
Read a Number and Square It
31
Read a Number and Square It
32
Read a Number and Square It
33