100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Summary

Summary Functions in C programming

Rating
-
Sold
-
Pages
16
Uploaded on
24-08-2025
Written in
2025/2026

This document covers all important aspects of functions in C programming with clear explanations, notes, and solved exam-style questions. Topics include syntax, types of functions, parameter passing methods, return values, and scope of variables. It is designed to help students prepare effectively for exams by combining theory with practical examples and practice questions. Keywords: C programming exam notes functions in C questions and answers C programming important topics parameter passing exam questions return values in C solved examples types of functions notes scope of variables exam preparation

Show more Read less
Institution
Course










Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Course

Document information

Uploaded on
August 24, 2025
Number of pages
16
Written in
2025/2026
Type
Summary

Subjects

Content preview

Chapter 2
Functions

LEARNING OBJECTIVES

 Functions  Pass by value
 Library functions  Pass by address
 User defined functions  Scope
 Defining user defined functions  Life time
 Recursion  Binding
 Parameter passing




functions • A function receives zero (or) more parameters, performs a spe-
cific task, and returns zero or one value.
A function is a block of code that performs a specific task. It has a
• A function is invoked by its name and parameters.
name and is reusable, i.e., it can be executed from as many differ-
• No two functions have the same name in a single C program.
ent parts in a program as required.
• The communication between the function and invoker is through
Functions make possible top down modular programming. In this
the parameter and the return value.
style of programming, the high-level logic of overall problem is solved
• A function is independent.
first, whereas the detail of each lower-level function is addressed later.
• It is “completely” self-contained.
This approach reduces the complexity in writing program.
• It can be called at any place of your code and can be ported to
1. Every C program can be thought of collection of functions. another program.
2. main( ) is also a function. • Functions make programs reusable and readable.
Types of Functions Example 3: Return the largest of two integers.
Library functions int maximum (int a, int b)
{
These are the in-built functions of ‘C’ library. These are already
if (a > b)
defined in header files.
return a;
Example 1: printf( ); is a function which is used to print at output. else
It is defined in ‘stdio.h’ file. return b;
}
User-defined functions
Note: Function calls execute with the help of execution stack.
Programmers can create their own function in ‘C’ to perform spe-
Execution of ‘C program’ starts with main( ) function. Main( ) is a
cific tasks.
user-defined function.
Example 2: # include <stdio.h>
main( )
{ Defining User-defined Functions
message( ); In order to work with user-defined functions, it requires the follow-
} ing concepts about functions:
message( )
{ • Declaration of a function
printf(“Hello”); • Definition of a function
} • Function call

, Chapter 2 • Functions | 3.15

Declaration specifies what printf (“\n function Hello”);
return;
•• is the name of the function }
•• are the parameters to pass (type, order and number of
A return statement has two important uses:
parameters).
•• it returns on completion of execution 1. first, it causes an immediate exit from the function.
Example 4: int maximum (int, int); int maximum (int a, 2. second, it may be used to return a value.
int b);
If a function does not return any value, then the return
Syntax: statement is optional.
Return_type Function_Name(Parameter_list);

•• Names of parameters are optional in declaration.
•• Default return type for ‘C’ functions is ‘int’. Recursion
•• A function, whose return type is void returns nothing. In general, programmers use two approaches to write repeti-
•• Empty parenthesis after a function name in declaration tive algorithms. One approach using loops, the other is
says, function does not accept any parameters. recursion.
Recursive functions typically implement recurrence
Definition specifies how relations, which are mathematical formula in which the
•• to perform the specified task desired expression (function) involving a positive integer, n,
•• to accept passed parameters is described in terms of the function applied to correspond-
•• to process parameters or execute instruction to producer ing values for integers less than ‘n’.
equired results (return value).
1. The function written in terms of itself is a recursive
Function definition is a self-contained block of instructions, case.
will be executed on call: 2. The recursive case must call the function with a
decreasing ‘n’.
Syntax:
3. Recursion in computer programming is exemplified
Return _type Function -Name(paralist)
when a function defined in terms of itself.
{
4. Recursion is a repetitive process in which a function
Local declaration(s);
calls itself.
Executable statements(s);
} Note: Recursive function must have an if condition to force
int maximum (int a, int b) the function to return without recursive call being executed.
{ If there is no such condition, then the function execution
if (a > b) falls into infinite loop.
return a;
else
return b; Rules for designing recursive function
} 1. Determine base case
2. Determine general case
Function call specifies 3. Combine, base and general case into a function

