Вы находитесь на странице: 1из 14

FUNCTIONS

A function is a section of program code that


performs a particular task. Making functions is
a way of isolating one section of code from
other independent sections. Functions allow a
programmer to separate code by its purpose,
and make a section of code reusable — that is,
make it so the section can be called in many
different contexts.
Functions should be written in the following
form:
type function name (type parameter1 name,
type parameter2 name, ...)
{
variable declarations
statements
A function can have a number of parameters, or
pieces of information from outside, and the
function’s body consists of a number of
declarations and statements,enclosed by curly
brackets: ‘{...}’.
Function names
Every function has a name by which it is known to
the rest of the program. The name of a function in
C can be anything from a single letter to a long
word. A function name must begin with an
alphabetic letter or the underscore ‘_’
character,but the other characters in the name can
be chosen from the following groups:
• Any lower-case letter from ‘a’ to ‘z’
• Any upper-case letter from ‘A’ to ‘Z’
Function examples
Example of a function that adds two integers
and prints the sum
void add_two_numbers (int a, int b)
{
int c;
c = a + b;
printf ("%d\n", c);}
The variables a and b are parameters passed in
from outside the function. The code defines a,
b, and c to be of type int.
The function above is not much use standing
alone. Here is a main function that calls the
int main()
{
int var1, var2;
var1 = 1;
var2 = 53;
add_two_numbers (var1, var2);
add_two_numbers (1, 2);
}
Functions with values
In mathematics, a function takes one or more
values and calculates, or returns, another value.
In C, some functions return values and others do
not; whether a function you write does or does
not will depend on what you want the function
to do. For example, a function that calculates a
value should probably return that value, while a
function that merely prints something out may
not need to.
The add_two_numbers function above did not
return a value. bill = calculate_bill (data1,
data2, data3);
When this statement is executed, control is
passed to the function calculate_bill, that
function executes, and then it returns control
int calculate_bill (int a, int b, int c){
int total;
total = a + b + c;
return total;}
As soon as the return statement is met,
calculate_bill stops executing and returns the
value total.
A function that returns a value must have a
return statement. For instance if calculate_bill
had read as follows, then the variable bill would
have had no meaningful value assigned to it, and
you might have received a warning from the
compiler . (The word void below indicates that
the function does not return a value.
void calculate_bill (int a, int b, int c)
{int total;
total = a + b + c;}
Function prototyping
Functions do not have to return integer values,
as in the above examples, but can return almost
any type of value, including floating point and
character values. (A function must be declared to
return a certain variable type (such as an
integer), just as variables must be.) To write code
in good C style, you should declare what type of
value a function returns (and what type of
parameters it accepts) in two places:
1.   At the beginning of the program, in global
scope.
2.   In the definition of the function itself.
Function declarations at the beginning of a
program are called prototypes.
Example of a program in which prototypes are
used:
#include <stdio.h>
void print_stuff (int foo, int bar);
int calc_value (int bas, int quux);
void print_stuff (int foo, int bar)
{
int var_to_print;
var_to_print = calc_value (foo, bar);
printf ("var_to_print = %d\n", var_to_print);
}
int calc_value (int bas, int quux)
return bas * quux;
}
int main() {
print_stuff (23, 5);
}
Without function prototypes, you usually
cannot write code that calls a function before
the function itself is defined in the program .
If you place prototypes for your functions in a
header file, however, you can call the functions
from any source code file that includes the
header. This is one reason C is considered to be
such a flexible programming language.
Some compilers avoid the use of prototypes by
making a first pass just to see what functions
are there, and a second pass to do the work,
but this takes about twice as long.
Programmers already hate the time compilers
take, and do not want to use compilers that
make unnecessary passes on their source code,
making prototypes a necessity. Also,prototypes
enable the C compiler to do more rigorous error
checking, and that saves an enormous amount
of time .
The exit function
We can use the exit function to terminate a
program at any point, no matter how many
function calls have been made. Before it
terminates the program, it calls a number of
other functions that perform tidy-up duties such
as closing open files.
exit is called with a return code, like this:
exit(0);
In the example above, the return code is 0.
Any program that calls your program can
read the return code from your program.
The return code is like a return value from
another function that is not main; in fact,
most of the time you can use the return
command within your main, instead of exit.
Conventionally, a return code of 0 specifies
that your program has ended normally and
all is well. (You can remember this as “zero
errors”, although for technical reasons, you
cannot use the number of errors your
program found as the return code. A return
code other than 0 indicates that some sort
of error has occurred.
If your code terminates when it encounters

Вам также может понравиться

  • Programming Modules
    Programming Modules
    Документ14 страниц
    Programming Modules
    franklyne muchui
    Оценок пока нет
  • A Function in
    A Function in
    Документ2 страницы
    A Function in
    Naveen Chandra
    Оценок пока нет
  • ICT3 Function
    ICT3 Function
    Документ25 страниц
    ICT3 Function
    Jadidah Saripada
    Оценок пока нет
  • C Assignment
    C Assignment
    Документ14 страниц
    C Assignment
    Satyam Singh
    Оценок пока нет
  • What Is Function in C Language
    What Is Function in C Language
    Документ4 страницы
    What Is Function in C Language
    vinodapache
    Оценок пока нет
  • C Functions
    C Functions
    Документ43 страницы
    C Functions
    prabhasavvaru0609
    Оценок пока нет
  • Lab 07: Functions: National University of Technology
    Lab 07: Functions: National University of Technology
    Документ8 страниц
    Lab 07: Functions: National University of Technology
    R k
    Оценок пока нет
  • Function in C
    Function in C
    Документ12 страниц
    Function in C
    Brajesh Kumar
    Оценок пока нет
  • Functions in C
    Functions in C
    Документ19 страниц
    Functions in C
    Zed watt
    Оценок пока нет
  • C Programming
    C Programming
    Документ30 страниц
    C Programming
    Backiyalakshmi Venkatraman
    Оценок пока нет
  • Function in C
    Function in C
    Документ12 страниц
    Function in C
    Goldy Batra
    100% (1)
  • Functions in C
    Functions in C
    Документ14 страниц
    Functions in C
    soonge
    Оценок пока нет
  • Unit-4 CTSD
    Unit-4 CTSD
    Документ56 страниц
    Unit-4 CTSD
    anshikahjjp
    Оценок пока нет
  • Unit 5 - Functions
    Unit 5 - Functions
    Документ25 страниц
    Unit 5 - Functions
    Anurag Goel
    Оценок пока нет
  • 1) Functions in C/C++
    1) Functions in C/C++
    Документ40 страниц
    1) Functions in C/C++
    Irene Sultana
    Оценок пока нет
  • Lec 8
    Lec 8
    Документ28 страниц
    Lec 8
    toin4m
    Оценок пока нет
  • ICP Ref Material
    ICP Ref Material
    Документ17 страниц
    ICP Ref Material
    Dimon He
    Оценок пока нет
  • cst181-1 Lecture8
    cst181-1 Lecture8
    Документ28 страниц
    cst181-1 Lecture8
    toin4m
    Оценок пока нет
  • C Functions Explanation
    C Functions Explanation
    Документ11 страниц
    C Functions Explanation
    Prakash
    100% (1)
  • Functions in C++ PDF
    Functions in C++ PDF
    Документ15 страниц
    Functions in C++ PDF
    Anonymous EpmRbw
    Оценок пока нет
  • Unit 3 PPS
    Unit 3 PPS
    Документ34 страницы
    Unit 3 PPS
    GANESH jadar
    Оценок пока нет
  • LP3 - Unit 4 - C++ Functions
    LP3 - Unit 4 - C++ Functions
    Документ6 страниц
    LP3 - Unit 4 - C++ Functions
    Ariel Verzosa
    Оценок пока нет
  • Unit1-User Defined Functions
    Unit1-User Defined Functions
    Документ8 страниц
    Unit1-User Defined Functions
    issei dragneel
    Оценок пока нет
  • Chapter 12
    Chapter 12
    Документ12 страниц
    Chapter 12
    alysonmicheaala
    Оценок пока нет
  • 13 Function
    13 Function
    Документ7 страниц
    13 Function
    kneel
    Оценок пока нет
  • C - Using Functions
    C - Using Functions
    Документ3 страницы
    C - Using Functions
    kok_oc25
    Оценок пока нет
  • UNIT3
    UNIT3
    Документ42 страницы
    UNIT3
    Vivekananda Ganjigunta Narayana
    Оценок пока нет
  • CP Unit-5 (R-23)
    CP Unit-5 (R-23)
    Документ35 страниц
    CP Unit-5 (R-23)
    sivapc3105
    Оценок пока нет
  • Practical Lecture: Function: Editedit Mastermaster Texttext Stylesstyles
    Practical Lecture: Function: Editedit Mastermaster Texttext Stylesstyles
    Документ40 страниц
    Practical Lecture: Function: Editedit Mastermaster Texttext Stylesstyles
    Dhruva is live
    Оценок пока нет
  • Lab-07 Functions: Contents
    Lab-07 Functions: Contents
    Документ7 страниц
    Lab-07 Functions: Contents
    farzana kousar
    Оценок пока нет
  • Session 5 Structure of Program
    Session 5 Structure of Program
    Документ8 страниц
    Session 5 Structure of Program
    Gandavadi Venkatesh
    Оценок пока нет
  • Functions and Recursion
    Functions and Recursion
    Документ3 страницы
    Functions and Recursion
    OPM Rocky
    Оценок пока нет
  • Lecture2 Part1
    Lecture2 Part1
    Документ21 страница
    Lecture2 Part1
    amin khalilian
    Оценок пока нет
  • C++ Chapter Four
    C++ Chapter Four
    Документ11 страниц
    C++ Chapter Four
    Aynalem
    Оценок пока нет
  • Input Operator:: Cascading of I/O Operators
    Input Operator:: Cascading of I/O Operators
    Документ21 страница
    Input Operator:: Cascading of I/O Operators
    Vishal Gupta
    Оценок пока нет
  • Engineering Research Project Proposal 2016
    Engineering Research Project Proposal 2016
    Документ15 страниц
    Engineering Research Project Proposal 2016
    subash
    Оценок пока нет
  • Functions
    Functions
    Документ33 страницы
    Functions
    Satgur gaming world
    Оценок пока нет
  • C Programming Functions and Recursion
    C Programming Functions and Recursion
    Документ15 страниц
    C Programming Functions and Recursion
    mithun d'souza
    Оценок пока нет
  • Unit-4... PPS
    Unit-4... PPS
    Документ25 страниц
    Unit-4... PPS
    Harini Kolli
    Оценок пока нет
  • Lecture6 C
    Lecture6 C
    Документ19 страниц
    Lecture6 C
    jonathan steven
    Оценок пока нет
  • Chapter 1
    Chapter 1
    Документ65 страниц
    Chapter 1
    husseneman325
    Оценок пока нет
  • UNIT III Functions
    UNIT III Functions
    Документ18 страниц
    UNIT III Functions
    Sivasathiya G
    Оценок пока нет
  • Chapter 3 Function
    Chapter 3 Function
    Документ73 страницы
    Chapter 3 Function
    Demeke
    Оценок пока нет
  • Unit 5 (C++) - Function
    Unit 5 (C++) - Function
    Документ102 страницы
    Unit 5 (C++) - Function
    abdi
    Оценок пока нет
  • Copy of UNIT - V - Functions
    Copy of UNIT - V - Functions
    Документ39 страниц
    Copy of UNIT - V - Functions
    arshadroyale111
    Оценок пока нет
  • User Defined Function Recursion
    User Defined Function Recursion
    Документ35 страниц
    User Defined Function Recursion
    Robiul Islam
    Оценок пока нет
  • Est 102 Module 4
    Est 102 Module 4
    Документ37 страниц
    Est 102 Module 4
    VYSAGH OFFICIAL CSE
    Оценок пока нет
  • Functions in C Programming
    Functions in C Programming
    Документ15 страниц
    Functions in C Programming
    Vignesh Murali
    100% (1)
  • Unit 4
    Unit 4
    Документ62 страницы
    Unit 4
    akhilyadavjerri488
    Оценок пока нет
  • Pemrograman Dasar
    Pemrograman Dasar
    Документ50 страниц
    Pemrograman Dasar
    Samuel Hamonangan
    Оценок пока нет
  • Functions in C
    Functions in C
    Документ13 страниц
    Functions in C
    Shivam Saxena
    Оценок пока нет
  • CENG 106 Lab7
    CENG 106 Lab7
    Документ8 страниц
    CENG 106 Lab7
    Wassen Hejjawi
    Оценок пока нет
  • C Functions
    C Functions
    Документ21 страница
    C Functions
    halfblood prince
    Оценок пока нет
  • C++ Chapter 4 Function
    C++ Chapter 4 Function
    Документ15 страниц
    C++ Chapter 4 Function
    Hanan Fuad
    Оценок пока нет
  • Functions
    Functions
    Документ44 страницы
    Functions
    HEALTHY TRICKS
    Оценок пока нет
  • Task 3 Algo
    Task 3 Algo
    Документ6 страниц
    Task 3 Algo
    Rizal Hakim
    Оценок пока нет
  • 1-Progarmming in C
    1-Progarmming in C
    Документ93 страницы
    1-Progarmming in C
    abubakirseifu
    Оценок пока нет
  • ADC - C Functions
    ADC - C Functions
    Документ7 страниц
    ADC - C Functions
    Mihigo ER Anaja
    Оценок пока нет
  • Summary of Basic C++-Commands
    Summary of Basic C++-Commands
    Документ5 страниц
    Summary of Basic C++-Commands
    primesam
    Оценок пока нет
  • Dive Into Sea of C
    Dive Into Sea of C
    От Everand
    Dive Into Sea of C
    Оценок пока нет
  • Filter Tutorial
    Filter Tutorial
    Документ15 страниц
    Filter Tutorial
    NikitaPrabhu
    Оценок пока нет
  • Adsp 2181
    Adsp 2181
    Документ32 страницы
    Adsp 2181
    api-3774180
    Оценок пока нет
  • Class 2
    Class 2
    Документ90 страниц
    Class 2
    api-3774180
    Оценок пока нет
  • Class 3
    Class 3
    Документ41 страница
    Class 3
    api-3774180
    Оценок пока нет
  • Device Drivers 1
    Device Drivers 1
    Документ14 страниц
    Device Drivers 1
    api-3774180
    Оценок пока нет
  • Class 3
    Class 3
    Документ37 страниц
    Class 3
    api-3774180
    Оценок пока нет
  • Dbms Pracs
    Dbms Pracs
    Документ6 страниц
    Dbms Pracs
    pratiksha55
    Оценок пока нет
  • Object Oriented Programming Through C++
    Object Oriented Programming Through C++
    Документ28 страниц
    Object Oriented Programming Through C++
    tuniya4
    Оценок пока нет
  • Js Sandeep
    Js Sandeep
    Документ248 страниц
    Js Sandeep
    venusd95
    Оценок пока нет
  • Bascom Avr Tutorial
    Bascom Avr Tutorial
    Документ22 страницы
    Bascom Avr Tutorial
    rezakaihani
    Оценок пока нет
  • TCL 3 2015.00 LG 01
    TCL 3 2015.00 LG 01
    Документ16 страниц
    TCL 3 2015.00 LG 01
    kehren
    Оценок пока нет
  • A Semantic Approach To English Grammar 2nd Edition
    A Semantic Approach To English Grammar 2nd Edition
    Документ67 страниц
    A Semantic Approach To English Grammar 2nd Edition
    Akshay Babbar
    Оценок пока нет
  • Oops
    Oops
    Документ227 страниц
    Oops
    Prema Raju
    Оценок пока нет
  • CIT - 206 - Lecture - 3 - Introduction To Event Driven Programming
    CIT - 206 - Lecture - 3 - Introduction To Event Driven Programming
    Документ8 страниц
    CIT - 206 - Lecture - 3 - Introduction To Event Driven Programming
    Thobius Joseph
    Оценок пока нет
  • Workforce Management (WFM) Fast Formula General Information
    Workforce Management (WFM) Fast Formula General Information
    Документ47 страниц
    Workforce Management (WFM) Fast Formula General Information
    Yonny Isidro Rendon
    Оценок пока нет
  • Smart Script User Manual
    Smart Script User Manual
    Документ130 страниц
    Smart Script User Manual
    Priya
    Оценок пока нет
  • Programming1 - In-Java PDF
    Programming1 - In-Java PDF
    Документ147 страниц
    Programming1 - In-Java PDF
    Pedja
    Оценок пока нет
  • C Viva Questions
    C Viva Questions
    Документ9 страниц
    C Viva Questions
    Lakshmi Lakshminarayana
    80% (5)
  • S7-SCL For S7-300 and S7-400 - Manual
    S7-SCL For S7-300 and S7-400 - Manual
    Документ364 страницы
    S7-SCL For S7-300 and S7-400 - Manual
    Юрий Казимирко
    Оценок пока нет
  • Systemverilog - Lecture 1
    Systemverilog - Lecture 1
    Документ62 страницы
    Systemverilog - Lecture 1
    Shilpa Reddy
    Оценок пока нет
  • OpenSTAAD Reference V8i PDF
    OpenSTAAD Reference V8i PDF
    Документ233 страницы
    OpenSTAAD Reference V8i PDF
    concretemad
    Оценок пока нет
  • Error Handling in C-1-1
    Error Handling in C-1-1
    Документ9 страниц
    Error Handling in C-1-1
    Abdiyasin Mohamed
    Оценок пока нет
  • Research Paper On C Programming Language PDF
    Research Paper On C Programming Language PDF
    Документ6 страниц
    Research Paper On C Programming Language PDF
    iwnlpjrif
    100% (1)
  • Build Your Own C Interpreter
    Build Your Own C Interpreter
    Документ18 страниц
    Build Your Own C Interpreter
    Richard Ferguson
    Оценок пока нет
  • Lotus Script Language Guide
    Lotus Script Language Guide
    Документ614 страниц
    Lotus Script Language Guide
    whatever_no_idea5191
    Оценок пока нет
  • Mql4 Course
    Mql4 Course
    Документ47 страниц
    Mql4 Course
    Kolyo Dankov
    100% (1)
  • S900II Programming Level I V21
    S900II Programming Level I V21
    Документ82 страницы
    S900II Programming Level I V21
    Francisco Macias
    100% (1)
  • 1.5 JavaScript Functions CH - 10
    1.5 JavaScript Functions CH - 10
    Документ58 страниц
    1.5 JavaScript Functions CH - 10
    Selva Ganesh
    Оценок пока нет
  • GR 12 - CS - Quick Revision Notes and Important Questions
    GR 12 - CS - Quick Revision Notes and Important Questions
    Документ131 страница
    GR 12 - CS - Quick Revision Notes and Important Questions
    shrinsaliha532
    Оценок пока нет
  • 05 Intro To Recursion PDF
    05 Intro To Recursion PDF
    Документ28 страниц
    05 Intro To Recursion PDF
    Sabahat Kanwal
    Оценок пока нет
  • Introduction To PHP Functions
    Introduction To PHP Functions
    Документ11 страниц
    Introduction To PHP Functions
    Mma
    Оценок пока нет
  • Solution Mannual PDF
    Solution Mannual PDF
    Документ246 страниц
    Solution Mannual PDF
    lakshmi
    Оценок пока нет
  • Manual PDL
    Manual PDL
    Документ45 страниц
    Manual PDL
    arfernandesjr
    Оценок пока нет
  • Everything You Need To Know To Start With C
    Everything You Need To Know To Start With C
    Документ53 страницы
    Everything You Need To Know To Start With C
    Jorge Salazar
    Оценок пока нет
  • (Pick The Date) : PPG Institute of Technology
    (Pick The Date) : PPG Institute of Technology
    Документ34 страницы
    (Pick The Date) : PPG Institute of Technology
    Anonymous EImkf6RGdQ
    Оценок пока нет
  • Blocks Programming Topics
    Blocks Programming Topics
    Документ25 страниц
    Blocks Programming Topics
    lekali
    Оценок пока нет