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

Functions Introduction in C

functions introduction in c language
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Functions Introduction in C

functions introduction in c language
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

CSE101 – R01

Problem Solving &


Programming in C
Unit II

23-11-2020 Ref: Programming with C, Byron S. Gottfried.


Functions

23-11-2020 Ref: Programming with C, Byron S. Gottfried. 2


Introduction
• Types of Function
• Library Function
• User-defined Function
• User-defined function
• Programmers can define their own functions for carrying out various
individual tasks
• Allows a large program to be broken down into a number of smaller, self-
contained components
 Each of which has some unique, identifiable purpose
• C program can be modularized through the intelligent use of such functions
23-11-2020 Ref: Programming with C, Byron S. Gottfried. 3
Advantages of function
• Repeated instructions can be placed within a single function
• Different set of data can be transferred to the function each time it is
accessed
• The use of a function avoids the need for redundant (repeated) programming
of the same instructions
• logical clarity resulting from the decomposition of a program into
several concise functions
• programs are easier to write and easier to debug
• enables a programmer to build a customized library of frequently
used routines

23-11-2020 Ref: Programming with C, Byron S. Gottfried. 4


Functions overview
• A function is a self-contained program segment that carries out some
specific, well-defined task
• Every C program consists of one or more functions
• One of these functions must be called main
• Execution of the program will always begin by carrying out the instructions in
main
• Additional functions will be subordinate to main, and perhaps to one
another

23-11-2020 Ref: Programming with C, Byron S. Gottfried. 5


Function Overview
• Definition of functions can appear in any order
• One function cannot embed another function
• function will carry out its intended action whenever it is accessed /
called
• Once accessed, control will be returned to the point from which the
function was accessed

23-11-2020 Ref: Programming with C, Byron S. Gottfried. 6


Function Overview
• Generally, function will process information that is passed to it from
the calling portion of the program, and return a single value
• Information is passed to the function via special identifiers called
arguments/parameters
• returned via the return statement

23-11-2020 Ref: Programming with C, Byron S. Gottfried. 7


Example
#include <stdio.h>
char lower_to_upper(char c 1){
char c2;
c2 = ( c1 >= 'a' && c1 <= ‘z' ) ? ( 'A' + c1 - 'a' ) : c1 ;
return(c2);
}
main(){
char lower, upper;
printf("P1ease enter a lowercase character: " ) ;
scanf( “%c" &lower) ;
upper = lower_to_upper(lower) ;
printf ( \nThe uppercase equivalent is %c", upper) ;
}

23-11-2020 Ref: Programming with C, Byron S. Gottfried. 8


Defining Function
• has two principal components:
• the first line (including the argument declarations)
• the body of the function. (Normally a compound Statement)
• The first line
• contains the type specification of the value returned by the function,
• followed by the function name,
• a set of arguments (optional), separated by commas and enclosed in
parentheses
• Each argument is preceded by its associated type declaration
• empty pair of parentheses must follow the function name if the function
definition does not include any arguments
23-11-2020 Ref: Programming with C, Byron S. Gottfried. 9
General syntax

Function name Formal Parameters /


Return type Arguments

data- type name( type 1 arg 1, type 2 arg 2, . . ., type n arg n )


{
......... Body of the
……….. function
}

23-11-2020 Ref: Programming with C, Byron S. Gottfried. 10


Return type Function name Formal Parameters /
Arguments

char lower_to_upper (char c 1){


Body of the
char c2; function
c2 = ( c1 >= 'a' && c1 <= ‘z' ) ? ( 'A' + c1 - 'a' ) : c1 ;
return(c2);
}
23-11-2020 Ref: Programming with C, Byron S. Gottfried. 11
return statement
• Information (only one value) is returned from the function to the
calling portion of the program via the return statement
• The return statement also causes the program logic to return to the
point from which the function was accessed
• General syntax
• return expression;
• The expression is optional
• A function definition can include multiple return statements

23-11-2020 Ref: Programming with C, Byron S. Gottfried. 12


Multiple return statement

char lower_to_upper (char c 1){


If(c1 >= 'a' && c1 <= ‘z' )
return ( 'A' + c1 - 'a' )
else
return(c1);
}

23-11-2020 Ref: Programming with C, Byron S. Gottfried. 13


References
• Byron S. Gottfried, “Programming with C”, McGraw Hill, Third Edition,
2017.

23-11-2020 Ref: Programming with C Byron S. Gottfried. 14

You might also like