1. where to execute the function
2. when to execute the function
Example 5: Recursive factorial function
Note: If the function definition provided before use (call),
the declaration is optional. 1. int factorial (int n)
2. {
The following example describes control flow during
3. if (n = = 0)
function call: 4. return 1;
void hello(); // Declaration 5. else
void main() 6. return (n ‫ ٭‬factorial (n − 1));
{ 7. }
printf(“\n function”);
The statement 3 is a base condition, which stops the recur-
hello();
sive call of function.
printf(“\n Main after call to hello”)
The statement 6 reduces the size of problem by recur-
void hello()//Definition sively calling the factorial with (n − 1).
{

, 3.16 | Unit 3 • Programming and Data Structures

Execution sequences for factorial (3): void swap2 (int *, int *); /* function to
swap two numbers by passing Address * /
Factorial (3) Factorial (3)   void main ()
= 3* factorial (2) 3* 2 = 6 {
int a = 10, b = 15, c = 5, d = 25;
printf(“value of a and b before swapping
Factorial (2) Factorial (2) :%d, %d” a , b );
= 2* factorial (1) 2* 1 = 2 swap1(a, b);
printf(“values of a and b after swapping :
%d, %d”, a, b);
Factorial (1)
printf (“values of c and d before swapping
Factorial (1)
1* factorial (0) 1* 1 = 1 :%d%d”, c,d );
Swap2(&c, &d);
printf(“values of c and d after swapping
%d, %d”, c, d);
Factorial (0) = 1 }
void swap1(int x, int y )
{
Disadvantages: int temp;
1. Recursive programs increase the execution time of temp = x;
program. x = y;
2. Recursive programs typically use a large amount of y = temp;
computer memory and the greater the recursion, the }
void swap2 (int *x, int *y)
more memory is used.
{
3. Recursive programs can be confusing to develop and int temp;
extremely complicated to debug. temp = *x;
*x = *y:
Parameter Passing *y = temp;
There are two ways of passing parameters to functions in }
‘C’ language. Output:
1. Pass-by-value: When parameters are passed by value, Value of a and b before swapping: 10, 15
create copies in called function. This mechanism Value of a and b after swapping: 10, 15
is used when we do not want to change the value of Value of c and d before swapping: 5, 25
actual parameters. Value of c and d after swapping: 25, 5
2. Pass-by-address: In this case, only the addresses
of parameters are passed to the called function.
Therefore, manipulation of formal parameters affects Solved Examples
actual parameters.
Examples 6: Example 1: Consider the program below:
void swap1(int, int); /* function – to swap #include<stdio.h>
two numbers by passing values */ int fun (int n, int *fp)



Table 1 Comparison of pass-by-value and pass-by-address

Pass-by-value Pass-by-address
1. Also known as call-by-value 1. Also known as call-by-address or call by-reference
2. Pass the values of actual parameters 2. Pass the address of actual parameters
3. Formal parameters act as duplicates or as a copy to actual 3. Formal parameters acts as references to the actual
parameters parameters
4. Operations on formal parameter does not affect actual 4. Operations on formal parameters affect actual parameters
parameters
5. Passing of parameters is time consuming as the data size 5. The size of parameters does not affect the time for transfer-
increases ring references.
6. Actual parameters are secured 6. Helps to return multiple parameters
$6.49
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
yogendra

Get to know the seller

Seller avatar
yogendra Poornima University
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
3 months
Number of followers
0
Documents
2
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions