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

Lecture 8

Uploaded by

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

Lecture 8

Uploaded by

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

Programming

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.

• C program generally consists of many small functions.


o
• A piece of a code which repeats at many places can be written only once and
used again and again.

• Debugging and maintenance is easy.


Note
In C, a program is made of one or more functions, one and
only one of which must be called main.
0
The execution of the program always starts
with main, but it can call other functions
e
to do some part of the job.

4
Structure Chart for a C Program
5
User-Defined Functions

Like every other object in C, functions must be both declared and


defined. The function declaration gives the whole picture of the function
that needs to be defined later. The function definition contains the code
for a function.

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

The return Statement 2 sumname


a

• When a return statement is executed, program control is immediately passed back


to the calling environment.

• If an expression follows the keyword return, the value of the expression is


AA
returned to the calling environment as well.

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);
}

int sum(int x1,int y1)


{
int z=x1+y1;
Return z;
}
Function Declarations
A function prototype tells the compiler:

• The number and type of arguments that are to be passed to the function.

• The type of the value that is to be returned by the function.

General Form of a Function Declaration

type function_name( parameter type list);


Examples of Function Declarations
double sqrt(double);

• The parameter list is typically a comma-separated list of types.


Identifiers are optional.
void f(char c, int i);
is equivalent to

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

You might also like