Unit 3 Topic 2
Unit 3 Topic 2
Topic 2
Functions are used to perform certain actions, and they are important for
reusing code: Define the code once, and use it many times.
Predefined Functions
Example:
Create a Function
Syntax:
void myFunction()
{// code to be executed
}
Example Explained:
Call a Function
Declared functions are not executed immediately. They are "saved for
later use", and will be executed when they are called.
Example:
You can put almost whatever you want inside a function. The purpose of
the function is to save the code, and execute it when you need it.
Example:
C function parameters
Parameters and Arguments
Parameters are specified after the function name, inside the parentheses.
You can add as many parameters as you want, just separate them with a
comma
Syntax:
returnType functionName(parameter1, parameter2, parameter3)
{
// code to be executed
}
In the example below, the function takes a string of
characters with name as parameter. When the function is called, we pass
along a name, which is used inside the function to print "Hello" and the
name of each person:
Inside the function, you can add as many parameters as you want.
Note: When you are working with multiple parameters, the function call
must have the same number of arguments as there are parameters, and
the arguments must be passed in the same order.
Pass Arrays as Function Parameters
Example:
Example Explained:
Return Values
The void keyword, used in the previous examples, indicates that the
function should not return a value. If you want the function to return a
value, you can use a data type (such as int or float, etc.) instead of void,
and use the return keyword inside the function:
C Variable Scope
In C, variables are only accessible inside the region they are created.
This is called scope.
Local Scope
Example:
A local variable cannot be used outside the function it belongs to.
Global Scope
Global variables are available from within any scope, global and local.
Example:
If you operate with the same variable name inside and outside of a
function, C will treat them as two separate variables; One available in
the global scope (outside the function) and one available in the local
scope (inside the function).
Example:
The function will print the local x, and then the code will print the
global x:
However, you should avoid using the same variable name for both
globally and locally variables as it can lead to errors and confusion.
In general, you should be careful with global variables, since they can be
accessed and modified from any function:
Example:
void myFunction()
{ // declaration
// the body of the function (definition)
}
Recursion Example
Example:
Example Explained:
When the sum() function is called, it adds parameter k to the sum of all
numbers smaller than k and returns the result. When k becomes 0, the
function just returns 0. When running, the program follows these steps,
10 + sum(9)
10 + ( 9 + sum(8) )
10 + ( 9 + ( 8 + sum(7) ) )
...
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + sum(0)
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0
Since the function does not call itself when k is 0, the program stops
there and returns the result.
Math Functions
There is also a list of math functions available, that allows you to
perform mathematical tasks on numbers.
To use them, you must include the math.h header file in your program:
#include <math.h>
Square Root
The ceil() function rounds a number upwards to its nearest integer, and
the floor() method rounds a number downwards to its nearest integer,
and returns the result:
Power
Structure of program
The basic structure of a C program is divided into 6 parts which makes
it easy to read, modify, document, and understand in a particular
format. C program must follow the below-mentioned outline in order to
successfully compile and execute.
Debugging is easier in a well-structured C program.
Sections of the C Program
Documentation:
This section consists of the description of the program, the name of the
program, and the creation date and time of the program. It is specified
at the start of the program in the form of comments. Documentation can
be represented as:
// description, name of the program, programmer name, date, time
etc.
or
/*
description, name of the program, programmer name, date, time
etc.
*/
#include<stdio.h>
#include<math.h>
Definition
Preprocessors are the programs that process our source code before the
process of compilation. There are multiple steps which are involved in
the writing and execution of the program. Preprocessor directives start
with the ‘#’ symbol. The #define preprocessor is used to create a
constant throughout the program. Whenever this name is encountered
by the compiler, it is replaced by the actual piece of defined code.
Example:
#define long long ll
Global Declaration
Sub Programs
Example:
int sum(int x, int y)
{
return x+y;
}
Structure of C Program with example
Example:
Below C program to find the sum of 2 numbers.
// Documentation
/**
* file: sum.c
* author: you
* description: program to find sum.
*/
// Link
#include <stdio.h>
// Definition
#define X 20
// Global Declaration
int sum(int y);
// Main() Function
int main(void)
{
int y = 55;
printf("Sum: %d", sum(y));
return 0;
}
// Subprogram
int sum(int y)
{
return y + X;
}
Output
Sum: 75
Steps involved in the Compilation and execution of a C
program-
Program Creation
Compilation of the program
Execution of the program
The output of the program