Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

5/24/2023

Introduction
• Most computer programs that solve
real-world problems are much larger

C Functions than the programs presented earlier.


• Experience has shown that the best way
to develop and maintain a large
program is to construct it from smaller
pieces or modules, each of which is
more manageable than the original
program.
© G.Y.Namani C Functions Slide 1 © G.Y.Namani C Functions Slide 2

Introduction cont… Introduction cont…


• This technique is called divide and conquer. • Function is a block of code that
• This technique of the C language facilitates performs a specific task.
the design, implementation, operation, and • C Standard Library provides built-in
maintenance of large programs.
functions.
• Modules in C are called functions.
• You can write functions to define
• C programs are typically written by
combining new functions you write with specific tasks that may be used at
built-in functions available in the C Standard many points in a program, called
Library. programmer-defined functions.
© G.Y.Namani C Functions Slide 3 © G.Y.Namani C Functions Slide 4

Function prototype Function prototype cont…


• Function prototype refers to function • When it is said that the function
declaration. returns a value, it means that the
function can be used in the same
• It contains the function’s signature. manner as a variable would be.
• It tells the compiler what the function • For example, a variable can be set
equal to a function that returns any
will return, what function will be called, value.
as well as what arguments of the • E.g., int sum = add(a,b);
function can be passed.

© G.Y.Namani C Functions Slide 5 © G.Y.Namani C Functions Slide 6

1
5/24/2023

Function prototype cont… Function definition


Syntax of the C function prototype: • The tasks that a particular function is
• return-type function_name ( expected to perform are implemented
param_type1 param1, param_type2 in function definition.
param2, ... , param_typeN paramN ); • The actual statements defining the
• Each parameter should be written function are written only once, and the
separately even if there are some of statements are hidden from other
the same data type. functions.

© G.Y.Namani C Functions Slide 7 © G.Y.Namani C Functions Slide 8

Function definition cont… Function definition cont…


• When the programmer actually defines Syntax of function definition:
the function, it will begin with the • return-type function_name (
prototype, minus the semi-colon. param_type1 param1, param_type2
• Then there should always be a block param2, ... , param_typeN paramN ){
(surrounded by curly braces {}) with //local variable declarations
the code that the function is to //statements
execute, just as you would write it for //return expression if any
the main function.
}
© G.Y.Namani C Functions Slide 9 © G.Y.Namani C Functions Slide 10

Function definition cont… Function calling


• param_type just means the type for • Functions are invoked by a function call
each parameter – for instance, an int. • A function call specifies the function name
• There can be more than one and provides information (as arguments)
parameter passed to a function or that the function needs to perform its
none (empty parentheses) at all, and it
does not have to return a value. designated task.
• Functions that do not return values • Syntax for function call:
have a return type of void. function_name ( argument1, argument2, ... ,
argumentN );

© G.Y.Namani C Functions Slide 11 © G.Y.Namani C Functions Slide 12

2
5/24/2023

Example Example cont…


/*program demonstrates function
printf( "Please input the second
prototype, definition and call*/
number: " );
#include <stdio.h>
scanf( "%d", &y );//function call
//function prototype
int mult ( int x, int y ); printf( "The product of your two
int main() {
numbers is %d\n", mult( x, y ) );
int x,y; return 0;
printf( "Please input the first number: }
" );
scanf( "%d", &x );
© G.Y.Namani C Functions Slide 13 © G.Y.Namani C Functions Slide 14

Advantages of using functions


Example cont… in programs
//function definition • Testing and correcting errors is easy
int mult (int x, int y) { because errors are localized and
corrected.
//local variable
• We can understand the flow of
int product=x*y; program, and its code easily since the
return product; readability is enhanced while using the
functions
}
© G.Y.Namani C Functions Slide 15 © G.Y.Namani C Functions Slide 16

Advantages of using functions Advantages of using functions


in programs cont… in programs cont…
• The divide-and-conquer approach • Avoid repeating code in a program.
makes program development more • Packaging code as a function allows
manageable. the code to be executed from other
• Another motivation is software locations in a program simply by calling
reusability – using existing functions as the function.
building blocks to create new
programs.

© G.Y.Namani C Functions Slide 17 © G.Y.Namani C Functions Slide 18

3
5/24/2023

Thank you for listening!

© G.Y.Namani C Functions Slide 19

You might also like